eure-document 0.1.8

Value type for Eure
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
//! RecordWriter for writing record types to Eure documents.

extern crate alloc;

use alloc::string::ToString;

use crate::document::constructor::DocumentConstructor;
use crate::path::PathSegment;
use crate::value::ObjectKey;

use super::{IntoEure, WriteError};

/// Helper for writing record (map with string keys) to Eure documents.
///
/// Used within the closure passed to [`DocumentConstructor::record`].
///
/// When `ext_mode` is `true`, field writes are redirected to extension writes.
/// This is used by `flatten_ext` to write fields as extensions.
///
/// # Example
///
/// ```ignore
/// c.record(|rec| {
///     rec.field("name", "Alice")?;
///     rec.field_optional("age", Some(30))?;
///     Ok(())
/// })?;
/// ```
pub struct RecordWriter<'a> {
    constructor: &'a mut DocumentConstructor,
    ext_mode: bool,
}

impl<'a> RecordWriter<'a> {
    /// Create a new RecordWriter with `ext_mode` disabled.
    pub(crate) fn new(constructor: &'a mut DocumentConstructor) -> Self {
        Self {
            constructor,
            ext_mode: false,
        }
    }

    /// Create a new RecordWriter with the specified `ext_mode`.
    ///
    /// This is primarily used by generated derive code that needs to write
    /// flattened extension fields onto an existing node.
    pub fn new_with_ext_mode(constructor: &'a mut DocumentConstructor, ext_mode: bool) -> Self {
        Self {
            constructor,
            ext_mode,
        }
    }

    /// Write a required field.
    ///
    /// In `ext_mode`, this redirects to an extension write.
    ///
    /// # Example
    ///
    /// ```ignore
    /// rec.field("name", "Alice")?;
    /// ```
    pub fn field<T: IntoEure>(&mut self, name: &str, value: T) -> Result<(), T::Error> {
        if self.ext_mode {
            return self.constructor.set_extension(name, value);
        }
        let scope = self.constructor.begin_scope();
        self.constructor
            .navigate(PathSegment::Value(ObjectKey::String(name.to_string())))
            .map_err(WriteError::from)?;
        T::write(value, self.constructor)?;
        self.constructor
            .end_scope(scope)
            .map_err(WriteError::from)?;
        Ok(())
    }

    /// Write a required field using a marker type.
    ///
    /// This enables writing types from external crates that can't implement
    /// `IntoEure` directly due to Rust's orphan rule.
    ///
    /// In `ext_mode`, this redirects to an extension write via the marker type.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // DurationDef implements IntoEure<std::time::Duration>
    /// rec.field_via::<DurationDef, _>("timeout", duration)?;
    /// ```
    pub fn field_via<M, T>(&mut self, name: &str, value: T) -> Result<(), M::Error>
    where
        M: IntoEure<T>,
    {
        if self.ext_mode {
            let ident: crate::identifier::Identifier = name
                .parse()
                .map_err(|_| WriteError::InvalidIdentifier(name.into()))?;
            let scope = self.constructor.begin_scope();
            self.constructor
                .navigate(PathSegment::Extension(ident))
                .map_err(WriteError::from)?;
            M::write(value, self.constructor)?;
            self.constructor
                .end_scope(scope)
                .map_err(WriteError::from)?;
            return Ok(());
        }
        let scope = self.constructor.begin_scope();
        self.constructor
            .navigate(PathSegment::Value(ObjectKey::String(name.to_string())))
            .map_err(WriteError::from)?;
        M::write(value, self.constructor)?;
        self.constructor
            .end_scope(scope)
            .map_err(WriteError::from)?;
        Ok(())
    }

    /// Write an optional field.
    /// Does nothing if the value is `None`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// rec.field_optional("age", self.age)?;
    /// ```
    pub fn field_optional<T: IntoEure>(
        &mut self,
        name: &str,
        value: Option<T>,
    ) -> Result<(), T::Error> {
        if let Some(v) = value {
            self.field(name, v)?;
        }
        Ok(())
    }

    /// Write a field using a custom writer closure.
    ///
    /// Useful for nested structures that need custom handling.
    ///
    /// # Example
    ///
    /// ```ignore
    /// rec.field_with("address", |c| {
    ///     c.record(|rec| {
    ///         rec.field("city", "Tokyo")?;
    ///         Ok(())
    ///     })
    /// })?;
    /// ```
    pub fn field_with<F, T>(&mut self, name: &str, f: F) -> Result<T, WriteError>
    where
        F: FnOnce(&mut DocumentConstructor) -> Result<T, WriteError>,
    {
        let scope = self.constructor.begin_scope();
        self.constructor
            .navigate(PathSegment::Value(ObjectKey::String(name.to_string())))
            .map_err(WriteError::from)?;
        let result = f(self.constructor)?;
        self.constructor
            .end_scope(scope)
            .map_err(WriteError::from)?;
        Ok(result)
    }

    /// Write an optional field using a custom writer closure.
    /// Does nothing if the value is `None`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// rec.field_with_optional("metadata", self.metadata.as_ref(), |c, meta| {
    ///     c.write(meta)
    /// })?;
    /// ```
    pub fn field_with_optional<T, F, R>(
        &mut self,
        name: &str,
        value: Option<T>,
        f: F,
    ) -> Result<Option<R>, WriteError>
    where
        F: FnOnce(&mut DocumentConstructor, T) -> Result<R, WriteError>,
    {
        if let Some(v) = value {
            let result = self.field_with(name, |c| f(c, v))?;
            Ok(Some(result))
        } else {
            Ok(None)
        }
    }

    /// Flatten a value's fields into this record writer.
    ///
    /// The flattened type's fields are written as if they were direct fields
    /// of this record. The current `ext_mode` is inherited.
    ///
    /// # Example
    ///
    /// ```ignore
    /// rec.flatten(value.address)?;
    /// ```
    pub fn flatten<M, T>(&mut self, value: T) -> Result<(), M::Error>
    where
        M: IntoEure<T>,
    {
        M::write_flatten(value, self)
    }

    /// Flatten a value's fields as extensions into this record.
    ///
    /// Creates a temporary `RecordWriter` with `ext_mode: true`, so that
    /// all field writes from the flattened type become extension writes.
    ///
    /// # Example
    ///
    /// ```ignore
    /// rec.flatten_ext(value.ext)?;
    /// ```
    pub fn flatten_ext<M, T>(&mut self, value: T) -> Result<(), M::Error>
    where
        M: IntoEure<T>,
    {
        let mut ext_rec = RecordWriter::new_with_ext_mode(self.constructor, true);
        M::write_flatten(value, &mut ext_rec)
    }

    /// Get a mutable reference to the underlying DocumentConstructor.
    ///
    /// Useful for advanced use cases that need direct access.
    pub fn constructor(&mut self) -> &mut DocumentConstructor {
        self.constructor
    }
}

#[cfg(test)]
mod tests {
    extern crate alloc;
    use alloc::string::{String, ToString};

    use super::*;
    use crate::document::node::NodeValue;
    use crate::text::Text;
    use crate::value::PrimitiveValue;

    #[test]
    fn test_field() {
        let mut c = DocumentConstructor::new();
        c.record(|rec| {
            rec.field("name", "Alice")?;
            Ok::<(), WriteError>(())
        })
        .unwrap();
        let doc = c.finish();
        let map = doc.root().as_map().unwrap();
        let name_id = map.get(&ObjectKey::String("name".to_string())).unwrap();
        let node = doc.node(*name_id);
        assert_eq!(
            node.content,
            NodeValue::Primitive(PrimitiveValue::Text(Text::plaintext("Alice")))
        );
    }

    #[test]
    fn test_field_optional_some() {
        let mut c = DocumentConstructor::new();
        c.record(|rec| {
            rec.field_optional("age", Some(30i32))?;
            Ok::<(), WriteError>(())
        })
        .unwrap();
        let doc = c.finish();
        let map = doc.root().as_map().unwrap();
        assert!(map.get(&ObjectKey::String("age".to_string())).is_some());
    }

    #[test]
    fn test_field_optional_none() {
        let mut c = DocumentConstructor::new();
        c.record(|rec| {
            rec.field_optional::<i32>("age", None)?;
            Ok::<(), WriteError>(())
        })
        .unwrap();
        let doc = c.finish();
        let map = doc.root().as_map().unwrap();
        assert!(map.get(&ObjectKey::String("age".to_string())).is_none());
    }

    #[test]
    fn test_field_with() {
        let mut c = DocumentConstructor::new();
        c.record(|rec| {
            rec.field_with("nested", |c| {
                c.record(|rec| {
                    rec.field("inner", "value")?;
                    Ok::<(), WriteError>(())
                })
            })?;
            Ok::<(), WriteError>(())
        })
        .unwrap();
        let doc = c.finish();
        let map = doc.root().as_map().unwrap();
        let nested_id = map.get(&ObjectKey::String("nested".to_string())).unwrap();
        let nested = doc.node(*nested_id).as_map().unwrap();
        assert!(
            nested
                .get(&ObjectKey::String("inner".to_string()))
                .is_some()
        );
    }

    #[test]
    fn test_multiple_fields() {
        let mut c = DocumentConstructor::new();
        c.record(|rec| {
            rec.field("name", "Bob")?;
            rec.field("age", 25i32)?;
            rec.field("active", true)?;
            Ok::<(), WriteError>(())
        })
        .unwrap();
        let doc = c.finish();
        let map = doc.root().as_map().unwrap();
        assert_eq!(map.len(), 3);
    }

    // Manually implement IntoEure flatten support for testing
    struct TestAddress {
        city: String,
        country: String,
    }

    impl IntoEure for TestAddress {
        type Error = WriteError;

        fn write(value: TestAddress, c: &mut DocumentConstructor) -> Result<(), Self::Error> {
            c.record(|rec| {
                rec.field("city", value.city)?;
                rec.field("country", value.country)?;
                Ok::<(), WriteError>(())
            })
        }

        fn write_flatten(
            value: TestAddress,
            rec: &mut super::RecordWriter<'_>,
        ) -> Result<(), Self::Error> {
            rec.field("city", value.city)?;
            rec.field("country", value.country)?;
            Ok(())
        }
    }

    #[test]
    fn test_flatten() {
        let mut c = DocumentConstructor::new();
        c.record(|rec| {
            rec.field("name", "Alice")?;
            rec.flatten::<TestAddress, _>(TestAddress {
                city: "Tokyo".to_string(),
                country: "Japan".to_string(),
            })?;
            Ok::<(), WriteError>(())
        })
        .unwrap();
        let doc = c.finish();
        let map = doc.root().as_map().unwrap();
        assert_eq!(map.len(), 3);
        assert!(map.get(&ObjectKey::String("name".to_string())).is_some());
        assert!(map.get(&ObjectKey::String("city".to_string())).is_some());
        assert!(map.get(&ObjectKey::String("country".to_string())).is_some());
    }

    struct TestMeta {
        version: i32,
        deprecated: bool,
    }

    impl IntoEure for TestMeta {
        type Error = WriteError;

        fn write(value: TestMeta, c: &mut DocumentConstructor) -> Result<(), Self::Error> {
            c.record(|rec| {
                rec.field("version", value.version)?;
                rec.field("deprecated", value.deprecated)?;
                Ok::<(), WriteError>(())
            })
        }

        fn write_flatten(
            value: TestMeta,
            rec: &mut super::RecordWriter<'_>,
        ) -> Result<(), Self::Error> {
            rec.field("version", value.version)?;
            rec.field("deprecated", value.deprecated)?;
            Ok(())
        }
    }

    #[test]
    fn test_flatten_ext() {
        use crate::identifier::Identifier;

        let mut c = DocumentConstructor::new();
        c.record(|rec| {
            rec.field("name", "test")?;
            rec.flatten_ext::<TestMeta, _>(TestMeta {
                version: 2,
                deprecated: true,
            })?;
            Ok::<(), WriteError>(())
        })
        .unwrap();
        let doc = c.finish();
        let root = doc.root();
        // "name" should be a record field
        let map = root.as_map().unwrap();
        assert_eq!(map.len(), 1);
        assert!(map.get(&ObjectKey::String("name".to_string())).is_some());
        // "version" and "deprecated" should be extensions
        assert!(
            root.extensions
                .contains_key(&"version".parse::<Identifier>().unwrap())
        );
        assert!(
            root.extensions
                .contains_key(&"deprecated".parse::<Identifier>().unwrap())
        );
    }
}