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

facet-rs / facet / 14438741953

14 Apr 2025 06:13AM UTC coverage: 20.628% (-11.0%) from 31.623%
14438741953

Pull #188

github

web-flow
Merge e1c313633 into 046ca7ecc
Pull Request #188: Rewrite facet-reflect for safety

492 of 1057 new or added lines in 44 files covered. (46.55%)

670 existing lines in 18 files now uncovered.

1229 of 5958 relevant lines covered (20.63%)

11.72 hits per line

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

0.0
/facet-msgpack/src/to_msgpack.rs
1
use facet_core::Facet;
2
use facet_reflect::Peek;
3
use log::trace;
4
use std::io::{self, Write};
5

6
/// Serializes any Facet type to MessagePack bytes
UNCOV
7
pub fn to_vec<T: Facet>(value: &T) -> Vec<u8> {
×
UNCOV
8
    let mut buffer = Vec::new();
×
UNCOV
9
    let peek = Peek::new(value);
×
UNCOV
10
    serialize(peek, &mut buffer).unwrap();
×
UNCOV
11
    buffer
×
12
}
13

14
/// Serializes any Facet type to a writer in MessagePack format
UNCOV
15
fn serialize<W: Write>(peek: Peek<'_>, writer: &mut W) -> io::Result<()> {
×
UNCOV
16
    match peek {
×
UNCOV
17
        Peek::Value(pv) => {
×
UNCOV
18
            trace!("Serializing scalar");
×
UNCOV
19
            if pv.shape().is_type::<String>() {
×
UNCOV
20
                let value = unsafe { pv.data().as_ref::<String>() };
×
UNCOV
21
                write_str(writer, value)
×
UNCOV
22
            } else if pv.shape().is_type::<u64>() {
×
UNCOV
23
                let value = unsafe { pv.data().as_ref::<u64>() };
×
UNCOV
24
                write_u64(writer, *value)
×
UNCOV
25
            } else if pv.shape().is_type::<u32>() {
×
UNCOV
26
                let value = unsafe { pv.data().as_ref::<u32>() };
×
UNCOV
27
                write_u32(writer, *value)
×
UNCOV
28
            } else if pv.shape().is_type::<u16>() {
×
UNCOV
29
                let value = unsafe { pv.data().as_ref::<u16>() };
×
UNCOV
30
                write_u16(writer, *value)
×
UNCOV
31
            } else if pv.shape().is_type::<u8>() {
×
UNCOV
32
                let value = unsafe { pv.data().as_ref::<u8>() };
×
UNCOV
33
                write_u8(writer, *value)
×
UNCOV
34
            } else if pv.shape().is_type::<i64>() {
×
UNCOV
35
                let value = unsafe { pv.data().as_ref::<i64>() };
×
UNCOV
36
                write_i64(writer, *value)
×
UNCOV
37
            } else if pv.shape().is_type::<i32>() {
×
UNCOV
38
                let value = unsafe { pv.data().as_ref::<i32>() };
×
UNCOV
39
                write_i32(writer, *value)
×
UNCOV
40
            } else if pv.shape().is_type::<i16>() {
×
UNCOV
41
                let value = unsafe { pv.data().as_ref::<i16>() };
×
UNCOV
42
                write_i16(writer, *value)
×
UNCOV
43
            } else if pv.shape().is_type::<i8>() {
×
UNCOV
44
                let value = unsafe { pv.data().as_ref::<i8>() };
×
UNCOV
45
                write_i8(writer, *value)
×
46
            } else {
47
                todo!("Unsupported scalar type: {}", pv.shape())
48
            }
49
        }
UNCOV
50
        Peek::Struct(ps) => {
×
UNCOV
51
            trace!("Serializing struct");
×
52

53
            // Write map header
UNCOV
54
            let fields: Vec<_> = ps.fields().collect();
×
UNCOV
55
            write_map_len(writer, fields.len())?;
×
56

57
            // Write fields
UNCOV
58
            for (name, field_peek) in fields {
×
UNCOV
59
                write_str(writer, name)?;
×
UNCOV
60
                serialize(field_peek, writer)?;
×
61
            }
UNCOV
62
            Ok(())
×
63
        }
64
        _ => {
×
65
            todo!("Unsupported type: {:?}", peek)
66
        }
67
    }
68
}
69

UNCOV
70
fn write_str<W: Write>(writer: &mut W, s: &str) -> io::Result<()> {
×
UNCOV
71
    let bytes = s.as_bytes();
×
UNCOV
72
    let len = bytes.len();
×
73

UNCOV
74
    match len {
×
UNCOV
75
        0..=31 => {
×
76
            // fixstr
UNCOV
77
            writer.write_all(&[(0xa0 | len as u8)])?;
×
78
        }
79
        32..=255 => {
×
80
            // str8
81
            writer.write_all(&[0xd9, len as u8])?;
×
82
        }
83
        256..=65535 => {
×
84
            // str16
85
            writer.write_all(&[0xda])?;
×
86
            writer.write_all(&(len as u16).to_be_bytes())?;
×
87
        }
88
        _ => {
×
89
            // str32
90
            writer.write_all(&[0xdb])?;
×
91
            writer.write_all(&(len as u32).to_be_bytes())?;
×
92
        }
93
    }
UNCOV
94
    writer.write_all(bytes)
×
95
}
96

UNCOV
97
fn write_u8<W: Write>(writer: &mut W, n: u8) -> io::Result<()> {
×
UNCOV
98
    match n {
×
UNCOV
99
        0..=127 => {
×
100
            // positive fixint
UNCOV
101
            writer.write_all(&[n])
×
102
        }
103
        _ => {
×
104
            // uint8
UNCOV
105
            writer.write_all(&[0xcc, n])
×
106
        }
107
    }
108
}
109

UNCOV
110
fn write_u16<W: Write>(writer: &mut W, n: u16) -> io::Result<()> {
×
UNCOV
111
    match n {
×
UNCOV
112
        0..=127 => {
×
113
            // positive fixint
114
            writer.write_all(&[n as u8])
×
115
        }
UNCOV
116
        128..=255 => {
×
117
            // uint8
118
            writer.write_all(&[0xcc, n as u8])
×
119
        }
120
        _ => {
×
121
            // uint16
UNCOV
122
            writer.write_all(&[0xcd])?;
×
UNCOV
123
            writer.write_all(&n.to_be_bytes())
×
124
        }
125
    }
126
}
127

UNCOV
128
fn write_u32<W: Write>(writer: &mut W, n: u32) -> io::Result<()> {
×
UNCOV
129
    match n {
×
UNCOV
130
        0..=127 => {
×
131
            // positive fixint
132
            writer.write_all(&[n as u8])
×
133
        }
UNCOV
134
        128..=255 => {
×
135
            // uint8
136
            writer.write_all(&[0xcc, n as u8])
×
137
        }
UNCOV
138
        256..=65535 => {
×
139
            // uint16
140
            writer.write_all(&[0xcd])?;
×
141
            writer.write_all(&(n as u16).to_be_bytes())
×
142
        }
143
        _ => {
×
144
            // uint32
UNCOV
145
            writer.write_all(&[0xce])?;
×
UNCOV
146
            writer.write_all(&n.to_be_bytes())
×
147
        }
148
    }
149
}
150

UNCOV
151
fn write_u64<W: Write>(writer: &mut W, n: u64) -> io::Result<()> {
×
UNCOV
152
    match n {
×
UNCOV
153
        0..=127 => {
×
154
            // positive fixint
UNCOV
155
            writer.write_all(&[n as u8])
×
156
        }
UNCOV
157
        128..=255 => {
×
158
            // uint8
159
            writer.write_all(&[0xcc, n as u8])
×
160
        }
UNCOV
161
        256..=65535 => {
×
162
            // uint16
163
            writer.write_all(&[0xcd])?;
×
164
            writer.write_all(&(n as u16).to_be_bytes())
×
165
        }
UNCOV
166
        65536..=4294967295 => {
×
167
            // uint32
168
            writer.write_all(&[0xce])?;
×
169
            writer.write_all(&(n as u32).to_be_bytes())
×
170
        }
171
        _ => {
×
172
            // uint64
UNCOV
173
            writer.write_all(&[0xcf])?;
×
UNCOV
174
            writer.write_all(&n.to_be_bytes())
×
175
        }
176
    }
177
}
178

UNCOV
179
fn write_i8<W: Write>(writer: &mut W, n: i8) -> io::Result<()> {
×
UNCOV
180
    match n {
×
UNCOV
181
        -32..=-1 => {
×
182
            // negative fixint
UNCOV
183
            writer.write_all(&[n as u8])
×
184
        }
UNCOV
185
        -128..=-33 => {
×
186
            // int8
UNCOV
187
            writer.write_all(&[0xd0, n as u8])
×
188
        }
189
        0..=127 => {
×
190
            // positive fixint
191
            writer.write_all(&[n as u8])
×
192
        }
193
    }
194
}
195

UNCOV
196
fn write_i16<W: Write>(writer: &mut W, n: i16) -> io::Result<()> {
×
UNCOV
197
    match n {
×
198
        -32..=-1 => {
×
199
            // negative fixint
200
            writer.write_all(&[n as u8])
×
201
        }
202
        -128..=-33 => {
×
203
            // int8
204
            writer.write_all(&[0xd0, n as u8])
×
205
        }
UNCOV
206
        -32768..=-129 => {
×
207
            // int16
UNCOV
208
            writer.write_all(&[0xd1])?;
×
UNCOV
209
            writer.write_all(&n.to_be_bytes())
×
210
        }
211
        0..=127 => {
×
212
            // positive fixint
213
            writer.write_all(&[n as u8])
×
214
        }
215
        128..=255 => {
×
216
            // uint8
217
            writer.write_all(&[0xcc, n as u8])
×
218
        }
219
        256..=32767 => {
×
220
            // uint16
221
            writer.write_all(&[0xcd])?;
×
222
            writer.write_all(&(n as u16).to_be_bytes())
×
223
        }
224
    }
225
}
226

UNCOV
227
fn write_i32<W: Write>(writer: &mut W, n: i32) -> io::Result<()> {
×
UNCOV
228
    match n {
×
229
        -32..=-1 => {
×
230
            // negative fixint
231
            writer.write_all(&[n as u8])
×
232
        }
233
        -128..=-33 => {
×
234
            // int8
235
            writer.write_all(&[0xd0, n as u8])
×
236
        }
237
        -32768..=-129 => {
×
238
            // int16
239
            writer.write_all(&[0xd1])?;
×
240
            writer.write_all(&(n as i16).to_be_bytes())
×
241
        }
UNCOV
242
        -2147483648..=-32769 => {
×
243
            // int32
UNCOV
244
            writer.write_all(&[0xd2])?;
×
UNCOV
245
            writer.write_all(&n.to_be_bytes())
×
246
        }
247
        0..=127 => {
×
248
            // positive fixint
249
            writer.write_all(&[n as u8])
×
250
        }
251
        128..=255 => {
×
252
            // uint8
253
            writer.write_all(&[0xcc, n as u8])
×
254
        }
255
        256..=65535 => {
×
256
            // uint16
257
            writer.write_all(&[0xcd])?;
×
258
            writer.write_all(&(n as u16).to_be_bytes())
×
259
        }
260
        65536..=2147483647 => {
×
261
            // uint32
262
            writer.write_all(&[0xce])?;
×
263
            writer.write_all(&(n as u32).to_be_bytes())
×
264
        }
265
    }
266
}
267

UNCOV
268
fn write_i64<W: Write>(writer: &mut W, n: i64) -> io::Result<()> {
×
UNCOV
269
    match n {
×
270
        -32..=-1 => {
×
271
            // negative fixint
272
            writer.write_all(&[n as u8])
×
273
        }
274
        -128..=-33 => {
×
275
            // int8
276
            writer.write_all(&[0xd0, n as u8])
×
277
        }
278
        -32768..=-129 => {
×
279
            // int16
280
            writer.write_all(&[0xd1])?;
×
281
            writer.write_all(&(n as i16).to_be_bytes())
×
282
        }
283
        -2147483648..=-32769 => {
×
284
            // int32
285
            writer.write_all(&[0xd2])?;
×
286
            writer.write_all(&(n as i32).to_be_bytes())
×
287
        }
UNCOV
288
        i64::MIN..=-2147483649 => {
×
289
            // int64
UNCOV
290
            writer.write_all(&[0xd3])?;
×
UNCOV
291
            writer.write_all(&n.to_be_bytes())
×
292
        }
293
        0..=127 => {
×
294
            // positive fixint
295
            writer.write_all(&[n as u8])
×
296
        }
297
        128..=255 => {
×
298
            // uint8
299
            writer.write_all(&[0xcc, n as u8])
×
300
        }
301
        256..=65535 => {
×
302
            // uint16
303
            writer.write_all(&[0xcd])?;
×
304
            writer.write_all(&(n as u16).to_be_bytes())
×
305
        }
306
        65536..=4294967295 => {
×
307
            // uint32
308
            writer.write_all(&[0xce])?;
×
309
            writer.write_all(&(n as u32).to_be_bytes())
×
310
        }
311
        4294967296..=i64::MAX => {
×
312
            // uint64
313
            writer.write_all(&[0xcf])?;
×
314
            writer.write_all(&(n as u64).to_be_bytes())
×
315
        }
316
    }
317
}
318

UNCOV
319
fn write_map_len<W: Write>(writer: &mut W, len: usize) -> io::Result<()> {
×
UNCOV
320
    match len {
×
UNCOV
321
        0..=15 => {
×
322
            // fixmap
UNCOV
323
            writer.write_all(&[(0x80 | len as u8)])
×
324
        }
325
        16..=65535 => {
×
326
            // map16
327
            writer.write_all(&[0xde])?;
×
328
            writer.write_all(&(len as u16).to_be_bytes())
×
329
        }
330
        _ => {
×
331
            // map32
332
            writer.write_all(&[0xdf])?;
×
333
            writer.write_all(&(len as u32).to_be_bytes())
×
334
        }
335
    }
336
}
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