noml 0.9.0

High-performance dynamic configuration language with format preservation, environment variables, native types, string interpolation, and TOML compatibility. Blazing-fast parsing at 25µs with zero-copy architecture.
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! # Format-Preserving NOML Serializer
//!
//! This module provides serialization capabilities that preserve the original
//! formatting, comments, whitespace, and style of NOML documents. This enables
//! perfect round-trip editing while maintaining the exact appearance of the
//! original file.

use crate::error::Result;
use crate::parser::ast::{
    AstNode, AstValue, Comment, Document, FormatMetadata, FormatStyle, Indentation, Key,
    LineEnding, StringStyle, TableEntry,
};
use std::fmt::Write;

/// A format-preserving serializer for NOML documents
pub struct Serializer {
    /// Output buffer
    output: String,
    /// Current indentation level
    indent_level: usize,
    /// Default indentation configuration
    indentation: Indentation,
    /// Default line ending style
    line_ending: LineEnding,
}

impl Serializer {
    /// Create a new serializer with default formatting
    pub fn new() -> Self {
        Self {
            output: String::new(),
            indent_level: 0,
            indentation: Indentation::default(),
            line_ending: LineEnding::default(),
        }
    }

    /// Create a serializer with custom formatting options
    pub fn with_options(indentation: Indentation, line_ending: LineEnding) -> Self {
        Self {
            output: String::new(),
            indent_level: 0,
            indentation,
            line_ending,
        }
    }

    /// Serialize a document to a string, preserving all formatting
    pub fn serialize_document(&mut self, document: &Document) -> Result<String> {
        self.output.clear();

        // Add leading whitespace from root format metadata
        self.output
            .push_str(&document.root.format.leading_whitespace);

        // Serialize the root node (typically a table)
        self.serialize_ast_node(&document.root)?;

        // Add trailing whitespace from root format metadata
        self.output
            .push_str(&document.root.format.trailing_whitespace);

        Ok(self.output.clone())
    }

    /// Serialize a single table entry with formatting preservation
    fn serialize_table_entry(&mut self, entry: &TableEntry) -> Result<()> {
        // Add leading whitespace from format metadata
        self.output.push_str(&entry.value.format.leading_whitespace);

        // Add comments before the entry
        for comment in &entry.comments.before {
            self.serialize_comment(comment);
            self.add_line_ending();
        }

        // Serialize the key
        self.serialize_key(&entry.key);

        // Add equals sign with proper spacing
        if let FormatStyle::KeyValue { equals_spacing, .. } = &entry.value.format.format_style {
            self.output.push_str(&equals_spacing.before);
            self.output.push('=');
            self.output.push_str(&equals_spacing.after);
        } else {
            self.output.push_str(" = ");
        }

        // Serialize the value
        self.serialize_ast_node(&entry.value)?;

        // Add inline comment if present
        if let Some(ref comment) = entry.comments.inline {
            self.output.push(' ');
            self.serialize_comment(comment);
        }

        // Add line ending
        self.add_line_ending();

        // Add comments after the entry
        for comment in &entry.comments.after {
            self.serialize_comment(comment);
            self.add_line_ending();
        }

        // Add trailing whitespace
        self.output
            .push_str(&entry.value.format.trailing_whitespace);

        Ok(())
    }

    /// Serialize a key with proper quoting and formatting
    fn serialize_key(&mut self, key: &Key) {
        for (i, segment) in key.segments.iter().enumerate() {
            if i > 0 {
                self.output.push('.');
            }

            if segment.quoted {
                // Use the original quote style if available
                let quote_char = match segment.quote_style {
                    Some(StringStyle::Double) => '"',
                    Some(StringStyle::Single) => '\'',
                    _ => '"', // Default to double quotes
                };
                self.output.push(quote_char);
                self.output.push_str(&segment.name);
                self.output.push(quote_char);
            } else {
                self.output.push_str(&segment.name);
            }
        }
    }

    /// Serialize an AST node with full formatting preservation
    fn serialize_ast_node(&mut self, node: &AstNode) -> Result<()> {
        match &node.value {
            AstValue::Null => self.output.push_str("null"),
            AstValue::Bool(b) => self.output.push_str(&b.to_string()),
            AstValue::Integer { raw, .. } => self.output.push_str(raw),
            AstValue::Float { raw, .. } => self.output.push_str(raw),
            AstValue::String {
                value,
                style,
                has_escapes,
            } => {
                self.serialize_string(value, style, *has_escapes);
            }
            AstValue::Array { elements, .. } => {
                self.serialize_array(elements, &node.format)?;
            }
            AstValue::Table { entries, inline } => {
                if *inline {
                    self.serialize_inline_table(entries)?;
                } else {
                    self.serialize_table(entries)?;
                }
            }
            AstValue::FunctionCall { name, args } => {
                self.serialize_function_call(name, args)?;
            }
            AstValue::Interpolation { path } => {
                write!(self.output, "${{{path}}}").map_err(|e| {
                    crate::error::NomlError::validation(format!(
                        "Failed to write interpolation: {e}"
                    ))
                })?;
            }
            AstValue::Include { path } => {
                write!(self.output, "include \"{path}\"").map_err(|e| {
                    crate::error::NomlError::validation(format!("Failed to write include: {e}"))
                })?;
            }
            AstValue::Native { type_name, args } => {
                write!(self.output, "@{type_name}(").map_err(|e| {
                    crate::error::NomlError::validation(format!("Failed to write native type: {e}"))
                })?;
                for (i, arg) in args.iter().enumerate() {
                    if i > 0 {
                        self.output.push_str(", ");
                    }
                    self.serialize_ast_node(arg)?;
                }
                self.output.push(')');
            }
        }
        Ok(())
    }

    /// Serialize a string value with proper quoting and escaping
    fn serialize_string(&mut self, value: &str, style: &StringStyle, has_escapes: bool) {
        match style {
            StringStyle::Double => {
                self.output.push('"');
                if has_escapes {
                    self.escape_string(value, '"');
                } else {
                    self.output.push_str(value);
                }
                self.output.push('"');
            }
            StringStyle::Single => {
                self.output.push('\'');
                if has_escapes {
                    self.escape_string(value, '\'');
                } else {
                    self.output.push_str(value);
                }
                self.output.push('\'');
            }
            StringStyle::TripleDouble => {
                self.output.push_str("\"\"\"");
                self.output.push_str(value);
                self.output.push_str("\"\"\"");
            }
            StringStyle::TripleSingle => {
                self.output.push_str("'''");
                self.output.push_str(value);
                self.output.push_str("'''");
            }
            StringStyle::Raw { hashes } => {
                self.output.push('r');
                for _ in 0..*hashes {
                    self.output.push('#');
                }
                self.output.push('"');
                self.output.push_str(value);
                self.output.push('"');
                for _ in 0..*hashes {
                    self.output.push('#');
                }
            }
        }
    }

    /// Escape a string value for serialization
    fn escape_string(&mut self, value: &str, quote_char: char) {
        for ch in value.chars() {
            match ch {
                '\n' => self.output.push_str("\\n"),
                '\t' => self.output.push_str("\\t"),
                '\r' => self.output.push_str("\\r"),
                '\\' => self.output.push_str("\\\\"),
                '"' if quote_char == '"' => self.output.push_str("\\\""),
                '\'' if quote_char == '\'' => self.output.push_str("\\'"),
                c => self.output.push(c),
            }
        }
    }

    /// Serialize an array with formatting preservation
    fn serialize_array(&mut self, elements: &[AstNode], format: &FormatMetadata) -> Result<()> {
        self.output.push('[');

        if let FormatStyle::Array {
            multiline,
            trailing_comma,
            bracket_spacing,
        } = &format.format_style
        {
            self.output.push_str(&bracket_spacing.after_open);

            if *multiline {
                // Multi-line array format
                self.add_line_ending();
                self.indent_level += 1;

                for (i, element) in elements.iter().enumerate() {
                    self.add_indentation();
                    self.serialize_ast_node(element)?;

                    if i < elements.len() - 1 || *trailing_comma {
                        self.output.push(',');
                    }

                    self.add_line_ending();
                }

                self.indent_level -= 1;
                self.add_indentation();
            } else {
                // Single-line array format
                for (i, element) in elements.iter().enumerate() {
                    if i > 0 {
                        self.output.push_str(", ");
                    }
                    self.serialize_ast_node(element)?;
                }

                if *trailing_comma && !elements.is_empty() {
                    self.output.push(',');
                }
            }

            self.output.push_str(&bracket_spacing.before_close);
        } else {
            // Default array formatting
            for (i, element) in elements.iter().enumerate() {
                if i > 0 {
                    self.output.push_str(", ");
                }
                self.serialize_ast_node(element)?;
            }
        }

        self.output.push(']');
        Ok(())
    }

    /// Serialize an inline table
    fn serialize_inline_table(&mut self, entries: &[TableEntry]) -> Result<()> {
        self.output.push_str("{ ");

        for (i, entry) in entries.iter().enumerate() {
            if i > 0 {
                self.output.push_str(", ");
            }

            self.serialize_key(&entry.key);
            self.output.push_str(" = ");
            self.serialize_ast_node(&entry.value)?;
        }

        self.output.push_str(" }");
        Ok(())
    }

    /// Serialize a regular table (not used for inline tables)
    fn serialize_table(&mut self, entries: &[TableEntry]) -> Result<()> {
        for entry in entries {
            self.serialize_table_entry(entry)?;
        }
        Ok(())
    }

    /// Serialize a function call
    fn serialize_function_call(&mut self, name: &str, args: &[AstNode]) -> Result<()> {
        write!(self.output, "{name}(").map_err(|e| {
            crate::error::NomlError::validation(format!("Failed to write function call: {e}"))
        })?;

        for (i, arg) in args.iter().enumerate() {
            if i > 0 {
                self.output.push_str(", ");
            }
            self.serialize_ast_node(arg)?;
        }

        self.output.push(')');
        Ok(())
    }

    /// Serialize a comment
    fn serialize_comment(&mut self, comment: &Comment) {
        self.output.push('#');
        if !comment.text.is_empty() {
            self.output.push(' ');
            self.output.push_str(&comment.text);
        }
    }

    /// Add appropriate line ending
    fn add_line_ending(&mut self) {
        match self.line_ending {
            LineEnding::Unix => self.output.push('\n'),
            LineEnding::Windows => self.output.push_str("\r\n"),
            LineEnding::Mac => self.output.push('\r'),
        }
    }

    /// Add indentation for the current level
    fn add_indentation(&mut self) {
        let indent_str = if self.indentation.use_tabs {
            "\t".repeat(self.indent_level)
        } else {
            " ".repeat(self.indent_level * self.indentation.size)
        };
        self.output.push_str(&indent_str);
    }
}

impl Default for Serializer {
    fn default() -> Self {
        Self::new()
    }
}

/// High-level function to serialize a document with format preservation
pub fn serialize_document(document: &Document) -> Result<String> {
    let mut serializer = Serializer::new();
    serializer.serialize_document(document)
}

/// High-level function to serialize a document with custom formatting
pub fn serialize_document_with_options(
    document: &Document,
    indentation: Indentation,
    line_ending: LineEnding,
) -> Result<String> {
    let mut serializer = Serializer::with_options(indentation, line_ending);
    serializer.serialize_document(document)
}

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

    #[test]
    fn test_serialize_simple_values() {
        let mut serializer = Serializer::new();

        // Test null
        let null_node = AstNode::new(AstValue::Null, Span::default());
        serializer.serialize_ast_node(&null_node).unwrap();
        assert_eq!(serializer.output, "null");

        serializer.output.clear();

        // Test boolean
        let bool_node = AstNode::new(AstValue::Bool(true), Span::default());
        serializer.serialize_ast_node(&bool_node).unwrap();
        assert_eq!(serializer.output, "true");
    }

    #[test]
    fn test_serialize_string_with_escapes() {
        let mut serializer = Serializer::new();

        let string_node = AstNode::new(
            AstValue::String {
                value: "hello\nworld".to_string(),
                style: StringStyle::Double,
                has_escapes: true,
            },
            Span::default(),
        );

        serializer.serialize_ast_node(&string_node).unwrap();
        assert_eq!(serializer.output, "\"hello\\nworld\"");
    }

    #[test]
    fn test_serialize_raw_string() {
        let mut serializer = Serializer::new();

        let string_node = AstNode::new(
            AstValue::String {
                value: "no\\escapes".to_string(),
                style: StringStyle::Raw { hashes: 0 },
                has_escapes: false,
            },
            Span::default(),
        );

        serializer.serialize_ast_node(&string_node).unwrap();
        assert_eq!(serializer.output, "r\"no\\escapes\"");
    }
}