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

facet-rs / facet / 19774894729

28 Nov 2025 10:22PM UTC coverage: 59.711% (-0.6%) from 60.346%
19774894729

push

github

fasterthanlime
Add DynamicValue support for deserializing into facet_value::Value

This adds support for deserializing JSON (and potentially other formats)
into facet_value::Value without format crates needing to depend on facet-value.

Key changes:
- Add Def::DynamicValue variant with vtable for building dynamic values
- Implement Facet trait for Value in facet-value
- Extend Partial to handle DynamicValue scalars via set_into_dynamic_value
- Add deserialize_dynamic_value handler in facet-json

Currently supports scalar values (null, bool, numbers, strings).
Arrays and objects are stubbed out with TODO errors.

451 of 922 new or added lines in 20 files covered. (48.92%)

29 existing lines in 3 files now uncovered.

16657 of 27896 relevant lines covered (59.71%)

153.76 hits per line

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

53.33
/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<&mut Self, ReflectError> {
12✔
14
        crate::trace!("begin_set()");
15
        self.require_active()?;
12✔
16
        let frame = self.frames_mut().last_mut().unwrap();
12✔
17

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

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

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

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

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

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

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

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

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

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

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

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

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

136
        Ok(self)
33✔
137
    }
33✔
138
}
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