nickel-lang-parser 0.3.0

The Nickel parser
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//! A builder interface for producing Nickel values.
//!
//! Using this module, you can define Nickel values using a builder style. For example:
//!
//! ```rust
//! # use nickel_lang_parser::ast::{Ast, MergePriority, Node, builder::Record, AstAlloc};
//!
//! let alloc = AstAlloc::new();
//!
//! let record = Record::new()
//!     .field("foo")
//!     .priority(MergePriority::Bottom)
//!     .doc("foo?")
//!     .not_exported(true)
//!     .value(&alloc, alloc.string("foo"));
//!
//! let record = record
//!     .field("bar")
//!     .value(&alloc, alloc.number(42.into()))
//!     .build(&alloc);
//!
//! // Gives {foo | doc "foo?" | not_exported | default = "foo", bar = 42}
//! ```
//!
//! This modules also offers a simpler interface for basic values where all arguments can be
//! provided at once, to avoid useless intermediate allocations.
use crate::identifier::Ident;

use super::{
    MergePriority,
    record::{FieldMetadata, FieldPathElem, Include},
    typ::Type,
    *,
};

type StaticPath = Vec<Ident>;

/// Typestate style tag for `Field`s that are not yet completely specified
pub struct Incomplete();

/// Typestate style tag for `Field`s that have been finalized
pub struct Complete<'ast>(Option<Ast<'ast>>);

/// A Nickel record field being constructed
#[derive(Debug)]
pub struct Field<'ast, State> {
    /// This field stores different piece of information depending on the state of this field:
    ///
    /// - It's empty for an incomplete field (`()`).
    /// - When a value (or `None`) has been set, `State` becomes [Complete] and stores the
    ///   optional value of this field.
    /// - Internally, [Self] is also used temporarily with `State` set to `Record<'ast>>` inside
    ///   [Self::attach], before updating a record with a completed field (and thus consuming said
    ///   field). During this transitory phase, `state` stores the parent record.
    state: State,
    path: StaticPath,
    metadata: FieldMetadata<'ast>,
    /// We need to store the documentation separately, because the metadata only accepts a string
    /// that has been allocated in the AST allocator. We could require to thread the allocator for
    /// calls to [Self::doc] or [Self::some_doc], but it's more ergonomic to keep the phases of
    /// building a field and finalizing it separate. We thus store the documentation here
    /// temporarily.
    doc: Option<String>,
    /// As we might build contract element by element, we can't rely on
    /// `metadata.annotation.contracts`, which is a fixed array.
    ///
    /// We accumulate them in this field instead and keep `metadata.annotation.contracts` empty
    /// until finalization, where we finally allocate it.
    contracts: Vec<Type<'ast>>,
}

impl<'ast, A> Field<'ast, A> {
    /// Attach documentation metadata to the field
    pub fn doc(self, doc: impl AsRef<str>) -> Self {
        self.some_doc(Some(doc))
    }

    /// Attach documentation metadata to the field, optionally
    pub fn some_doc(mut self, some_doc: Option<impl AsRef<str>>) -> Self {
        self.doc = some_doc.map(|s| s.as_ref().to_string());
        self
    }

    /// Mark the field as optional or not, depending on `opt`
    pub fn optional(mut self, opt: bool) -> Self {
        self.metadata.opt = opt;
        self
    }

    /// Mark the field as `not_exported` or not, depending on the argument
    pub fn not_exported(mut self, not_exported: bool) -> Self {
        self.metadata.not_exported = not_exported;
        self
    }

    /// Attach a contract to the field
    pub fn contract(mut self, contract: impl Into<Type<'ast>>) -> Self {
        self.contracts.push(contract.into());
        self
    }

    /// Attach multiple contracts to the field
    pub fn contracts<I>(mut self, contracts: I) -> Self
    where
        I: IntoIterator<Item = Type<'ast>>,
    {
        self.contracts.extend(contracts);
        self
    }

    /// Attach a type annotation to the field
    pub fn types(mut self, typ: impl Into<Type<'ast>>) -> Self {
        self.metadata.annotation.typ = Some(typ.into());
        self
    }

    /// Set the field's merge priority
    pub fn priority(mut self, priority: MergePriority) -> Self {
        self.metadata.priority = priority;
        self
    }

    /// Set the field's metadata all at once
    pub fn metadata(mut self, metadata: FieldMetadata<'ast>) -> Self {
        self.metadata = metadata;
        self
    }
}

impl<'ast> Field<'ast, Incomplete> {
    /// Construct an incomplete [`Field`] at a given path
    pub fn path<I, It>(path: It) -> Self
    where
        I: AsRef<str>,
        It: IntoIterator<Item = I>,
    {
        Field {
            state: Incomplete(),
            path: path.into_iter().map(|e| e.as_ref().into()).collect(),
            metadata: Default::default(),
            doc: None,
            contracts: Vec::new(),
        }
    }

    /// Construct an incomplete [`Field`] with a given name
    pub fn name(name: impl AsRef<str>) -> Self {
        Self::path([name])
    }

    /// Finalize the [`Field`] without setting a value
    pub fn no_value(self) -> Field<'ast, Complete<'ast>> {
        Field {
            state: Complete(None),
            path: self.path,
            metadata: self.metadata,
            doc: self.doc,
            contracts: self.contracts,
        }
    }

    /// Finalize the [`Field`] by setting its value
    pub fn value(self, value: impl Into<Ast<'ast>>) -> Field<'ast, Complete<'ast>> {
        Field {
            state: Complete(Some(value.into())),
            path: self.path,
            metadata: self.metadata,
            doc: self.doc,
            contracts: self.contracts,
        }
    }
}

impl<'ast> Field<'ast, Complete<'ast>> {
    /// Attach a finalized [`Field`] to a [`Record`]
    pub fn attach(self, alloc: &'ast AstAlloc, record: Record<'ast>) -> Record<'ast> {
        let value = self.state;

        let field = Field {
            state: record,
            path: self.path,
            metadata: self.metadata,
            doc: self.doc,
            contracts: self.contracts,
        };
        match value {
            Complete(Some(v)) => field.value(alloc, v),
            Complete(None) => field.no_value(alloc),
        }
    }
}

impl<'ast> Field<'ast, Record<'ast>> {
    /// Finalize the [`Field`] without setting a value
    pub fn no_value(mut self, alloc: &'ast AstAlloc) -> Record<'ast> {
        self.finalize_contracts(alloc);
        self.metadata.doc = self.doc.map(|s| alloc.alloc_str(&s));

        self.state.field_defs.push(record::FieldDef {
            path: alloc.alloc_many(
                self.path
                    .into_iter()
                    .map(|id| FieldPathElem::Ident(id.into())),
            ),
            metadata: self.metadata,
            value: None,
            pos: TermPos::None,
        });
        self.state
    }

    /// Finalize the [`Field`] by setting its a value
    pub fn value(mut self, alloc: &'ast AstAlloc, value: impl Into<Ast<'ast>>) -> Record<'ast> {
        self.finalize_contracts(alloc);
        self.metadata.doc = self.doc.map(|s| alloc.alloc_str(&s));

        self.state.field_defs.push(record::FieldDef {
            path: alloc.alloc_many(
                self.path
                    .into_iter()
                    .map(|id| FieldPathElem::Ident(id.into())),
            ),
            metadata: self.metadata,
            value: Some(value.into()),
            pos: TermPos::None,
        });

        self.state
    }

    /// Finalize contracts by consuming the vector `self.contracts`, allocating a fixed array and
    /// filling the field `self.metadata.annotation.contracts` with it. After this call,
    /// `self.contracts` is empty and shouldn't be used anymore (it will have no effect).
    fn finalize_contracts(&mut self, alloc: &'ast AstAlloc) {
        self.metadata.annotation.contracts = alloc.alloc_many(self.contracts.drain(..));
    }
}

/// A Nickel record being constructed
#[derive(Debug, Default)]
pub struct Record<'ast> {
    field_defs: Vec<record::FieldDef<'ast>>,
    includes: Vec<Include<'ast>>,
    open: bool,
}

impl<'ast> Record<'ast> {
    /// Make a new, empty record builder
    pub fn new() -> Self {
        Record::default()
    }

    /// Start constructing a field with the given name
    pub fn field(self, name: impl AsRef<str>) -> Field<'ast, Record<'ast>> {
        Field {
            state: self,
            path: vec![Ident::new(name)],
            metadata: Default::default(),
            doc: None,
            contracts: Vec::new(),
        }
    }

    /// Attach possibly multiple fields to this record
    pub fn fields<I, It>(mut self, alloc: &'ast AstAlloc, fields: It) -> Self
    where
        I: Into<Field<'ast, Complete<'ast>>>,
        It: IntoIterator<Item = I>,
    {
        for f in fields {
            self = f.into().attach(alloc, self)
        }
        self
    }

    /// Adds an `include` expression (define a field by taking it from the outer environment).
    pub fn include(self, ident: LocIdent) -> Self {
        self.include_with_metadata(ident, Default::default())
    }

    /// Adds an `include` expression with associated metadata.
    pub fn include_with_metadata(mut self, ident: LocIdent, metadata: FieldMetadata<'ast>) -> Self {
        self.includes.push(Include { ident, metadata });
        self
    }

    /// Start constructing a field at the given path
    pub fn path<It, I>(self, path: It) -> Field<'ast, Record<'ast>>
    where
        I: AsRef<str>,
        It: IntoIterator<Item = I>,
    {
        Field {
            state: self,
            path: path.into_iter().map(|e| Ident::new(e)).collect(),
            metadata: Default::default(),
            doc: None,
            contracts: Vec::new(),
        }
    }

    /// Mark this record as "open" when used as a contract
    pub fn open(mut self) -> Self {
        self.open = true;
        self
    }

    /// Mark this record as "open" when used as a contract, depending on the value of `open`
    pub fn set_open(mut self, open: bool) -> Self {
        self.open = open;
        self
    }

    /// Finalize the record and turn it into a [`super::Ast`]
    pub fn build(self, alloc: &'ast AstAlloc) -> Ast<'ast> {
        alloc
            .record(record::Record {
                field_defs: alloc.alloc_many(self.field_defs),
                includes: alloc.alloc_many(self.includes),
                open: self.open,
            })
            .into()
    }

    /// Creates a record from an iterator of finalized fields.
    ///
    /// We can't implement `FromIterator` for `Record` because we need to provide an additional
    /// allocator.
    pub fn from_iterator<I, It>(alloc: &'ast AstAlloc, fields: It) -> Self
    where
        I: Into<Field<'ast, Complete<'ast>>>,
        It: IntoIterator<Item = I>,
    {
        Record::new().fields(alloc, fields)
    }
}

/// Multi-ary application for types implementing `Into<Ast>`.
#[macro_export]
macro_rules! app {
    // We avoid a vec allocation for unary applications, which are relatively common.
    ( $alloc:expr, $f:expr , $arg:expr $(,)?) => {
            $crate::ast::Ast::from(
                $alloc.app(
                    $crate::ast::Ast::from($f),
                    std::iter::once($crate::ast::Ast::from($arg))
                )
            )
    };
    ( $alloc:expr, $f:expr, $arg1:expr $(, $args:expr )+ $(,)?) => {
        {
            let args = vec![
                $crate::ast::Ast::from($arg1)
                $(, $crate::ast::Ast::from($args) )+
            ];

            $crate::ast::Ast::from($alloc.app($crate::ast::Ast::from($f), args))
        }
    };
}

#[macro_export]
/// Multi-ary application for types implementing `Into<NickelValue>`.
macro_rules! primop_app {
    // We avoid a vec allocation for unary primop applications, which are relatively common.
    ( $alloc:expr, $op:expr , $arg:expr $(,)?) => {
            $crate::ast::Ast::from(
                $alloc.prim_op(
                    $op,
                    std::iter::once($crate::ast::Ast::from($arg))
                )
            )
    };
    ( $alloc:expr, $op:expr, $arg1:expr $(, $args:expr )+ $(,)?) => {
        {
            let args = vec![
                $crate::ast::Ast::from($arg1)
                $(, $crate::ast::Ast::from($args) )+
            ];
            $crate::ast::Ast::from($alloc.prim_op($op, args))
        }
    };
}

#[macro_export]
/// Multi argument function for types implementing `Into<Ident>` (for the identifiers), and
/// `Into<NickelValue>` for the body.
macro_rules! fun {
    // Auxiliary form for `fun!` where the arguments are clearly delimited syntactically by
    // brackets. The issue with the general interface of `fun!` is that it must extract its last
    // argument, the body of the function, with an arbitrary number of macro arguments before it.
    // This isn't easy to do because the macro parser can't backtrack. The bracketed form uses
    // recursion and a separate syntax for arguments to make it work.
    //
    // For example, calling `fun!(alloc, args=[], arg1, arg2, body)` will expand to `fun!(alloc,
    // args=[arg1, arg2], body)`, which is the base case that can be turned into a function. The
    // bracket part is used to accumulate the arguments before the body.
    ($alloc:expr, args=[ $( $args:expr, )+ ], $body:expr) => {
        {
            let args = vec![
                $($crate::ast::pattern::Pattern::any($crate::identifier::LocIdent::from($args)), )+
            ];

            $crate::ast::Ast::from(
                $alloc.fun(args, $crate::ast::Ast::from($body))
            )
        }
    };
    // Recursive case for the bracketed form.
    ($alloc:expr, args=[ $( $args:expr, )* ], $next_arg:expr $(, $rest:expr )+) => {
        fun!($alloc, args=[ $( $args, )* $next_arg, ] $(, $rest )+)
    };
    // We avoid a vec allocation for unary functions, which are relatively common.
    ( $alloc:expr, $arg:expr, $body:expr $(,)?) => {
        $crate::ast::Ast::from(
            $alloc.fun(
                std::iter::once($crate::ast::pattern::Pattern::any($crate::identifier::LocIdent::from($arg))),
                $crate::ast::Ast::from($body)
            )
        )
    };
    ( $alloc:expr, $arg1:expr, $arg2:expr $(, $rest:expr )+ $(,)?) => {
        fun!($alloc, args=[ $arg1, $arg2, ] $(, $rest )+)
    };
}

pub fn var<'ast>(id: impl Into<LocIdent>) -> Ast<'ast> {
    Ast::from(Node::Var(id.into()))
}

pub fn enum_tag<'ast>(tag: impl Into<LocIdent>) -> Ast<'ast> {
    Ast::from(Node::EnumVariant {
        tag: tag.into(),
        arg: None,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use std::iter;

    fn build_simple_record<'ast, I, Id>(alloc: &'ast AstAlloc, fields: I, open: bool) -> Ast<'ast>
    where
        Id: Into<LocIdent>,
        I: IntoIterator<Item = (Id, Node<'ast>)>,
        I::IntoIter: ExactSizeIterator,
    {
        build_record(
            alloc,
            fields
                .into_iter()
                .map(|(id, node)| (id, Default::default(), Some(node))),
            open,
        )
    }

    fn build_record<'ast, I, Id>(alloc: &'ast AstAlloc, fields: I, open: bool) -> Ast<'ast>
    where
        Id: Into<LocIdent>,
        I: IntoIterator<Item = (Id, FieldMetadata<'ast>, Option<Node<'ast>>)>,
        I::IntoIter: ExactSizeIterator,
    {
        alloc
            .record(record::Record {
                field_defs: alloc.alloc_many(fields.into_iter().map(|(id, metadata, node)| {
                    record::FieldDef {
                        path: FieldPathElem::single_ident_path(alloc, id.into()),
                        value: node.map(Ast::from),
                        metadata,
                        pos: TermPos::None,
                    }
                })),
                includes: &[],
                open,
            })
            .into()
    }

    #[test]
    fn trivial() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::new()
            .field("foo")
            .value(&alloc, alloc.string("bar"))
            .build(&alloc);

        assert_eq!(
            ast,
            build_simple_record(&alloc, vec![("foo", alloc.string("bar"))], false)
        );
    }

    #[test]
    fn from_iter() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::from_iterator(
            &alloc,
            [
                Field::name("foo").value(Node::Null),
                Field::name("bar").value(Node::Null),
            ],
        )
        .build(&alloc);

        assert_eq!(
            ast,
            build_simple_record(
                &alloc,
                vec![("foo", Node::Null), ("bar", Node::Null),],
                false
            )
        );
    }

    #[test]
    fn some_doc() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::from_iterator(
            &alloc,
            [
                Field::name("foo").some_doc(Some("foo")).no_value(),
                Field::name("bar").some_doc(None as Option<&str>).no_value(),
                Field::name("baz").doc("baz").no_value(),
            ],
        )
        .build(&alloc);

        assert_eq!(
            ast,
            build_record(
                &alloc,
                vec![
                    (
                        "foo",
                        FieldMetadata {
                            doc: Some(alloc.alloc_str("foo")),
                            ..Default::default()
                        },
                        None
                    ),
                    ("bar", Default::default(), None),
                    (
                        "baz",
                        FieldMetadata {
                            doc: Some(alloc.alloc_str("baz")),
                            ..Default::default()
                        },
                        None,
                    )
                ],
                false,
            )
        );
    }

    #[test]
    fn fields() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::new()
            .fields(
                &alloc,
                [
                    Field::name("foo").value(alloc.string("foo")),
                    Field::name("bar").value(alloc.string("bar")),
                ],
            )
            .build(&alloc);

        assert_eq!(
            ast,
            build_simple_record(
                &alloc,
                vec![("foo", alloc.string("foo")), ("bar", alloc.string("bar")),],
                false,
            )
        );
    }

    #[test]
    fn fields_metadata() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::new()
            .fields(
                &alloc,
                [
                    Field::name("foo").optional(true).no_value(),
                    Field::name("bar").optional(true).no_value(),
                ],
            )
            .build(&alloc);

        assert_eq!(
            ast,
            build_record(
                &alloc,
                vec![
                    (
                        "foo",
                        FieldMetadata {
                            opt: true,
                            ..Default::default()
                        },
                        None,
                    ),
                    (
                        "bar",
                        FieldMetadata {
                            opt: true,
                            ..Default::default()
                        },
                        None,
                    ),
                ],
                false,
            )
        );
    }

    #[test]
    fn overriding() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::new()
            .path(vec!["terraform", "required_providers"])
            .value(
                &alloc,
                Record::from_iterator(
                    &alloc,
                    [
                        Field::name("foo").value(Node::Null),
                        Field::name("bar").value(Node::Null),
                    ],
                )
                .build(&alloc),
            )
            .path(vec!["terraform", "required_providers", "foo"])
            .value(&alloc, alloc.string("hello world!"))
            .build(&alloc);

        eprintln!("{ast:?}");

        assert_eq!(
            ast,
            alloc
                .record(record::Record {
                    field_defs: alloc.alloc_many(vec![
                        record::FieldDef {
                            path: alloc.alloc_many(vec![
                                FieldPathElem::Ident("terraform".into()),
                                FieldPathElem::Ident("required_providers".into())
                            ]),
                            metadata: Default::default(),
                            value: Some(
                                alloc
                                    .record(record::Record {
                                        field_defs: alloc.alloc_many(vec![
                                            record::FieldDef {
                                                path: FieldPathElem::single_ident_path(
                                                    &alloc,
                                                    "foo".into()
                                                ),
                                                metadata: Default::default(),
                                                value: Some(Node::Null.into()),
                                                pos: TermPos::None,
                                            },
                                            record::FieldDef {
                                                path: FieldPathElem::single_ident_path(
                                                    &alloc,
                                                    "bar".into()
                                                ),
                                                metadata: Default::default(),
                                                value: Some(Node::Null.into()),
                                                pos: TermPos::None,
                                            },
                                        ]),
                                        ..Default::default()
                                    })
                                    .into()
                            ),
                            pos: TermPos::None,
                        },
                        record::FieldDef {
                            path: alloc.alloc_many(vec![
                                FieldPathElem::Ident("terraform".into()),
                                FieldPathElem::Ident("required_providers".into()),
                                FieldPathElem::Ident("foo".into())
                            ]),
                            metadata: Default::default(),
                            value: Some(alloc.string("hello world!").into()),
                            pos: TermPos::None,
                        }
                    ]),
                    ..Default::default()
                })
                .into()
        );
    }

    #[test]
    fn open_record() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::new().open().build(&alloc);

        assert_eq!(ast, alloc.record(record::Record::empty().open()).into());
    }

    #[test]
    fn prio_metadata() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::new()
            .field("foo")
            .priority(MergePriority::Top)
            .no_value(&alloc)
            .build(&alloc);

        assert_eq!(
            ast,
            build_record(
                &alloc,
                iter::once((
                    "foo",
                    FieldMetadata {
                        priority: MergePriority::Top,
                        ..Default::default()
                    },
                    None,
                )),
                false
            )
        );
    }

    #[test]
    fn contract() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::new()
            .field("foo")
            .contract(TypeF::String)
            .no_value(&alloc)
            .build(&alloc);

        assert_eq!(
            ast,
            build_record(
                &alloc,
                iter::once((
                    "foo",
                    record::FieldMetadata::from(Annotation {
                        contracts: alloc.alloc_singleton(Type {
                            typ: TypeF::String,
                            pos: TermPos::None
                        }),
                        ..Default::default()
                    }),
                    None
                )),
                false
            )
        );
    }

    #[test]
    fn exercise_metadata() {
        let alloc = AstAlloc::new();

        let ast: Ast = Record::new()
            .field("foo")
            .priority(MergePriority::Bottom)
            .doc("foo?")
            .contract(TypeF::String)
            .types(TypeF::Number)
            .optional(true)
            .not_exported(true)
            .no_value(&alloc)
            .build(&alloc);

        assert_eq!(
            ast,
            build_record(
                &alloc,
                iter::once((
                    "foo",
                    FieldMetadata {
                        doc: Some(alloc.alloc_str("foo?")),
                        opt: true,
                        priority: MergePriority::Bottom,
                        not_exported: true,
                        annotation: Annotation {
                            typ: Some(Type {
                                typ: TypeF::Number,
                                pos: TermPos::None,
                            }),
                            contracts: alloc.alloc_singleton(Type {
                                typ: TypeF::String,
                                pos: TermPos::None
                            }),
                        },
                    },
                    None,
                )),
                Default::default()
            )
        );
    }
}