[][src]Crate py_literal

This crate provides a type Value that represents a Python literal. Value can be parsed from a string and formatted as a string.

Example

extern crate num;
extern crate py_literal;

use num::{BigInt, Complex};
use py_literal::Value;

// Parse a literal value from a string.
let value: Value = "{ 'foo': [5, (7e3,)], 2 - 5j: {b'bar'} }".parse()?;
assert_eq!(
    value,
    Value::Dict(vec![
        (
            Value::String("foo".to_string()),
            Value::List(vec![
                Value::Integer(BigInt::from(5)),
                Value::Tuple(vec![Value::Float(7e3)]),
            ]),
        ),
        (
            Value::Complex(Complex::new(2., -5.)),
            Value::Set(vec![Value::Bytes(b"bar".to_vec())]),
        ),
    ]),
);

// Format a literal value as a string.
let formatted = format!("{}", value);
assert_eq!(
    formatted,
    "{'foo': [5, (7e3,)], 2-5j: {b'bar'}}",
);

Enums

FormatError

Error formatting a Python literal.

ParseError

Error parsing a Python literal.

Value

Python literal.