tylax 0.3.5

Tylax - High-performance bidirectional LaTeX ↔ Typst converter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Data loading functions for MiniEval.
//!
//! This module provides functions for loading data from various formats:
//! - JSON
//! - YAML
//! - TOML
//! - CSV
//!
//! These are equivalent to Typst's built-in data loading functions.

use indexmap::IndexMap;

use super::value::{EvalError, EvalResult, Value};

/// Parse JSON string into Value.
pub fn parse_json(input: &str) -> EvalResult<Value> {
    #[cfg(feature = "data-loading")]
    {
        use serde_json::Value as JsonValue;
        let json: JsonValue = serde_json::from_str(input)
            .map_err(|e| EvalError::other(format!("JSON parse error: {}", e)))?;
        json_to_value(json)
    }

    #[cfg(not(feature = "data-loading"))]
    {
        // Fallback: simple JSON parser for basic cases
        parse_json_simple(input)
    }
}

/// Parse YAML string into Value.
pub fn parse_yaml(input: &str) -> EvalResult<Value> {
    #[cfg(feature = "data-loading")]
    {
        use serde_yaml::Value as YamlValue;
        let yaml: YamlValue = serde_yaml::from_str(input)
            .map_err(|e| EvalError::other(format!("YAML parse error: {}", e)))?;
        yaml_to_value(yaml)
    }

    #[cfg(not(feature = "data-loading"))]
    {
        let _ = input; // Suppress unused variable warning
        Err(EvalError::other(
            "YAML parsing requires 'data-loading' feature".to_string(),
        ))
    }
}

/// Parse TOML string into Value.
pub fn parse_toml(input: &str) -> EvalResult<Value> {
    #[cfg(feature = "data-loading")]
    {
        let toml_value: toml::Value = input
            .parse()
            .map_err(|e| EvalError::other(format!("TOML parse error: {}", e)))?;
        toml_to_value(toml_value)
    }

    #[cfg(not(feature = "data-loading"))]
    {
        let _ = input; // Suppress unused variable warning
        Err(EvalError::other(
            "TOML parsing requires 'data-loading' feature".to_string(),
        ))
    }
}

/// Parse CSV string into array of arrays/dicts.
pub fn parse_csv(input: &str, has_header: bool) -> EvalResult<Value> {
    #[cfg(feature = "data-loading")]
    {
        let mut reader = csv::ReaderBuilder::new()
            .has_headers(has_header)
            .from_reader(input.as_bytes());

        if has_header {
            let headers: Vec<String> = reader
                .headers()
                .map_err(|e| EvalError::other(format!("CSV header error: {}", e)))?
                .iter()
                .map(|s| s.to_string())
                .collect();

            let mut rows = Vec::new();
            for result in reader.records() {
                let record =
                    result.map_err(|e| EvalError::other(format!("CSV record error: {}", e)))?;
                let mut row = IndexMap::new();
                for (i, value) in record.iter().enumerate() {
                    let key = headers.get(i).cloned().unwrap_or_else(|| i.to_string());
                    row.insert(key, Value::Str(value.to_string()));
                }
                rows.push(Value::Dict(row));
            }
            Ok(Value::Array(rows))
        } else {
            let mut rows = Vec::new();
            for result in reader.records() {
                let record =
                    result.map_err(|e| EvalError::other(format!("CSV record error: {}", e)))?;
                let row: Vec<Value> = record.iter().map(|s| Value::Str(s.to_string())).collect();
                rows.push(Value::Array(row));
            }
            Ok(Value::Array(rows))
        }
    }

    #[cfg(not(feature = "data-loading"))]
    {
        // Simple CSV parser for basic cases
        parse_csv_simple(input, has_header)
    }
}

// ============================================================================
// Conversion helpers for data-loading feature
// ============================================================================

#[cfg(feature = "data-loading")]
fn json_to_value(json: serde_json::Value) -> EvalResult<Value> {
    use serde_json::Value as JsonValue;
    Ok(match json {
        JsonValue::Null => Value::None,
        JsonValue::Bool(b) => Value::Bool(b),
        JsonValue::Number(n) => {
            if let Some(i) = n.as_i64() {
                Value::Int(i)
            } else if let Some(f) = n.as_f64() {
                Value::Float(f)
            } else {
                Value::Str(n.to_string())
            }
        }
        JsonValue::String(s) => Value::Str(s),
        JsonValue::Array(arr) => {
            let values: Result<Vec<_>, _> = arr.into_iter().map(json_to_value).collect();
            Value::Array(values?)
        }
        JsonValue::Object(obj) => {
            let mut dict = IndexMap::new();
            for (k, v) in obj {
                dict.insert(k, json_to_value(v)?);
            }
            Value::Dict(dict)
        }
    })
}

#[cfg(feature = "data-loading")]
fn yaml_to_value(yaml: serde_yaml::Value) -> EvalResult<Value> {
    use serde_yaml::Value as YamlValue;
    Ok(match yaml {
        YamlValue::Null => Value::None,
        YamlValue::Bool(b) => Value::Bool(b),
        YamlValue::Number(n) => {
            if let Some(i) = n.as_i64() {
                Value::Int(i)
            } else if let Some(f) = n.as_f64() {
                Value::Float(f)
            } else {
                Value::Str(n.to_string())
            }
        }
        YamlValue::String(s) => Value::Str(s),
        YamlValue::Sequence(arr) => {
            let values: Result<Vec<_>, _> = arr.into_iter().map(yaml_to_value).collect();
            Value::Array(values?)
        }
        YamlValue::Mapping(obj) => {
            let mut dict = IndexMap::new();
            for (k, v) in obj {
                let key = match k {
                    YamlValue::String(s) => s,
                    other => yaml_to_value(other)?.display(),
                };
                dict.insert(key, yaml_to_value(v)?);
            }
            Value::Dict(dict)
        }
        YamlValue::Tagged(tagged) => yaml_to_value(tagged.value)?,
    })
}

#[cfg(feature = "data-loading")]
fn toml_to_value(toml_val: toml::Value) -> EvalResult<Value> {
    use toml::Value as TomlValue;
    Ok(match toml_val {
        TomlValue::String(s) => Value::Str(s),
        TomlValue::Integer(i) => Value::Int(i),
        TomlValue::Float(f) => Value::Float(f),
        TomlValue::Boolean(b) => Value::Bool(b),
        TomlValue::Datetime(dt) => Value::Str(dt.to_string()),
        TomlValue::Array(arr) => {
            let values: Result<Vec<_>, _> = arr.into_iter().map(toml_to_value).collect();
            Value::Array(values?)
        }
        TomlValue::Table(obj) => {
            let mut dict = IndexMap::new();
            for (k, v) in obj {
                dict.insert(k, toml_to_value(v)?);
            }
            Value::Dict(dict)
        }
    })
}

// ============================================================================
// Fallback simple parsers (no external dependencies)
// ============================================================================

/// Simple JSON parser for basic cases (no external dependencies).
#[cfg(not(feature = "data-loading"))]
fn parse_json_simple(input: &str) -> EvalResult<Value> {
    let input = input.trim();

    if input == "null" {
        return Ok(Value::None);
    }
    if input == "true" {
        return Ok(Value::Bool(true));
    }
    if input == "false" {
        return Ok(Value::Bool(false));
    }

    // Try parsing as number
    if let Ok(i) = input.parse::<i64>() {
        return Ok(Value::Int(i));
    }
    if let Ok(f) = input.parse::<f64>() {
        return Ok(Value::Float(f));
    }

    // Try parsing as string
    if input.starts_with('"') && input.ends_with('"') {
        let s = &input[1..input.len() - 1];
        // Basic unescape
        let s = s
            .replace("\\\"", "\"")
            .replace("\\n", "\n")
            .replace("\\t", "\t");
        return Ok(Value::Str(s));
    }

    // Try parsing as array
    if input.starts_with('[') && input.ends_with(']') {
        let inner = &input[1..input.len() - 1].trim();
        if inner.is_empty() {
            return Ok(Value::Array(Vec::new()));
        }
        // Simple comma splitting (doesn't handle nested structures well)
        let parts = split_json_elements(inner);
        let values: Result<Vec<_>, _> = parts.iter().map(|s| parse_json_simple(s)).collect();
        return Ok(Value::Array(values?));
    }

    // Try parsing as object
    if input.starts_with('{') && input.ends_with('}') {
        let inner = &input[1..input.len() - 1].trim();
        if inner.is_empty() {
            return Ok(Value::Dict(IndexMap::new()));
        }
        let mut dict = IndexMap::new();
        let pairs = split_json_elements(inner);
        for pair in pairs {
            let parts: Vec<&str> = pair.splitn(2, ':').collect();
            if parts.len() != 2 {
                return Err(EvalError::other(format!("Invalid JSON pair: {}", pair)));
            }
            let key = parts[0].trim();
            let value = parts[1].trim();
            // Parse key (remove quotes)
            let key = if key.starts_with('"') && key.ends_with('"') {
                key[1..key.len() - 1].to_string()
            } else {
                key.to_string()
            };
            dict.insert(key, parse_json_simple(value)?);
        }
        return Ok(Value::Dict(dict));
    }

    Err(EvalError::other(format!("Cannot parse JSON: {}", input)))
}

/// Split JSON elements by comma, respecting nesting.
#[cfg(not(feature = "data-loading"))]
fn split_json_elements(input: &str) -> Vec<String> {
    let mut elements = Vec::new();
    let mut current = String::new();
    let mut depth = 0;
    let mut in_string = false;
    let mut escape = false;

    for c in input.chars() {
        if escape {
            current.push(c);
            escape = false;
            continue;
        }

        match c {
            '\\' if in_string => {
                current.push(c);
                escape = true;
            }
            '"' => {
                current.push(c);
                in_string = !in_string;
            }
            '[' | '{' if !in_string => {
                current.push(c);
                depth += 1;
            }
            ']' | '}' if !in_string => {
                current.push(c);
                depth -= 1;
            }
            ',' if !in_string && depth == 0 => {
                elements.push(current.trim().to_string());
                current = String::new();
            }
            _ => current.push(c),
        }
    }

    if !current.trim().is_empty() {
        elements.push(current.trim().to_string());
    }

    elements
}

/// Simple CSV parser for basic cases (no external dependencies).
/// Used as fallback when `data-loading` feature is not enabled.
#[cfg(not(feature = "data-loading"))]
fn parse_csv_simple(input: &str, has_header: bool) -> EvalResult<Value> {
    let lines: Vec<&str> = input.lines().collect();
    if lines.is_empty() {
        return Ok(Value::Array(Vec::new()));
    }

    let parse_row = |line: &str| -> Vec<String> {
        // Simple CSV parsing (doesn't handle quoted fields with commas)
        line.split(',').map(|s| s.trim().to_string()).collect()
    };

    if has_header {
        let headers = parse_row(lines[0]);
        let mut rows = Vec::new();
        for line in lines.iter().skip(1) {
            if line.is_empty() {
                continue;
            }
            let values = parse_row(line);
            let mut row = IndexMap::new();
            for (i, value) in values.into_iter().enumerate() {
                let key = headers.get(i).cloned().unwrap_or_else(|| i.to_string());
                row.insert(key, Value::Str(value));
            }
            rows.push(Value::Dict(row));
        }
        Ok(Value::Array(rows))
    } else {
        let mut rows = Vec::new();
        for line in lines {
            if line.is_empty() {
                continue;
            }
            let values: Vec<Value> = parse_row(line).into_iter().map(Value::Str).collect();
            rows.push(Value::Array(values));
        }
        Ok(Value::Array(rows))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_json_simple_primitives() {
        // Use public parse_json API which works with or without data-loading feature
        assert_eq!(parse_json("null").unwrap(), Value::None);
        assert_eq!(parse_json("true").unwrap(), Value::Bool(true));
        assert_eq!(parse_json("false").unwrap(), Value::Bool(false));
        assert_eq!(parse_json("42").unwrap(), Value::Int(42));
        assert_eq!(parse_json("3.14").unwrap(), Value::Float(3.14));
        assert_eq!(
            parse_json("\"hello\"").unwrap(),
            Value::Str("hello".to_string())
        );
    }

    #[test]
    fn test_parse_json_simple_array() {
        let result = parse_json("[1, 2, 3]").unwrap();
        if let Value::Array(arr) = result {
            assert_eq!(arr.len(), 3);
            assert_eq!(arr[0], Value::Int(1));
            assert_eq!(arr[1], Value::Int(2));
            assert_eq!(arr[2], Value::Int(3));
        } else {
            panic!("Expected array");
        }
    }

    #[test]
    fn test_parse_json_simple_object() {
        let result = parse_json("{\"name\": \"test\", \"value\": 42}").unwrap();
        if let Value::Dict(dict) = result {
            assert_eq!(dict.get("name"), Some(&Value::Str("test".to_string())));
            assert_eq!(dict.get("value"), Some(&Value::Int(42)));
        } else {
            panic!("Expected dict");
        }
    }

    #[test]
    fn test_parse_csv_simple() {
        let csv = "name,age\nAlice,30\nBob,25";
        let result = parse_csv(csv, true).unwrap();
        if let Value::Array(rows) = result {
            assert_eq!(rows.len(), 2);
            if let Value::Dict(ref row) = rows[0] {
                assert_eq!(row.get("name"), Some(&Value::Str("Alice".to_string())));
                assert_eq!(row.get("age"), Some(&Value::Str("30".to_string())));
            }
        } else {
            panic!("Expected array");
        }
    }
}