Enum edn_rs::edn::Edn

source ·
#[non_exhaustive]
pub enum Edn {
Show 16 variants Tagged(String, Box<Edn>), Vector(Vector), Set(Set), Map(Map), List(List), Key(String), Symbol(String), Str(String), Int(i64), UInt(u64), Double(Double), Rational(String), Char(char), Bool(bool), Nil, Empty,
}
Expand description

EdnType is an Enum with possible values for an EDN type Symbol and Char are not yet implemented String implementation of Edn can be obtained with .to_string()

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Tagged(String, Box<Edn>)

§

Vector(Vector)

§

Set(Set)

§

Map(Map)

§

List(List)

§

Key(String)

§

Symbol(String)

§

Str(String)

§

Int(i64)

§

UInt(u64)

§

Double(Double)

§

Rational(String)

§

Char(char)

§

Bool(bool)

§

Nil

§

Empty

Implementations§

source§

impl Edn

source

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

to_float takes an Edn and returns an Option<f64> with its value. Most types return None

use edn_rs::edn::{Edn, Vector};

let key = Edn::Key(String::from(":1234"));
let q = Edn::Rational(String::from("3/4"));
let i = Edn::Int(12i64);

assert_eq!(Edn::Vector(Vector::empty()).to_float(), None);
assert_eq!(key.to_float().unwrap(),1234f64);
assert_eq!(q.to_float().unwrap(), 0.75f64);
assert_eq!(i.to_float().unwrap(), 12f64);
source

pub fn to_int(&self) -> Option<i64>

to_int takes an Edn and returns an Option<i64> with its value. Most types return None

use edn_rs::edn::{Edn, Vector};

let key = Edn::Key(String::from(":1234"));
let q = Edn::Rational(String::from("3/4"));
let f = Edn::Double(12.3f64.into());

assert_eq!(Edn::Vector(Vector::empty()).to_float(), None);
assert_eq!(key.to_int().unwrap(),1234i64);
assert_eq!(q.to_int().unwrap(), 1i64);
assert_eq!(f.to_int().unwrap(), 12i64);
source

pub fn to_uint(&self) -> Option<u64>

Similar to to_int but returns an Option<u64>

source

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

to_bool takes an Edn and returns an Option<bool> with its value. Most types return None

use edn_rs::edn::{Edn};

let b = Edn::Bool(true);
let s = Edn::Str("true".to_string());
let symbol = Edn::Symbol("false".to_string());

assert_eq!(b.to_bool().unwrap(),true);
assert_eq!(s.to_bool().unwrap(),true);
assert_eq!(symbol.to_bool().unwrap(),false);
source

pub const fn to_char(&self) -> Option<char>

to_char takes an Edn and returns an Option<char> with its value. Most types return None

use edn_rs::edn::{Edn};

let c = Edn::Char('c');
let symbol = Edn::Symbol("false".to_string());

assert_eq!(c.to_char().unwrap(),'c');
assert_eq!(symbol.to_char(), None);
source

pub fn to_vec(&self) -> Option<Vec<String>>

to_vec converts Edn types Vector, List and Set into an Option<Vec<String>>. Type String was selected because it is the current way to mix floats, integers and Strings.

source

pub fn to_int_vec(&self) -> Option<Vec<i64>>

to_int_vec converts Edn types Vector List and Set into an Option<Vec<i64>>. All elements of this Edn structure should be of the same type

source

pub fn to_uint_vec(&self) -> Option<Vec<u64>>

to_uint_vec converts Edn types Vector List and Set into an Option<Vec<u64>>. All elements of this Edn structure should be of the same type

source

pub fn to_float_vec(&self) -> Option<Vec<f64>>

to_float_vec converts Edn types Vector List and Set into an Option<Vec<f64>>. All elements of this Edn structure should be of the same type

source

pub fn to_bool_vec(&self) -> Option<Vec<bool>>

to_bool_vec converts Edn types Vector List and Set into an Option<Vec<bool>>. All elements of this Edn structure should be of the same type

source

pub fn to_debug(&self) -> String

std::fmt::Debug to_debug is a wrapper of format!("{:?}", &self) for &Edn.

use edn_rs::edn::{Edn, Vector};

let edn = Edn::Vector(Vector::new(vec![Edn::Int(5), Edn::Int(6), Edn::Int(7)]));
let expected = "Vector(Vector([Int(5), Int(6), Int(7)]))";

assert_eq!(edn.to_debug(), expected);

While to_string returns a valid edn:

use edn_rs::edn::{Edn, Vector};

let edn = Edn::Vector(Vector::new(vec![Edn::Int(5), Edn::Int(6), Edn::Int(7)]));
let expected = "[5 6 7]";

assert_eq!(edn.to_string(), expected);
source

pub fn get<I: Index>(&self, index: I) -> Option<&Self>

Index into a EDN vector, list, set or map. A string index can be used to access a value in a map, and a u64 index can be used to access an element of a seqs.

Returns None if the type of self does not match the type of the index, for example if the index is a string and self is a seq or a number. Also returns None if the given key does not exist in the map or the given index is not within the bounds of the seq.

#[macro_use]
extern crate edn_rs;
use edn_rs::edn::{Edn, Map, Vector};

fn main() {
    let edn = edn!([ 1 1.2 3 {false :f nil 3/4}]);

    assert_eq!(edn[1], edn!(1.2));
    assert_eq!(edn.get(1).unwrap(), &edn!(1.2));
    assert_eq!(edn[3]["false"], edn!(:f));
    assert_eq!(edn[3].get("false").unwrap(), &Edn::Key(":f".to_string()));
}
source

pub fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self>

Mutably index into a EDN vector, set, list or map. A string index can be used to access a value in a map, and a u64 index can be used to access an element of a seq.

Returns None if the type of self does not match the type of the index, for example if the index is a string and self is a seq or a number. Also returns None if the given key does not exist in the map or the given index is not within the bounds of the seq.

#[macro_use]
extern crate edn_rs;
use edn_rs::edn::{Edn, Map, Vector};

fn main() {
    let mut edn = edn!([ 1 1.2 3 {false :f nil 3/4}]);

    assert_eq!(edn[1], edn!(1.2));
    assert_eq!(edn.get_mut(1).unwrap(), &edn!(1.2));
    assert_eq!(edn[3]["false"], edn!(:f));
    assert_eq!(edn[3].get_mut("false").unwrap(), &Edn::Key(":f".to_string()));
}
source

pub fn iter_some(&self) -> Option<Iter<'_, Self>>

iter_some returns an Option<Iter<Edn>> with Some for types Edn::Vector and Edn::List Other types return None

use edn_rs::{Edn, Vector};

fn main() {
    let v = Edn::Vector(Vector::new(vec![Edn::Int(5), Edn::Int(6), Edn::Int(7)]));
    let sum = v.iter_some().unwrap().filter(|e| e.to_int().is_some()).map(|e| e.to_int().unwrap()).sum();

    assert_eq!(18i64, sum);
}
source

pub fn set_iter(&self) -> Option<Iter<'_, Self>>

set_iter returns am Option<btree_set::Iter<Edn>> with Some for type Edn::Set Other types return None

source

pub fn map_iter(&self) -> Option<Iter<'_, String, Self>>

map_iter returns am Option<btree_map::Iter<String, Edn>> with Some for type Edn::Map Other types return None

Trait Implementations§

source§

impl Clone for Edn

source§

fn clone(&self) -> Edn

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 Debug for Edn

source§

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

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

impl Display for Edn

source§

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

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

impl FromStr for Edn

source§

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

Parses a &str that contains an Edn into Result<Edn, EdnError>

§

type Err = Error

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

impl<I> Index<I> for Edn
where I: Index,

§

type Output = Edn

The returned type after indexing.
source§

fn index(&self, index: I) -> &Self

Performs the indexing (container[index]) operation. Read more
source§

impl<I> IndexMut<I> for Edn
where I: Index,

source§

fn index_mut(&mut self, index: I) -> &mut Self

Performs the mutable indexing (container[index]) operation. Read more
source§

impl Ord for Edn

source§

fn cmp(&self, other: &Edn) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Edn

source§

fn eq(&self, other: &Edn) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Edn

source§

fn partial_cmp(&self, other: &Edn) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for Edn

source§

impl StructuralPartialEq for Edn

Auto Trait Implementations§

§

impl Freeze for Edn

§

impl RefUnwindSafe for Edn

§

impl Send for Edn

§

impl Sync for Edn

§

impl Unpin for Edn

§

impl UnwindSafe for Edn

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

§

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§

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

§

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

§

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.