Struct ion_rs::element::Element

source ·
pub struct Element { /* private fields */ }
Expand description

An (annotations, value) pair representing an Ion value.

Implementations§

source§

impl Element

source

pub fn value(&self) -> &Value

Returns a reference to this Element’s Value.

use ion_rs::element::{Element, Value};
let element: Element = true.into();
if let Value::Bool(b) = element.value() {
    println!("It was a boolean: {b}");
} else {
    println!("It was something else.");
}
source

pub fn null(null_type: IonType) -> Element

source

pub fn boolean(value: bool) -> Element

source

pub fn string<I: Into<Str>>(str: I) -> Element

source

pub fn symbol<I: Into<Symbol>>(symbol: I) -> Element

source

pub fn integer<I: Into<Int>>(integer: I) -> Element

source

pub fn decimal(decimal: Decimal) -> Element

source

pub fn timestamp(timestamp: Timestamp) -> Element

source

pub fn float(float: f64) -> Element

source

pub fn clob<A: AsRef<[u8]>>(bytes: A) -> Element

source

pub fn blob<A: AsRef<[u8]>>(bytes: A) -> Element

source

pub fn sequence_builder() -> SequenceBuilder

source

pub fn struct_builder() -> StructBuilder

source

pub fn ion_type(&self) -> IonType

source

pub fn annotations(&self) -> &Annotations

source

pub fn with_annotations<I: IntoAnnotations>(self, annotations: I) -> Self

source

pub fn is_null(&self) -> bool

source

pub fn as_int(&self) -> Option<&Int>

source

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

source

pub fn as_decimal(&self) -> Option<&Decimal>

source

pub fn as_timestamp(&self) -> Option<&Timestamp>

source

pub fn as_text(&self) -> Option<&str>

source

pub fn as_string(&self) -> Option<&str>

source

pub fn as_symbol(&self) -> Option<&Symbol>

source

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

source

pub fn as_lob(&self) -> Option<&[u8]>

source

pub fn as_blob(&self) -> Option<&[u8]>

source

pub fn as_clob(&self) -> Option<&[u8]>

source

pub fn as_sequence(&self) -> Option<&Sequence>

source

pub fn as_struct(&self) -> Option<&Struct>

source

pub fn read_first<A: AsRef<[u8]>>(data: A) -> IonResult<Option<Element>>

Reads a single Ion Element from the provided data source.

If the data source is empty, returns Ok(None). If the data source has at least one value, returns Ok(Some(Element)). If the data source has invalid data, returns Err.

source

pub fn read_one<A: AsRef<[u8]>>(data: A) -> IonResult<Element>

Reads a single Ion Element from the provided data source. If the input has invalid data or does not contain at exactly one Ion value, returns Err(IonError).

source

pub fn read_all<A: AsRef<[u8]>>(data: A) -> IonResult<Vec<Element>>

Reads all available Elements from the provided data source.

If the input has valid data, returns Ok(Vec<Element>). If the input has invalid data, returns Err(IonError).

source

pub fn write_to<W: ElementWriter>(&self, writer: &mut W) -> IonResult<()>

Serializes this element to the provided writer.

use ion_rs::element::Element;
use ion_rs::{ion_list, IonType, IonWriter, TextWriterBuilder};

// Construct an Element
let element_before: Element = ion_list! [1, 2, 3].into();

// Serialize the Element to a writer
let mut writer = TextWriterBuilder::default().build(Vec::new())?;
element_before.write_to(&mut writer)?;
writer.flush()?;

// Read the Element back from the serialized form
let element_after = Element::read_one(writer.output())?;

// Confirm that no data was lost
assert_eq!(element_before, element_after);
source

pub fn write_as<W: Write>(&self, format: Format, output: W) -> IonResult<()>

Serializes this Element as Ion, writing the resulting bytes to the provided io::Write. The caller must verify that output is either empty or only contains Ion of the same format (text or binary) before writing begins.

This method constructs a new writer for each invocation, which means that there will only be a single top level value in the output stream. Writing several values to the same stream is preferable to maximize encoding efficiency. To reuse a writer and have greater control over resource management, see Element::write_to.

use ion_rs::element::Element;
use ion_rs::{ion_list, IonType, IonWriter, TextWriterBuilder};
use ion_rs::element::writer::{Format, TextKind};

// Construct an Element
let element_before: Element = ion_list! [1, 2, 3].into();

// Write the Element to a buffer using a specified format
let mut buffer = Vec::new();
element_before.write_as(Format::Text(TextKind::Pretty), &mut buffer)?;

// Read the Element back from the serialized form
let element_after = Element::read_one(&buffer)?;

// Confirm that no data was lost
assert_eq!(element_before, element_after);
source

pub fn to_binary(&self) -> IonResult<Vec<u8>>

Serializes this Element as binary Ion, returning the output as a Vec<u8>.

This is a convenience method; it is less efficient than Element::write_to because:

  1. It must allocate a new Vec<u8> to fill and return.
  2. It encodes this Element as its own binary Ion stream, limiting the benefit of the symbol table.
use ion_rs::element::Element;
use ion_rs::{ion_list, IonType, IonWriter, TextWriterBuilder};
use ion_rs::element::writer::{Format, TextKind};

// Construct an Element
let element_before: Element = ion_list! [1, 2, 3].into();

// Write the Element to a buffer as binary Ion
let binary_ion: Vec<u8> = element_before.to_binary()?;

// Read the Element back from the serialized form
let element_after = Element::read_one(&binary_ion)?;

// Confirm that no data was lost
assert_eq!(element_before, element_after);
source

pub fn to_text(&self, text_kind: TextKind) -> IonResult<String>

Serializes this Element as text Ion using the requested TextKind and returning the output as a String.

This is a convenience method; to provide your own buffer instead of allocating a String on each call, see Element::write_as. To provide your own writer instead of constructing a new String and writer on each call, see Element::write_to.

use ion_rs::element::Element;
use ion_rs::{ion_list, IonType, IonWriter, TextWriterBuilder};
use ion_rs::element::writer::{Format, TextKind};

// Construct an Element
let element_before: Element = ion_list! [1, 2, 3].into();

let text_ion: String = element_before.to_text(TextKind::Pretty)?;

// Read the Element back from the serialized form
let element_after = Element::read_one(&text_ion)?;

// Confirm that no data was lost
assert_eq!(element_before, element_after);

Trait Implementations§

source§

impl Clone for Element

source§

fn clone(&self) -> Element

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 Element

source§

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

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

impl Display for Element

source§

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

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

impl<'a> From<&'a Element> for Element

source§

fn from(element: &'a Element) -> Self

Converts to this type from the input type.
source§

impl<T> From<T> for Elementwhere T: Into<Value>,

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl IntAccess for Element

source§

fn as_i64(&self) -> Option<i64>

Returns the value as an i64 if it can be represented as such. Read more
source§

fn as_big_int(&self) -> Option<&BigInt>

Returns a reference as a BigInt if it is represented as such. Note that this method may return None if the underlying representation is not stored in a BigInt such as if it is represented as an i64 so it is somewhat asymmetric with respect to IntAccess::as_i64. Read more
source§

impl PartialEq<Element> for Element

source§

fn eq(&self, other: &Self) -> 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 Eq for Element

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.