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
//! Integration between `Value` and `sval`.

//!

//! This module allows any `Value` to implement the `Value` trait,

//! and for any `Value` to be captured as a `Value`.


use crate::{std::fmt, Error, Slot, ValueBag};

use super::{
    cast::{self, Cast},
    Inner, Primitive, Visitor,
};

impl<'v> ValueBag<'v> {
    /// Get a value from a structured type.

    ///

    /// This method will attempt to capture the given value as a well-known primitive

    /// before resorting to using its `Value` implementation.

    pub fn capture_sval<T>(value: &'v T) -> Self
    where
        T: Value + 'static,
    {
        cast::try_from_primitive(value).unwrap_or(ValueBag {
            inner: Inner::Sval {
                value,
                type_id: Some(cast::type_id::<T>()),
            },
        })
    }
}

impl<'s, 'f> Slot<'s, 'f> {
    /// Fill the slot with a structured value.

    ///

    /// The given value doesn't need to satisfy any particular lifetime constraints.

    ///

    /// # Panics

    ///

    /// Calling more than a single `fill` method on this slot will panic.

    pub fn fill_sval<T>(&mut self, value: T) -> Result<(), Error>
    where
        T: Value,
    {
        self.fill(|visitor| visitor.sval(&value))
    }
}

impl<'v> Value for ValueBag<'v> {
    fn stream(&self, s: &mut sval::value::Stream) -> sval::value::Result {
        struct SvalVisitor<'a, 'b: 'a>(&'a mut sval::value::Stream<'b>);

        impl<'a, 'b: 'a, 'v> Visitor<'v> for SvalVisitor<'a, 'b> {
            fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
                self.0.debug(v).map_err(Error::from_sval)
            }

            fn u64(&mut self, v: u64) -> Result<(), Error> {
                self.0.u64(v).map_err(Error::from_sval)
            }

            fn i64(&mut self, v: i64) -> Result<(), Error> {
                self.0.i64(v).map_err(Error::from_sval)
            }

            fn f64(&mut self, v: f64) -> Result<(), Error> {
                self.0.f64(v).map_err(Error::from_sval)
            }

            fn bool(&mut self, v: bool) -> Result<(), Error> {
                self.0.bool(v).map_err(Error::from_sval)
            }

            fn char(&mut self, v: char) -> Result<(), Error> {
                self.0.char(v).map_err(Error::from_sval)
            }

            fn str(&mut self, v: &str) -> Result<(), Error> {
                self.0.str(v).map_err(Error::from_sval)
            }

            fn none(&mut self) -> Result<(), Error> {
                self.0.none().map_err(Error::from_sval)
            }

            #[cfg(feature = "std")]
            fn error(&mut self, v: &dyn std::error::Error) -> Result<(), Error> {
                Value::stream(&sval::stream::Source::from(v), self.0).map_err(Error::from_sval)
            }

            fn sval(&mut self, v: &dyn Value) -> Result<(), Error> {
                self.0.any(v).map_err(Error::from_sval)
            }

            #[cfg(feature = "serde")]
            fn serde(&mut self, v: &dyn super::serde::Serialize) -> Result<(), Error> {
                super::serde::sval(self.0, v)
            }
        }

        self.visit(&mut SvalVisitor(s)).map_err(Error::into_sval)?;

        Ok(())
    }
}

impl<'v> From<&'v (dyn Value)> for ValueBag<'v> {
    fn from(value: &'v (dyn Value)) -> ValueBag<'v> {
        ValueBag {
            inner: Inner::Sval {
                value,
                type_id: None,
            },
        }
    }
}

pub use sval::value::Value;

pub(super) fn fmt(f: &mut fmt::Formatter, v: &dyn Value) -> Result<(), Error> {
    sval::fmt::debug(f, v)?;
    Ok(())
}

#[cfg(feature = "serde")]
pub(super) fn serde<S>(s: S, v: &dyn Value) -> Result<S::Ok, S::Error>
where
    S: serde_lib::Serializer,
{
    sval::serde::v1::serialize(s, v)
}

pub(super) fn cast<'v>(v: &dyn Value) -> Cast<'v> {
    struct CastStream<'v>(Cast<'v>);

    impl<'v> sval::stream::Stream for CastStream<'v> {
        fn u64(&mut self, v: u64) -> sval::stream::Result {
            self.0 = Cast::Primitive(Primitive::from(v));
            Ok(())
        }

        fn i64(&mut self, v: i64) -> sval::stream::Result {
            self.0 = Cast::Primitive(Primitive::from(v));
            Ok(())
        }

        fn f64(&mut self, v: f64) -> sval::stream::Result {
            self.0 = Cast::Primitive(Primitive::from(v));
            Ok(())
        }

        fn char(&mut self, v: char) -> sval::stream::Result {
            self.0 = Cast::Primitive(Primitive::from(v));
            Ok(())
        }

        fn bool(&mut self, v: bool) -> sval::stream::Result {
            self.0 = Cast::Primitive(Primitive::from(v));
            Ok(())
        }

        #[cfg(feature = "std")]
        fn str(&mut self, s: &str) -> sval::stream::Result {
            self.0 = Cast::String(s.into());
            Ok(())
        }
    }

    let mut cast = CastStream(Cast::Primitive(Primitive::None));
    let _ = sval::stream(&mut cast, v);

    cast.0
}

impl Error {
    pub(crate) fn from_sval(_: sval::Error) -> Self {
        Error::msg("`sval` serialization failed")
    }

    pub(crate) fn into_sval(self) -> sval::Error {
        sval::Error::msg("`sval` serialization failed")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::Token;

    #[test]
    fn sval_capture() {
        assert_eq!(ValueBag::capture_sval(&42u64).to_token(), Token::U64(42));
    }

    #[test]
    fn sval_cast() {
        assert_eq!(
            42u32,
            ValueBag::capture_sval(&42u64)
                .to_u32()
                .expect("invalid value")
        );

        assert_eq!(
            "a string",
            ValueBag::capture_sval(&"a string")
                .to_borrowed_str()
                .expect("invalid value")
        );

        #[cfg(feature = "std")]
        assert_eq!(
            "a string",
            ValueBag::capture_sval(&"a string")
                .to_str()
                .expect("invalid value")
        );
    }

    #[test]
    fn sval_downcast() {
        #[derive(Debug, PartialEq, Eq)]
        struct Timestamp(usize);

        impl Value for Timestamp {
            fn stream(&self, stream: &mut sval::value::Stream) -> sval::value::Result {
                stream.u64(self.0 as u64)
            }
        }

        let ts = Timestamp(42);

        assert_eq!(
            &ts,
            ValueBag::capture_sval(&ts)
                .downcast_ref::<Timestamp>()
                .expect("invalid value")
        );
    }

    #[test]
    fn sval_structured() {
        let value = ValueBag::from(42u64);
        let expected = vec![sval::test::Token::Unsigned(42)];

        assert_eq!(sval::test::tokens(value), expected);
    }

    #[test]
    fn sval_debug() {
        struct TestSval;

        impl Value for TestSval {
            fn stream(&self, stream: &mut sval::value::Stream) -> sval::value::Result {
                stream.u64(42)
            }
        }

        assert_eq!(
            format!("{:04?}", 42u64),
            format!("{:04?}", ValueBag::capture_sval(&TestSval)),
        );
    }

    #[test]
    #[cfg(feature = "serde")]
    fn sval_serde() {
        use serde_test::{assert_ser_tokens, Token};

        struct TestSval;

        impl Value for TestSval {
            fn stream(&self, stream: &mut sval::value::Stream) -> sval::value::Result {
                stream.u64(42)
            }
        }

        assert_ser_tokens(&ValueBag::capture_sval(&TestSval), &[Token::U64(42)]);
    }

    #[cfg(feature = "std")]
    mod std_support {
        use super::*;

        use crate::std::borrow::ToOwned;

        #[test]
        fn sval_cast() {
            assert_eq!(
                "a string",
                ValueBag::capture_sval(&"a string".to_owned())
                    .to_str()
                    .expect("invalid value")
            );
        }
    }
}