Enum Value

Source
pub enum Value {
    String(String),
    Bytes(Vec<u8>),
    Integer(BigInt),
    Float(f64),
    Complex(Complex<f64>),
    Tuple(Vec<Value>),
    List(Vec<Value>),
    Dict(Vec<(Value, Value)>),
    Set(Vec<Value>),
    Boolean(bool),
    None,
}
Expand description

Python literal.

This type should be able to express everything that Python’s ast.literal_eval() can evaluate, except for operators. Similar to literal_eval(), addition and subtraction of numbers is supported in the parser. However, binary addition and subtraction operators cannot be formatted using Value.

Variants§

§

String(String)

Python string (str). When parsing, backslash escapes are interpreted. When formatting, backslash escapes are used to ensure the result contains only ASCII chars.

§

Bytes(Vec<u8>)

Python byte sequence (bytes). When parsing, backslash escapes are interpreted. When formatting, backslash escapes are used to ensure the result contains only ASCII chars.

§

Integer(BigInt)

Python integer (int). Python integers have unlimited precision, so we use BigInt.

§

Float(f64)

Python floating-point number (float). The representation and precision of the Python float type varies by the machine where the program is executing, but f64 should be good enough.

§

Complex(Complex<f64>)

Python complex number (complex). The Python complex type contains two float values.

§

Tuple(Vec<Value>)

Python tuple (tuple).

§

List(Vec<Value>)

Python list (list).

§

Dict(Vec<(Value, Value)>)

Python dictionary (dict).

§

Set(Vec<Value>)

Python set (set).

§

Boolean(bool)

Python boolean (bool).

§

None

Python None.

Implementations§

Source§

impl Value

Source

pub fn format_ascii(&self) -> Result<String, FormatError>

Formats the value as an ASCII string.

Source

pub fn write_ascii<W: Write>(&self, w: &mut W) -> Result<(), FormatError>

Writes the value as ASCII.

This implementation performs a lot of small writes. If individual writes are expensive (e.g. if the writer is a TcpStream), it would be a good idea to wrap the writer in a BufWriter before passing it to .write_ascii().

Source§

impl Value

Source

pub fn is_string(&self) -> bool

Returns true if self is Value::String. Returns false otherwise.

Source

pub fn as_string(&self) -> Option<&String>

If self is Value::String, returns the associated string. Returns None otherwise.

Source

pub fn is_bytes(&self) -> bool

Returns true if self is Value::Bytes. Returns false otherwise.

Source

pub fn as_bytes(&self) -> Option<&Vec<u8>>

If self is Value::Bytes, returns the associated bytes. Returns None otherwise.

Source

pub fn is_integer(&self) -> bool

Returns true if self is Value::Integer. Returns false otherwise.

Source

pub fn as_integer(&self) -> Option<&BigInt>

If self is Value::Integer, returns the associated integer. Returns None otherwise.

Source

pub fn is_float(&self) -> bool

Returns true if self is Value::Float. Returns false otherwise.

Source

pub fn as_float(&self) -> Option<f64>

If self is Value::Float, returns the associated float. Returns None otherwise.

Source

pub fn is_complex(&self) -> bool

Returns true if self is Value::Complex. Returns false otherwise.

Source

pub fn as_complex(&self) -> Option<Complex<f64>>

If self is Value::Complex, returns the associated complex number. Returns None otherwise.

Source

pub fn is_tuple(&self) -> bool

Returns true if self is Value::Tuple. Returns false otherwise.

Source

pub fn as_tuple(&self) -> Option<&Vec<Value>>

If self is Value::Tuple, returns the associated data. Returns None otherwise.

Source

pub fn is_list(&self) -> bool

Returns true if self is Value::List. Returns false otherwise.

Source

pub fn as_list(&self) -> Option<&Vec<Value>>

If self is Value::List, returns the associated data. Returns None otherwise.

Source

pub fn is_dict(&self) -> bool

Returns true if self is Value::Dict. Returns false otherwise.

Source

pub fn as_dict(&self) -> Option<&Vec<(Value, Value)>>

If self is Value::Dict, returns the associated data. Returns None otherwise.

Source

pub fn is_set(&self) -> bool

Returns true if self is Value::Set. Returns false otherwise.

Source

pub fn as_set(&self) -> Option<&Vec<Value>>

If self is Value::Set, returns the associated data. Returns None otherwise.

Source

pub fn is_boolean(&self) -> bool

Returns true if self is Value::Boolean. Returns false otherwise.

Source

pub fn as_boolean(&self) -> Option<bool>

If self is Value::Boolean, returns the associated data. Returns None otherwise.

Source

pub fn is_none(&self) -> bool

Returns true if self is Value::None. Returns false otherwise.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate 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 Debug for Value

Source§

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

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

impl Display for Value

Source§

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

Formats the value as a Python literal.

Currently, this just calls self.format_ascii(), but that may change in the future.

Source§

impl FromStr for Value

Source§

fn from_str(s: &str) -> Result<Self, ParseError>

Parses a Value from a Python literal. The goal is for the parser to support everything ast.literal_eval() does. A few things haven’t been implemented yet:

  • r/R and u/U prefixes for string and bytes literals.
  • string literal concatenation
  • newlines (except in string literals)
  • parentheses (except as tuple delimiters)
  • Unicode name escapes in strings (\N{name})

Note that the parser is limited to Python literals, not the full Python AST, so many things are not supported, such as:

  • identifiers
  • formatted string literals (f/F prefix)
  • binary operators (except for + and - on numeric literals)
  • function calls
Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> 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 StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

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.