|
1 | 1 | import os |
2 | 2 | import toml |
| 3 | +import toml.ast |
| 4 | +import x.json2 |
| 5 | +import strconv |
3 | 6 |
|
4 | 7 | // Instructions for developers: |
5 | 8 | // The actual tests and data can be obtained by doing: |
@@ -140,7 +143,9 @@ fn test_burnt_sushi_tomltest() { |
140 | 143 | bs_toml_json_path := os.join_path(compare_work_dir_root, |
141 | 144 | os.file_name(valid_test_file).all_before_last('.') + '.json') |
142 | 145 |
|
143 | | - os.write_file(v_toml_json_path, toml_doc.to_burntsushi()) or { panic(err) } |
| 146 | + os.write_file(v_toml_json_path, to_burntsushi(toml_doc.ast.table)) or { |
| 147 | + panic(err) |
| 148 | + } |
144 | 149 |
|
145 | 150 | bs_json := os.read_file(valid_test_file.all_before_last('.') + '.json') or { |
146 | 151 | panic(err) |
@@ -206,3 +211,68 @@ fn test_burnt_sushi_tomltest() { |
206 | 211 | assert true |
207 | 212 | } |
208 | 213 | } |
| 214 | + |
| 215 | +// to_burntsushi returns a BurntSushi compatible json string converted from the `value` ast.Value. |
| 216 | +fn to_burntsushi(value ast.Value) string { |
| 217 | + match value { |
| 218 | + ast.Quoted { |
| 219 | + json_text := json2.Any(value.text).json_str() |
| 220 | + return '{ "type": "string", "value": "$json_text" }' |
| 221 | + } |
| 222 | + ast.DateTime { |
| 223 | + // Normalization for json |
| 224 | + json_text := json2.Any(value.text).json_str().to_upper().replace(' ', 'T') |
| 225 | + typ := if json_text.ends_with('Z') || json_text.all_after('T').contains('-') |
| 226 | + || json_text.all_after('T').contains('+') { |
| 227 | + 'datetime' |
| 228 | + } else { |
| 229 | + 'datetime-local' |
| 230 | + } |
| 231 | + return '{ "type": "$typ", "value": "$json_text" }' |
| 232 | + } |
| 233 | + ast.Date { |
| 234 | + json_text := json2.Any(value.text).json_str() |
| 235 | + return '{ "type": "date-local", "value": "$json_text" }' |
| 236 | + } |
| 237 | + ast.Time { |
| 238 | + json_text := json2.Any(value.text).json_str() |
| 239 | + return '{ "type": "time-local", "value": "$json_text" }' |
| 240 | + } |
| 241 | + ast.Bool { |
| 242 | + json_text := json2.Any(value.text.bool()).json_str() |
| 243 | + return '{ "type": "bool", "value": "$json_text" }' |
| 244 | + } |
| 245 | + ast.Null { |
| 246 | + json_text := json2.Any(value.text).json_str() |
| 247 | + return '{ "type": "null", "value": "$json_text" }' |
| 248 | + } |
| 249 | + ast.Number { |
| 250 | + if value.text.contains('.') || value.text.to_lower().contains('e') { |
| 251 | + json_text := value.text.f64() |
| 252 | + return '{ "type": "float", "value": "$json_text" }' |
| 253 | + } |
| 254 | + i64_ := strconv.parse_int(value.text, 0, 0) or { i64(0) } |
| 255 | + return '{ "type": "integer", "value": "$i64_" }' |
| 256 | + } |
| 257 | + map[string]ast.Value { |
| 258 | + mut str := '{ ' |
| 259 | + for key, val in value { |
| 260 | + json_key := json2.Any(key).json_str() |
| 261 | + str += ' "$json_key": ${to_burntsushi(val)},' |
| 262 | + } |
| 263 | + str = str.trim_right(',') |
| 264 | + str += ' }' |
| 265 | + return str |
| 266 | + } |
| 267 | + []ast.Value { |
| 268 | + mut str := '[ ' |
| 269 | + for val in value { |
| 270 | + str += ' ${to_burntsushi(val)},' |
| 271 | + } |
| 272 | + str = str.trim_right(',') |
| 273 | + str += ' ]\n' |
| 274 | + return str |
| 275 | + } |
| 276 | + } |
| 277 | + return '<error>' |
| 278 | +} |
0 commit comments