Enum Value

Source
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 Values 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>

Source

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));
Source

pub fn int_from_str<S>(int: S) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

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));
Source

pub fn float_from_str<S>(float: S) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

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));
Source

pub fn bool_from_str<S>(b: S) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

Source

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());
Source

pub fn date_from_str<S>( year: S, month: S, day: S, ) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

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());
Source

pub fn datetime_from_str<S>( year: S, month: S, day: S, hour: S, minute: S, second: S, ) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

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());
Source

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>
where S: Into<String> + Clone,

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());
Source

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());
Source

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>
where S: Into<String> + Clone,

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());
Source

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 andErr(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());
Source

pub fn datetime_zulu_from_str<S>( year: S, month: S, day: S, hour: S, minute: S, second: S, ) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

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 andErr(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());
Source

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>
where S: Into<String> + Clone,

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());
Source

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());
Source

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>
where S: Into<String> + Clone,

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());
Source

pub fn datetime_parse<S>(dt: S) -> Result<Value<'a>, TOMLError>
where S: Into<&'a str>,

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());
Source

pub fn basic_string<S>(s: S) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

pub fn ml_basic_string<S>(s: S) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

pub fn literal_string<S>(s: S) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

pub fn ml_literal_string<S>(s: S) -> Result<Value<'a>, TOMLError>
where S: Into<String> + Clone,

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());
Source

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> Clone for Value<'a>

Source§

fn clone(&self) -> Value<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Value<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Display for Value<'a>

Formats a Value for display. Uses default rust formatting for for i64 for Integers, f64 for Floats, bool for Booleans. The default formatting for Arrays and InlineTables 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.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> PartialEq for Value<'a>

Source§

fn eq(&self, other: &Value<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for Value<'a>

Source§

impl<'a> StructuralPartialEq for Value<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Value<'a>

§

impl<'a> RefUnwindSafe for Value<'a>

§

impl<'a> !Send for Value<'a>

§

impl<'a> !Sync for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> UnwindSafe for Value<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.