Skip to main content

Sequence

Struct Sequence 

Source
pub struct Sequence<'a> { /* private fields */ }
Expand description

A CBOR Sequence.

Typical actions on this are the same as on StandaloneItem, but it can process multiple CBOR items in a row, and thus offers interaction with all those items:

let from_edn = Sequence::parse("
    1
    2
    3
").unwrap();
assert_eq!(from_edn.items().count(), 3);

Implementations§

Source§

impl<'a> Sequence<'a>

Source

pub fn parse(s: &'a str) -> Result<Self, ParseError>

Ingests CBOR Diagnostic Notation (EDN) representing a CBOR sequence

Note that this will only return syntactic errors. Content errors that make it impossible to produce this as CBOR, such as non-matching encoding indicators or unknown application oriented literals, are not reported.

Source

pub fn serialize(&self) -> String

Produce an EDN String from the sequence

Source

pub fn from_cbor(cbor: &[u8]) -> Result<Self, CborError>

Source

pub fn to_cbor(&self) -> Result<Vec<u8>, InconsistentEdn>

Encode into a binary CBOR representation

Source

pub fn new(items: impl Iterator<Item = Item<'a>>) -> Self

Construct a CBOR sequence from items

Source

pub fn visit_application_literals<F, RF>(&mut self, f: RF)
where F: for<'b> FnMut(String, String, &mut Item<'b>) -> Result<(), String> + ?Sized, RF: DerefMut<Target = F>,

For each item in the tree that is any element of the squence, call a callback.

This is primarily used to apply custom EDN filtering:

let mut full = Sequence::parse("0 /unmodified/, german'zweiundvierzig'").unwrap();
full.visit_application_literals(&mut |id, value: String, item: &mut cbor_edn::Item| {
    if id == "german" {
        let numeric = match value.as_str() {
            "dreiundzwanzig" => 23,
            "zweiundvierzig" => 42,
            _ => todo!(),
        };
        *item = Item::new_integer_decimal(numeric).into();
    }
    Ok(())
});
assert_eq!(full.serialize(), "0 /unmodified/, 42");
Source

pub fn visit_tag<F, RF>(&mut self, f: RF)
where F: for<'b> FnMut(u64, &mut Item<'b>) -> Result<(), String> + ?Sized, RF: DerefMut<Target = F>,

For each item in the full tree of any element (including embedded representations) that is tagged, call a callback.

Any error string is placed in a comment next to the item. The function should return Ok(()) on any tags it is not interested in visiting.

This is primarily used to apply custom EDN application; see application::dt_tag_to_aol for an example.

Source

pub fn items(&self) -> impl Iterator<Item = &Item<'a>>

Access the items of the sequence

Source

pub fn items_mut(&mut self) -> impl Iterator<Item = &mut Item<'a>>

Mutably access the items of the sequence

Source

pub fn get_items_mut(&mut self) -> impl Iterator<Item = &mut Item<'a>>

👎Deprecated:

renamed to items_mut()

Source

pub fn discard_encoding_indicators(&mut self)

Removes any encoding indicators present in the sequence.

This does not affect space or comments; in particular, an item containing only the necessary space may be left with extraneous (but harmless) space that was previously needed to set an encoding indicator apart from a value.

Source

pub fn set_delimiters(&mut self, policy: DelimiterPolicy)

Alters how space and comments are placed inside the sequence.

See the policy values for details.

Source

pub fn cloned<'any>(&self) -> Sequence<'any>

Clone the item, turning any Cow::Borrowed into owned versions, which can then satisfy any lifetime.

Trait Implementations§

Source§

impl<'a> Clone for Sequence<'a>

Source§

fn clone(&self) -> Sequence<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Sequence<'a>

Source§

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

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

impl<'a> PartialEq for Sequence<'a>

Source§

fn eq(&self, other: &Sequence<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<'a> StructuralPartialEq for Sequence<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Sequence<'a>

§

impl<'a> RefUnwindSafe for Sequence<'a>

§

impl<'a> Send for Sequence<'a>

§

impl<'a> Sync for Sequence<'a>

§

impl<'a> Unpin for Sequence<'a>

§

impl<'a> UnsafeUnpin for Sequence<'a>

§

impl<'a> UnwindSafe for Sequence<'a>

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