Struct toml_edit::Formatted

source ·
pub struct Formatted<T> { /* private fields */ }
Expand description

A value together with its to_string representation, including surrounding it whitespaces and comments.

Implementations§

Default-formatted value

Examples found in repository?
src/value.rs (line 249)
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
    fn from(s: String) -> Self {
        Value::String(Formatted::new(s))
    }
}

impl<'b> From<&'b InternalString> for Value {
    fn from(s: &'b InternalString) -> Self {
        s.as_str().into()
    }
}

impl From<InternalString> for Value {
    fn from(s: InternalString) -> Self {
        s.as_str().into()
    }
}

impl From<i64> for Value {
    fn from(i: i64) -> Self {
        Value::Integer(Formatted::new(i))
    }
}

impl From<f64> for Value {
    fn from(f: f64) -> Self {
        Value::Float(Formatted::new(f))
    }
}

impl From<bool> for Value {
    fn from(b: bool) -> Self {
        Value::Boolean(Formatted::new(b))
    }
}

impl From<Datetime> for Value {
    fn from(d: Datetime) -> Self {
        Value::Datetime(Formatted::new(d))
    }
More examples
Hide additional examples
src/parser/value.rs (lines 22-24)
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
pub(crate) fn value(input: Input<'_>) -> IResult<Input<'_>, v::Value, ParserError<'_>> {
    dispatch!{peek(any);
            crate::parser::strings::QUOTATION_MARK |
            crate::parser::strings::APOSTROPHE => string.map(|s| {
                v::Value::String(Formatted::new(
                    s.into_owned()
                ))
            }),
            crate::parser::array::ARRAY_OPEN => array.map(v::Value::Array),
            crate::parser::inline_table::INLINE_TABLE_OPEN => inline_table.map(v::Value::InlineTable),
            // Date/number starts
            b'+' | b'-' | b'0'..=b'9' => {
                // Uncommon enough not to be worth optimizing at this time
                alt((
                    date_time
                        .map(v::Value::from),
                    float
                        .map(v::Value::from),
                    integer
                        .map(v::Value::from),
                ))
            },
            // Report as if they were numbers because its most likely a typo
            b'_' => {
                    integer
                        .map(v::Value::from)
                .context(Context::Expected(ParserValue::Description("leading digit")))
            },
            // Report as if they were numbers because its most likely a typo
            b'.' =>  {
                    float
                        .map(v::Value::from)
                .context(Context::Expected(ParserValue::Description("leading digit")))
            },
            b't' => {
                crate::parser::numbers::true_.map(v::Value::from)
                    .context(Context::Expression("string"))
                    .context(Context::Expected(ParserValue::CharLiteral('"')))
                    .context(Context::Expected(ParserValue::CharLiteral('\'')))
            },
            b'f' => {
                crate::parser::numbers::false_.map(v::Value::from)
                    .context(Context::Expression("string"))
                    .context(Context::Expected(ParserValue::CharLiteral('"')))
                    .context(Context::Expected(ParserValue::CharLiteral('\'')))
            },
            b'i' => {
                crate::parser::numbers::inf.map(v::Value::from)
                    .context(Context::Expression("string"))
                    .context(Context::Expected(ParserValue::CharLiteral('"')))
                    .context(Context::Expected(ParserValue::CharLiteral('\'')))
            },
            b'n' => {
                crate::parser::numbers::nan.map(v::Value::from)
                    .context(Context::Expression("string"))
                    .context(Context::Expected(ParserValue::CharLiteral('"')))
                    .context(Context::Expected(ParserValue::CharLiteral('\'')))
            },
            _ => {
                fail
                    .context(Context::Expression("string"))
                    .context(Context::Expected(ParserValue::CharLiteral('"')))
                    .context(Context::Expected(ParserValue::CharLiteral('\'')))
            },
    }
        .with_recognized()
        .map_res(|(value, raw)| apply_raw(value, raw))
        .parse(input)
}

The wrapped value

Examples found in repository?
src/value.rs (line 48)
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
    pub fn as_str(&self) -> Option<&str> {
        match *self {
            Value::String(ref value) => Some(value.value()),
            _ => None,
        }
    }

    /// Returns true iff `self` is a string.
    pub fn is_str(&self) -> bool {
        self.as_str().is_some()
    }

    /// Casts `self` to integer.
    pub fn as_integer(&self) -> Option<i64> {
        match *self {
            Value::Integer(ref value) => Some(*value.value()),
            _ => None,
        }
    }

    /// Returns true iff `self` is an integer.
    pub fn is_integer(&self) -> bool {
        self.as_integer().is_some()
    }

    /// Casts `self` to float.
    pub fn as_float(&self) -> Option<f64> {
        match *self {
            Value::Float(ref value) => Some(*value.value()),
            _ => None,
        }
    }

    /// Returns true iff `self` is a float.
    pub fn is_float(&self) -> bool {
        self.as_float().is_some()
    }

    /// Casts `self` to boolean.
    pub fn as_bool(&self) -> Option<bool> {
        match *self {
            Value::Boolean(ref value) => Some(*value.value()),
            _ => None,
        }
    }

    /// Returns true iff `self` is a boolean.
    pub fn is_bool(&self) -> bool {
        self.as_bool().is_some()
    }

    /// Casts `self` to date-time.
    pub fn as_datetime(&self) -> Option<&Datetime> {
        match *self {
            Value::Datetime(ref value) => Some(value.value()),
            _ => None,
        }
    }

The wrapped value

Examples found in repository?
src/de/value.rs (line 13)
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
    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: serde::de::Visitor<'de>,
    {
        match self {
            crate::Value::String(v) => visitor.visit_string(v.into_value()),
            crate::Value::Integer(v) => visitor.visit_i64(v.into_value()),
            crate::Value::Float(v) => visitor.visit_f64(v.into_value()),
            crate::Value::Boolean(v) => visitor.visit_bool(v.into_value()),
            crate::Value::Datetime(v) => visitor.visit_map(DatetimeDeserializer {
                date: v.into_value(),
                visited: false,
            }),
            crate::Value::Array(v) => visitor.visit_seq(crate::de::ArraySeqAccess::with_array(v)),
            crate::Value::InlineTable(v) => {
                visitor.visit_map(crate::de::InlineTableMapAccess::new(v))
            }
        }
    }

    fn deserialize_struct<V>(
        self,
        name: &'static str,
        fields: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Error>
    where
        V: serde::de::Visitor<'de>,
    {
        if name == toml_datetime::__unstable::NAME && fields == [toml_datetime::__unstable::FIELD] {
            if let crate::Value::Datetime(d) = self {
                return visitor.visit_map(DatetimeDeserializer {
                    date: d.into_value(),
                    visited: false,
                });
            }
        }

        self.deserialize_any(visitor)
    }

    // `None` is interpreted as a missing field so be sure to implement `Some`
    // as a present field.
    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
    where
        V: serde::de::Visitor<'de>,
    {
        visitor.visit_some(self)
    }

    // Called when the type to deserialize is an enum, as opposed to a field in the type.
    fn deserialize_enum<V>(
        self,
        _name: &'static str,
        _variants: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Error>
    where
        V: serde::de::Visitor<'de>,
    {
        match self {
            crate::Value::String(v) => visitor.visit_enum(v.into_value().into_deserializer()),
            crate::Value::InlineTable(v) => {
                if v.is_empty() {
                    Err(crate::de::Error::custom(
                        "wanted exactly 1 element, found 0 elements",
                    ))
                } else if v.len() != 1 {
                    Err(crate::de::Error::custom(
                        "wanted exactly 1 element, more than 1 element",
                    ))
                } else {
                    visitor.visit_enum(crate::de::InlineTableMapAccess::new(v))
                }
            }
            _ => Err(crate::de::Error::custom("wanted string or inline table")),
        }
    }

Returns the key raw representation.

Examples found in repository?
src/encode.rs (line 63)
62
63
64
65
66
67
68
69
70
71
    fn encode(&self, buf: &mut dyn Write, default_decor: (&str, &str)) -> Result {
        let repr = self.to_repr();
        write!(
            buf,
            "{}{}{}",
            self.decor().prefix().unwrap_or(default_decor.0),
            repr,
            self.decor().suffix().unwrap_or(default_decor.1)
        )
    }

Returns the surrounding whitespace

Examples found in repository?
src/value.rs (line 162)
160
161
162
163
164
165
166
167
168
169
170
    pub fn decor_mut(&mut self) -> &mut Decor {
        match self {
            Value::String(f) => f.decor_mut(),
            Value::Integer(f) => f.decor_mut(),
            Value::Float(f) => f.decor_mut(),
            Value::Boolean(f) => f.decor_mut(),
            Value::Datetime(f) => f.decor_mut(),
            Value::Array(a) => a.decor_mut(),
            Value::InlineTable(t) => t.decor_mut(),
        }
    }

Returns the surrounding whitespace

Examples found in repository?
src/encode.rs (line 67)
62
63
64
65
66
67
68
69
70
71
    fn encode(&self, buf: &mut dyn Write, default_decor: (&str, &str)) -> Result {
        let repr = self.to_repr();
        write!(
            buf,
            "{}{}{}",
            self.decor().prefix().unwrap_or(default_decor.0),
            repr,
            self.decor().suffix().unwrap_or(default_decor.1)
        )
    }
More examples
Hide additional examples
src/value.rs (line 180)
178
179
180
181
182
183
184
185
186
187
188
    pub fn decor(&self) -> &Decor {
        match *self {
            Value::String(ref f) => f.decor(),
            Value::Integer(ref f) => f.decor(),
            Value::Float(ref f) => f.decor(),
            Value::Boolean(ref f) => f.decor(),
            Value::Datetime(ref f) => f.decor(),
            Value::Array(ref a) => a.decor(),
            Value::InlineTable(ref t) => t.decor(),
        }
    }

Auto formats the value.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.