| 1 | module vmod |
| 2 | |
| 3 | import strings |
| 4 | |
| 5 | fn quote(input string) string { |
| 6 | if input.contains("'") { |
| 7 | return '"' + input + '"' |
| 8 | } |
| 9 | return "'" + input + "'" |
| 10 | } |
| 11 | |
| 12 | fn encode_array(mut b strings.Builder, input []string) { |
| 13 | if input.join('').len > 60 { |
| 14 | b.writeln('[') |
| 15 | for item in input { |
| 16 | b.write_string('\t\t') |
| 17 | b.write_string(quote(item)) |
| 18 | b.writeln(',') |
| 19 | } |
| 20 | b.writeln('\t]') |
| 21 | } else { |
| 22 | mut quoted := []string{} |
| 23 | for item in input { |
| 24 | quoted << quote(item) |
| 25 | } |
| 26 | b.write_string('[') |
| 27 | b.write_string(quoted.join(', ')) |
| 28 | b.writeln(']') |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub fn encode(manifest Manifest) string { |
| 33 | mut b := strings.new_builder(512) |
| 34 | b.writeln('Module {') |
| 35 | b.write_string('\tname: ') |
| 36 | b.writeln(quote(manifest.name)) |
| 37 | if manifest.base_url != '' { |
| 38 | b.write_string('\tbase_url: ') |
| 39 | b.writeln(quote(manifest.base_url)) |
| 40 | } |
| 41 | b.write_string('\tdescription: ') |
| 42 | b.writeln(quote(manifest.description)) |
| 43 | b.write_string('\tversion: ') |
| 44 | b.writeln(quote(manifest.version)) |
| 45 | b.write_string('\tlicense: ') |
| 46 | b.writeln(quote(manifest.license)) |
| 47 | if manifest.repo_url != '' { |
| 48 | b.write_string('\trepo_url: ') |
| 49 | b.writeln(quote(manifest.repo_url)) |
| 50 | } |
| 51 | if manifest.author != '' { |
| 52 | b.write_string('\tauthor: ') |
| 53 | b.writeln(quote(manifest.author)) |
| 54 | } |
| 55 | b.write_string('\tdependencies: ') |
| 56 | encode_array(mut b, manifest.dependencies) |
| 57 | for key, values in manifest.unknown { |
| 58 | b.write_string('\t') |
| 59 | b.write_string(key) |
| 60 | b.write_string(': ') |
| 61 | encode_array(mut b, values) |
| 62 | } |
| 63 | b.write_string('}') |
| 64 | return b.str() |
| 65 | } |
| 66 | |