Enum py_literal::Value[][src]

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,
}

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

impl Value[src]

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

Formats the value as an ASCII string.

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

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().

impl Value[src]

pub fn is_string(&self) -> bool[src]

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

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

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

pub fn is_bytes(&self) -> bool[src]

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

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

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

pub fn is_integer(&self) -> bool[src]

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

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

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

pub fn is_float(&self) -> bool[src]

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

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

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

pub fn is_complex(&self) -> bool[src]

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

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

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

pub fn is_tuple(&self) -> bool[src]

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

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

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

pub fn is_list(&self) -> bool[src]

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

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

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

pub fn is_dict(&self) -> bool[src]

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

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

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

pub fn is_set(&self) -> bool[src]

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

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

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

pub fn is_boolean(&self) -> bool[src]

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

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

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

pub fn is_none(&self) -> bool[src]

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

Trait Implementations

impl Clone for Value[src]

impl Debug for Value[src]

impl Display for Value[src]

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

Formats the value as a Python literal.

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

impl FromStr for Value[src]

type Err = ParseError

The associated error which can be returned from parsing.

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

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

impl PartialEq<Value> for Value[src]

impl StructuralPartialEq for Value[src]

Auto Trait Implementations

impl RefUnwindSafe for Value

impl Send for Value

impl Sync for Value

impl Unpin for Value

impl UnwindSafe for Value

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.