pub struct Formatted<T> { /* private fields */ }
Expand description
A value together with its to_string
representation,
including surrounding it whitespaces and comments.
Implementations§
source§impl<T> Formatted<T>where
T: ValueRepr,
impl<T> Formatted<T>where
T: ValueRepr,
sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
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
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)
}
sourcepub fn value(&self) -> &T
pub fn value(&self) -> &T
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,
}
}
sourcepub fn into_value(self) -> T
pub fn into_value(self) -> T
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")),
}
}
sourcepub fn decor(&self) -> &Decor
pub fn decor(&self) -> &Decor
Returns the surrounding whitespace
Examples found in repository?
More examples
Trait Implementations§
source§impl<T: PartialEq> PartialEq<Formatted<T>> for Formatted<T>
impl<T: PartialEq> PartialEq<Formatted<T>> for Formatted<T>
impl<T: Eq> Eq for Formatted<T>
impl<T> StructuralEq for Formatted<T>
impl<T> StructuralPartialEq for Formatted<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Formatted<T>where
T: RefUnwindSafe,
impl<T> Send for Formatted<T>where
T: Send,
impl<T> Sync for Formatted<T>where
T: Sync,
impl<T> Unpin for Formatted<T>where
T: Unpin,
impl<T> UnwindSafe for Formatted<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.