pub enum Value<'a> {
Integer(Cow<'a, str>),
Float(Cow<'a, str>),
Boolean(bool),
DateTime(DateTime<'a>),
String(Cow<'a, str>, StrType),
Array(Rc<Vec<Value<'a>>>),
InlineTable(Rc<Vec<(Cow<'a, str>, Value<'a>)>>),
}
Variants§
Integer(Cow<'a, str>)
An integer value. Contains a Cow<str>
representing the integer since integers can contain underscores.
Float(Cow<'a, str>)
A float value. Contains a Cow<str>
representing the float since floats can be formatted many different ways and
can contain underscores.
Boolean(bool)
A boolean value. Contains a bool
value since only true
and false
are allowed.
DateTime(DateTime<'a>)
A DateTime
value. Contains a DateTime
struct that has a date and optionally a time, fractional seconds, and
offset from UTC.
String(Cow<'a, str>, StrType)
A string value. Contains a Cow<str>
with the string contents (without quotes) and StrType
indicating whether
the string is a basic string, multi-line basic string, literal string or multi-line literal string.
Array(Rc<Vec<Value<'a>>>)
An array value. Contains an Rc<Vec>
of Value
s contained in the Array
.
InlineTable(Rc<Vec<(Cow<'a, str>, Value<'a>)>>)
An inline table value. Contains an Rc<Vec>
of tuples that contain a Cow<str>
representing a key, and Value
that the key points to.
Implementations§
Source§impl<'a> Value<'a>
impl<'a> Value<'a>
Sourcepub fn int(int: i64) -> Value<'a>
pub fn int(int: i64) -> Value<'a>
Convenience function for creating an Value::Integer
from an i64
. Cannot fail since i64
maps directly onto
TOML integers.
§Examples
use tomllib::types::Value;
assert_eq!(Value::Integer("100".into()), Value::int(100));
Sourcepub fn int_from_str<S>(int: S) -> Result<Value<'a>, TOMLError>
pub fn int_from_str<S>(int: S) -> Result<Value<'a>, TOMLError>
Convenience function for creating an Value::Integer
from an string type. Returns Ok(Integer)
on success and
Err(TOMLError)
on failure.
§Examples
use tomllib::types::Value;
assert_eq!(Value::Integer("200".into()), Value::int_from_str("200").unwrap());
Sourcepub fn float(float: f64) -> Value<'a>
pub fn float(float: f64) -> Value<'a>
Convenience function for creating a Value::Float
from a f64
. Cannot fail since f64
maps directly onto TOML
floats.
§Examples
use tomllib::types::Value;
assert_eq!(Value::Float("300.3".into()), Value::float(300.3));
Sourcepub fn float_from_str<S>(float: S) -> Result<Value<'a>, TOMLError>
pub fn float_from_str<S>(float: S) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::Float
from an string type. Returns Ok(Float)
on success and
Err(TOMLError)
on failure.
§Examples
use tomllib::types::Value;
assert_eq!(Value::Float("400.4".into()), Value::float_from_str("400.4").unwrap());
Sourcepub fn bool(b: bool) -> Value<'a>
pub fn bool(b: bool) -> Value<'a>
Convenience function for creating a Value::Boolean
from a bool
. Cannot fail since bool
maps directly onto
TOML booleans.
§Examples
use tomllib::types::Value;
assert_eq!(Value::Boolean(true), Value::bool(true));
pub fn bool_from_str<S>(b: S) -> Result<Value<'a>, TOMLError>
Sourcepub fn date_from_int(
year: usize,
month: usize,
day: usize,
) -> Result<Value<'a>, TOMLError>
pub fn date_from_int( year: usize, month: usize, day: usize, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing only a date from integer values. Returns
Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2010", "04", "10").unwrap(), None)),
Value::date_from_int(2010, 4, 10).unwrap());
Sourcepub fn date_from_str<S>(
year: S,
month: S,
day: S,
) -> Result<Value<'a>, TOMLError>
pub fn date_from_str<S>( year: S, month: S, day: S, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing only a date from string values. Returns
Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2011", "05", "11").unwrap(), None)),
Value::date_from_str("2011", "05", "11").unwrap());
Sourcepub fn datetime_from_int(
year: usize,
month: usize,
day: usize,
hour: usize,
minute: usize,
second: usize,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_from_int( year: usize, month: usize, day: usize, hour: usize, minute: usize, second: usize, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time from integer values. Returns
Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2010", "04", "10").unwrap(),
Some(Time::from_str("01", "02", "03", None, None).unwrap()))),
Value::datetime_from_int(2010, 4, 10, 1, 2, 3).unwrap());
Sourcepub fn datetime_from_str<S>(
year: S,
month: S,
day: S,
hour: S,
minute: S,
second: S,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_from_str<S>( year: S, month: S, day: S, hour: S, minute: S, second: S, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time from string values. Returns
Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2011", "05", "11").unwrap(),
Some(Time::from_str("02", "03", "04", None, None).unwrap()))),
Value::datetime_from_str("2011", "05", "11", "02", "03", "04").unwrap());
Sourcepub fn datetime_frac_from_int(
year: usize,
month: usize,
day: usize,
hour: usize,
minute: usize,
second: usize,
frac: usize,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_frac_from_int( year: usize, month: usize, day: usize, hour: usize, minute: usize, second: usize, frac: usize, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with fractional seconds from
integer values. Returns Ok(DateTime)
on success and Err(TOMLError)
on failure. Note, you can’t represent
leading zeros on the fractional part this way for example: 2016-03-15T08:05:22.00055
is not possible using this
function.
§Examples
use tomllib::types::{Value, DateTime, Date, Time};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2010", "04", "10").unwrap(),
Some(Time::from_str("01", "02", "03", Some("5432".into()), None).unwrap()))),
Value::datetime_frac_from_int(2010, 4, 10, 1, 2, 3, 5432).unwrap());
Sourcepub fn datetime_frac_from_str<S>(
year: S,
month: S,
day: S,
hour: S,
minute: S,
second: S,
frac: S,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_frac_from_str<S>( year: S, month: S, day: S, hour: S, minute: S, second: S, frac: S, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with fractional seconds from
string values. Returns Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2011", "05", "11").unwrap(),
Some(Time::from_str("02", "03", "04", Some("0043".into()), None).unwrap()))),
Value::datetime_frac_from_str("2011", "05", "11", "02", "03", "04", "0043").unwrap());
Sourcepub fn datetime_offset_from_int(
year: usize,
month: usize,
day: usize,
hour: usize,
minute: usize,
second: usize,
posneg: char,
off_hour: usize,
off_minute: usize,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_offset_from_int( year: usize, month: usize, day: usize, hour: usize, minute: usize, second: usize, posneg: char, off_hour: usize, off_minute: usize, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with a timezone offset from UTC
from integer values, except for the plus/minus sign which is passed as a char '+'
or '-'
. Returns
Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset, TimeOffsetAmount};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2010", "04", "10").unwrap(),
Some(Time::from_str("01", "02", "03", None, Some(TimeOffset::Time(TimeOffsetAmount::from_str(
"+", "08", "00"
).unwrap()))).unwrap()))),
Value::datetime_offset_from_int(2010, 4, 10, 1, 2, 3, '+', 8, 0).unwrap());
Sourcepub fn datetime_offset_from_str<S>(
year: S,
month: S,
day: S,
hour: S,
minute: S,
second: S,
posneg: S,
off_hour: S,
off_minute: S,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_offset_from_str<S>( year: S, month: S, day: S, hour: S, minute: S, second: S, posneg: S, off_hour: S, off_minute: S, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with a timezone offset from UTC
from string values. Returns Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset, TimeOffsetAmount};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2011", "05", "11").unwrap(),
Some(Time::from_str("02", "03", "04", None, Some(TimeOffset::Time(TimeOffsetAmount::from_str(
"+", "09", "30"
).unwrap()))).unwrap()))),
Value::datetime_offset_from_str("2011", "05", "11", "02", "03", "04", "+", "09", "30").unwrap());
Sourcepub fn datetime_zulu_from_int(
year: usize,
month: usize,
day: usize,
hour: usize,
minute: usize,
second: usize,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_zulu_from_int( year: usize, month: usize, day: usize, hour: usize, minute: usize, second: usize, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with a timezone of Zulu from
integer values. Returns Ok(DateTime)on success and
Err(TOMLError)` on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2010", "04", "10").unwrap(),
Some(Time::from_str("01", "02", "03", None, Some(TimeOffset::Zulu)).unwrap()))),
Value::datetime_zulu_from_int(2010, 4, 10, 1, 2, 3).unwrap());
Sourcepub fn datetime_zulu_from_str<S>(
year: S,
month: S,
day: S,
hour: S,
minute: S,
second: S,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_zulu_from_str<S>( year: S, month: S, day: S, hour: S, minute: S, second: S, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with a timezone of Zulu from
string values. Returns Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2011", "05", "11").unwrap(),
Some(Time::from_str("02", "03", "04", None, Some(TimeOffset::Zulu)).unwrap()))),
Value::datetime_zulu_from_str("2011", "05", "11", "02", "03", "04").unwrap());
Sourcepub fn datetime_full_zulu_from_int(
year: usize,
month: usize,
day: usize,
hour: usize,
minute: usize,
second: usize,
frac: u64,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_full_zulu_from_int( year: usize, month: usize, day: usize, hour: usize, minute: usize, second: usize, frac: u64, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with fractional seconds and a
timezone of Zulu from integer values, except for the plus/minus sign which is passed as a string value "+"
or
“-”. Returns
Ok(DateTime)on success and
Err(TOMLError)on failure. Note, you can't represent leading zeros on the fractional part this way for example:
2016-03-15T08:05:22.00055Z` is not possible using this function.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2010", "04", "10").unwrap(),
Some(Time::from_str("01", "02", "03", Some("5678".into()), Some(TimeOffset::Zulu)).unwrap()))),
Value::datetime_full_zulu_from_int(2010, 4, 10, 1, 2, 3, 5678).unwrap());
Sourcepub fn datetime_full_zulu_from_str<S>(
year: S,
month: S,
day: S,
hour: S,
minute: S,
second: S,
frac: S,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_full_zulu_from_str<S>( year: S, month: S, day: S, hour: S, minute: S, second: S, frac: S, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with fractional seconds and a
timezone of Zulu from string values. Returns Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2011", "05", "11").unwrap(),
Some(Time::from_str("02", "03", "04", None, Some(TimeOffset::Zulu)).unwrap()))),
Value::datetime_zulu_from_str("2011", "05", "11", "02", "03", "04").unwrap());
Sourcepub fn datetime_full_from_int(
year: usize,
month: usize,
day: usize,
hour: usize,
minute: usize,
second: usize,
frac: u64,
posneg: char,
off_hour: usize,
off_minute: usize,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_full_from_int( year: usize, month: usize, day: usize, hour: usize, minute: usize, second: usize, frac: u64, posneg: char, off_hour: usize, off_minute: usize, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with fractional seconds and a
timezone offset from UTC from integer values, except for the plus/minus sign which is passed as a char "+"
or
"-"
. Returns Ok(DateTime)
on success and Err(TOMLError)
on failure. Note, you can’t represent
leading zeros on the fractional part this way for example: 2016-03-15T08:05:22.00055-11:00
is not possible using
this function.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset, TimeOffsetAmount};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2010", "04", "10").unwrap(),
Some(Time::from_str("01", "02", "03", Some("135".into()), Some(TimeOffset::Time(TimeOffsetAmount::from_str(
"-", "11", "00"
).unwrap()))).unwrap()))),
Value::datetime_full_from_int(2010, 4, 10, 1, 2, 3, 135, '-', 11, 0).unwrap());
Sourcepub fn datetime_full_from_str<S>(
year: S,
month: S,
day: S,
hour: S,
minute: S,
second: S,
frac: S,
posneg: S,
off_hour: S,
off_minute: S,
) -> Result<Value<'a>, TOMLError>
pub fn datetime_full_from_str<S>( year: S, month: S, day: S, hour: S, minute: S, second: S, frac: S, posneg: S, off_hour: S, off_minute: S, ) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
containing a date and time with fractional seconds and a
timezone offset from UTC from string values. Returns Ok(DateTime)
on success and Err(TOMLError)
on failure.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset, TimeOffsetAmount};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2011", "05", "11").unwrap(),
Some(Time::from_str("02", "03", "04", Some("0864".into()), Some(TimeOffset::Time(TimeOffsetAmount::from_str(
"+", "09", "30"
).unwrap()))).unwrap()))),
Value::datetime_full_from_str("2011", "05", "11", "02", "03", "04", "0864","+", "09", "30").unwrap());
Sourcepub fn datetime_parse<S>(dt: S) -> Result<Value<'a>, TOMLError>
pub fn datetime_parse<S>(dt: S) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::DateTime
from a sinle string value.
§Examples
use tomllib::types::{Value, DateTime, Date, Time, TimeOffset, TimeOffsetAmount};
assert_eq!(Value::DateTime(DateTime::new(Date::from_str("2012", "06", "12").unwrap(),
Some(Time::from_str("02", "03", "04", Some("0864".into()), Some(TimeOffset::Time(TimeOffsetAmount::from_str(
"+", "10", "30"
).unwrap()))).unwrap()))),
Value::datetime_parse("2012-06-12T02:03:04.0864+10:30").unwrap());
Sourcepub fn basic_string<S>(s: S) -> Result<Value<'a>, TOMLError>
pub fn basic_string<S>(s: S) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::String
with StrType::Basic
. Returns Ok() on success and Err() on
failure.
§Examples
use tomllib::types::{Value, StrType};
assert_eq!(Value::String("foobar".into(), StrType::Basic), Value::basic_string("foobar").unwrap());
Sourcepub fn ml_basic_string<S>(s: S) -> Result<Value<'a>, TOMLError>
pub fn ml_basic_string<S>(s: S) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::String
with StrType::MLBasic
. Returns Ok() on success and Err() on
failure.
§Examples
use tomllib::types::{Value, StrType};
assert_eq!(Value::String("foo\nbar".into(), StrType::MLBasic), Value::ml_basic_string("foo\nbar").unwrap());
Sourcepub fn literal_string<S>(s: S) -> Result<Value<'a>, TOMLError>
pub fn literal_string<S>(s: S) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::String
with StrType::Literal
. Returns Ok() on success and Err() on
failure.
§Examples
use tomllib::types::{Value, StrType};
assert_eq!(Value::String("\"foobar\"".into(), StrType::Literal), Value::literal_string("\"foobar\"").unwrap());
Sourcepub fn ml_literal_string<S>(s: S) -> Result<Value<'a>, TOMLError>
pub fn ml_literal_string<S>(s: S) -> Result<Value<'a>, TOMLError>
Convenience function for creating a Value::String
with StrType::MLLiteral
. Returns Ok() on success and Err()
on failure.
§Examples
use tomllib::types::{Value, StrType};
assert_eq!(Value::String("\"foo\nbar\"".into(), StrType::MLLiteral),
Value::ml_literal_string("\"foo\nbar\"").unwrap());
Sourcepub fn validate(&self) -> bool
pub fn validate(&self) -> bool
Parses and validates a Value
, returns true if the value is valid and false if it is invalid.
§Examples
use tomllib::types::{Value};
assert!(!Value::Integer("_989_721_".into()).validate()); // Integers may have underscores but they must be
// surrounded by digits
assert!(Value::Float("7.62".into()).validate());
Trait Implementations§
Source§impl<'a> Display for Value<'a>
Formats a Value
for display. Uses default rust formatting for for i64
for Integer
s, f64
for Float
s, bool
for Boolean
s. The default formatting for Array
s and InlineTable
s is No whitespace after/before
opening/closing braces, no whitespace before and one space after all commas, no comments on the same line as the
Array
or InlineTable
, and one space before and after an equals sign in an InlineTable
.
impl<'a> Display for Value<'a>
Formats a Value
for display. Uses default rust formatting for for i64
for Integer
s, f64
for Float
s, bool
for Boolean
s. The default formatting for Array
s and InlineTable
s is No whitespace after/before
opening/closing braces, no whitespace before and one space after all commas, no comments on the same line as the
Array
or InlineTable
, and one space before and after an equals sign in an InlineTable
.