• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 19803020611

30 Nov 2025 06:23PM UTC coverage: 57.923% (-3.0%) from 60.927%
19803020611

push

github

fasterthanlime
Convert facet-kdl to use define_attr_grammar!

- Replace ~140 lines of hand-written macros with concise grammar DSL
- Enhance make_parse_attr.rs grammar compiler:
  - Add `ns "kdl";` syntax for namespace declaration
  - Add to_snake_case() helper (NodeName → node_name)
  - Generate __attr! macro with proper ExtensionAttr return
  - Use () data for unit variants, full dispatch for complex ones
  - Add #[repr(u8)] to generated enums for Facet derive
  - Use HashMap for O(1) struct lookup
- Update design diagrams with namespace fixes

50 of 53 new or added lines in 1 file covered. (94.34%)

3583 existing lines in 40 files now uncovered.

18788 of 32436 relevant lines covered (57.92%)

179.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.05
/facet-reflect/src/partial/partial_api/sets.rs
1
use super::*;
2

3
////////////////////////////////////////////////////////////////////////////////////////////////////
4
// Sets
5
////////////////////////////////////////////////////////////////////////////////////////////////////
6
impl Partial<'_> {
7
    /// Initializes a set (HashSet, BTreeSet, etc.) if it hasn't been initialized before.
8
    /// This is a prerequisite to `begin_set_item`/`set`/`end` or the shorthand `insert`.
9
    ///
10
    /// `begin_set` does not clear the set if it was previously initialized.
11
    /// `begin_set` does not push a new frame to the stack, and thus does not
12
    /// require `end` to be called afterwards.
13
    pub fn begin_set(mut self) -> Result<Self, ReflectError> {
12✔
14
        crate::trace!("begin_set()");
15
        let frame = self.frames_mut().last_mut().unwrap();
12✔
16

17
        match &frame.tracker {
12✔
18
            Tracker::Scalar if !frame.is_init => {
12✔
19
                // that's good, let's initialize it
12✔
20
            }
12✔
21
            Tracker::Scalar => {
22
                // is_init is true - initialized (perhaps from a previous round?) but should be a set tracker, let's fix that:
UNCOV
23
                frame.tracker = Tracker::Set {
×
UNCOV
24
                    current_child: false,
×
UNCOV
25
                };
×
UNCOV
26
                return Ok(self);
×
27
            }
28
            Tracker::Set { .. } => {
UNCOV
29
                if frame.is_init {
×
30
                    // already initialized, nothing to do
UNCOV
31
                    return Ok(self);
×
UNCOV
32
                }
×
33
            }
34
            _ => {
UNCOV
35
                return Err(ReflectError::UnexpectedTracker {
×
UNCOV
36
                    message: "begin_set called but tracker isn't something set-like",
×
UNCOV
37
                    current_tracker: frame.tracker.kind(),
×
UNCOV
38
                });
×
39
            }
40
        };
41

42
        // Check that we have a Set
43
        let set_def = match &frame.shape.def {
12✔
44
            Def::Set(set_def) => set_def,
12✔
45
            _ => {
UNCOV
46
                return Err(ReflectError::OperationFailed {
×
UNCOV
47
                    shape: frame.shape,
×
UNCOV
48
                    operation: "begin_set can only be called on Set types",
×
UNCOV
49
                });
×
50
            }
51
        };
52

53
        let init_fn = set_def.vtable.init_in_place_with_capacity_fn;
12✔
54

55
        // Initialize the set with default capacity (0)
56
        unsafe {
12✔
57
            init_fn(frame.data, 0);
12✔
58
        }
12✔
59

60
        // Update tracker to Set state and mark as initialized
61
        frame.tracker = Tracker::Set {
12✔
62
            current_child: false,
12✔
63
        };
12✔
64
        frame.is_init = true;
12✔
65

66
        Ok(self)
12✔
67
    }
12✔
68

69
    /// Begins pushing an element to the set.
70
    /// The element should be set using `set()` or similar methods, then `end()` to complete.
71
    pub fn begin_set_item(mut self) -> Result<Self, ReflectError> {
33✔
72
        crate::trace!("begin_set_item()");
73
        let frame = self.frames_mut().last_mut().unwrap();
33✔
74

75
        // Check that we have a Set that's been initialized
76
        let set_def = match &frame.shape.def {
33✔
77
            Def::Set(set_def) => set_def,
33✔
78
            _ => {
UNCOV
79
                return Err(ReflectError::OperationFailed {
×
UNCOV
80
                    shape: frame.shape,
×
81
                    operation: "begin_set_item can only be called on Set types",
×
82
                });
×
83
            }
84
        };
85

86
        // Verify the tracker is in Set state and initialized
87
        match &mut frame.tracker {
33✔
88
            Tracker::Set { current_child } if frame.is_init => {
33✔
89
                if *current_child {
33✔
UNCOV
90
                    return Err(ReflectError::OperationFailed {
×
UNCOV
91
                        shape: frame.shape,
×
92
                        operation: "already pushing an element, call end() first",
×
93
                    });
×
94
                }
33✔
95
                *current_child = true;
33✔
96
            }
97
            _ => {
UNCOV
98
                return Err(ReflectError::OperationFailed {
×
UNCOV
99
                    shape: frame.shape,
×
100
                    operation: "must call begin_set() before begin_set_item()",
×
101
                });
×
102
            }
103
        }
104

105
        // Get the element shape
106
        let element_shape = set_def.t();
33✔
107

108
        // Allocate space for the new element
109
        let element_layout = match element_shape.layout.sized_layout() {
33✔
110
            Ok(layout) => layout,
33✔
111
            Err(_) => {
UNCOV
112
                return Err(ReflectError::Unsized {
×
UNCOV
113
                    shape: element_shape,
×
114
                    operation: "begin_set_item: calculating element layout",
×
115
                });
×
116
            }
117
        };
118
        let element_ptr: *mut u8 = unsafe { ::alloc::alloc::alloc(element_layout) };
33✔
119

120
        let Some(element_ptr) = NonNull::new(element_ptr) else {
33✔
UNCOV
121
            return Err(ReflectError::OperationFailed {
×
UNCOV
122
                shape: frame.shape,
×
123
                operation: "failed to allocate memory for set element",
×
124
            });
×
125
        };
126

127
        // Push a new frame for the element
128
        self.frames_mut().push(Frame::new(
33✔
129
            PtrUninit::new(element_ptr),
33✔
130
            element_shape,
33✔
131
            FrameOwnership::Owned,
33✔
132
        ));
133

134
        Ok(self)
33✔
135
    }
33✔
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc