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
impl Value
Sourcepub fn format_ascii(&self) -> Result<String, FormatError>
pub fn format_ascii(&self) -> Result<String, FormatError>
Formats the value as an ASCII string.
Sourcepub fn write_ascii<W: Write>(&self, w: &mut W) -> Result<(), FormatError>
pub fn write_ascii<W: Write>(&self, w: &mut W) -> Result<(), FormatError>
Source§impl Value
impl Value
Sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
Returns true
if self
is Value::String
. Returns false
otherwise.
Sourcepub fn as_string(&self) -> Option<&String>
pub fn as_string(&self) -> Option<&String>
If self
is Value::String
, returns the associated string. Returns None
otherwise.
Sourcepub fn as_bytes(&self) -> Option<&Vec<u8>>
pub fn as_bytes(&self) -> Option<&Vec<u8>>
If self
is Value::Bytes
, returns the associated bytes. Returns None
otherwise.
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true
if self
is Value::Integer
. Returns false
otherwise.
Sourcepub fn as_integer(&self) -> Option<&BigInt>
pub fn as_integer(&self) -> Option<&BigInt>
If self
is Value::Integer
, returns the associated integer. Returns None
otherwise.
Sourcepub fn as_float(&self) -> Option<f64>
pub fn as_float(&self) -> Option<f64>
If self
is Value::Float
, returns the associated float. Returns None
otherwise.
Sourcepub fn is_complex(&self) -> bool
pub fn is_complex(&self) -> bool
Returns true
if self
is Value::Complex
. Returns false
otherwise.
Sourcepub fn as_complex(&self) -> Option<Complex<f64>>
pub fn as_complex(&self) -> Option<Complex<f64>>
If self
is Value::Complex
, returns the associated complex number. Returns None
otherwise.
Sourcepub fn as_tuple(&self) -> Option<&Vec<Value>>
pub fn as_tuple(&self) -> Option<&Vec<Value>>
If self
is Value::Tuple
, returns the associated data. Returns None
otherwise.
Sourcepub fn as_list(&self) -> Option<&Vec<Value>>
pub fn as_list(&self) -> Option<&Vec<Value>>
If self
is Value::List
, returns the associated data. Returns None
otherwise.
Sourcepub fn as_dict(&self) -> Option<&Vec<(Value, Value)>>
pub fn as_dict(&self) -> Option<&Vec<(Value, Value)>>
If self
is Value::Dict
, returns the associated data. Returns None
otherwise.
Sourcepub fn as_set(&self) -> Option<&Vec<Value>>
pub fn as_set(&self) -> Option<&Vec<Value>>
If self
is Value::Set
, returns the associated data. Returns None
otherwise.
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Returns true
if self
is Value::Boolean
. Returns false
otherwise.
Sourcepub fn as_boolean(&self) -> Option<bool>
pub fn as_boolean(&self) -> Option<bool>
If self
is Value::Boolean
, returns the associated data. Returns None
otherwise.
Trait Implementations§
Source§impl FromStr for Value
impl FromStr for Value
Source§fn from_str(s: &str) -> Result<Self, ParseError>
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
andu
/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