Skip to main content

Element

Struct 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, 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 location(&self) -> &SourceLocation

Returns the source location (row, column) of this element in the original Ion text.

The location metadata is primarily intended for error reporting and debugging purposes, helping applications provide meaningful feedback to users about the source of issues.

§Returns
  • Some((row, column)) - Position where this element was found in the source text
  • None - Location information is not available
§Important

Location information is best-effort and may not be available in all cases:

  • Elements created programmatically won’t have locations
  • Some parsing scenarios might not preserve location data
  • Binary Ion data does not contain location information

Do not rely on this metadata for programmatic manipulation of Ion data.

Source

pub fn into_value(self) -> Value

Consumes self and returns this Element’s Value.

Source

pub fn into_annotations(self) -> Annotations

Consumes self and returns this Element’s Annotations.

Source

pub fn into_parts(self) -> (Annotations, Value)

Consumes self and returns this Element’s Annotations and Value.

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 int<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 expect_int(&self) -> IonResult<&Int>

Source

pub fn try_into_int(self) -> ConversionOperationResult<Element, Int>

Source

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

Source

pub fn expect_i64(&self) -> IonResult<i64>

Source

pub fn try_into_i64(self) -> ConversionOperationResult<Element, i64>

Source

pub fn as_usize(&self) -> Option<usize>

Source

pub fn expect_usize(&self) -> IonResult<usize>

Source

pub fn try_into_usize(self) -> ConversionOperationResult<Element, usize>

Source

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

Source

pub fn expect_float(&self) -> IonResult<f64>

Source

pub fn try_into_float(self) -> ConversionOperationResult<Element, f64>

Source

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

Source

pub fn expect_decimal(&self) -> IonResult<Decimal>

Source

pub fn try_into_decimal(self) -> ConversionOperationResult<Element, Decimal>

Source

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

Source

pub fn expect_timestamp(&self) -> IonResult<Timestamp>

Source

pub fn try_into_timestamp(self) -> ConversionOperationResult<Element, Timestamp>

Source

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

Source

pub fn expect_text(&self) -> IonResult<&str>

Source

pub fn try_into_text(self) -> ConversionOperationResult<Element, String>

Source

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

Source

pub fn expect_string(&self) -> IonResult<&str>

Source

pub fn try_into_string(self) -> ConversionOperationResult<Element, Str>

Source

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

Source

pub fn expect_symbol(&self) -> IonResult<&Symbol>

Source

pub fn try_into_symbol(self) -> ConversionOperationResult<Element, Symbol>

Source

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

Source

pub fn expect_bool(&self) -> IonResult<bool>

Source

pub fn try_into_bool(self) -> ConversionOperationResult<Element, bool>

Source

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

Source

pub fn expect_lob(&self) -> IonResult<&[u8]>

Source

pub fn try_into_lob(self) -> ConversionOperationResult<Element, Bytes>

Source

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

Source

pub fn expect_blob(&self) -> IonResult<&[u8]>

Source

pub fn try_into_blob(self) -> ConversionOperationResult<Element, Bytes>

Source

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

Source

pub fn expect_clob(&self) -> IonResult<&[u8]>

Source

pub fn try_into_clob(self) -> ConversionOperationResult<Element, Bytes>

Source

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

Source

pub fn expect_sequence(&self) -> IonResult<&Sequence>

Source

pub fn try_into_sequence(self) -> ConversionOperationResult<Element, Sequence>

Source

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

Source

pub fn expect_list(&self) -> IonResult<&Sequence>

Source

pub fn try_into_list(self) -> ConversionOperationResult<Element, Sequence>

Source

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

Source

pub fn expect_sexp(&self) -> IonResult<&Sequence>

Source

pub fn try_into_sexp(self) -> ConversionOperationResult<Element, Sequence>

Source

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

Source

pub fn expect_struct(&self) -> IonResult<&Struct>

Source

pub fn try_into_struct(self) -> ConversionOperationResult<Element, 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<Sequence>

Reads all available Elements from the provided data source.

If the input has valid data, returns Ok(Sequence). If the input has invalid data, returns Err(IonError).

Source

pub fn iter<'a, I: IonInput + 'a>( source: I, ) -> IonResult<impl Iterator<Item = IonResult<Element>> + 'a>

Returns an iterator over the Elements in the provided Ion data source. If the data source cannot be read or contains invalid Ion data, this method will return an Err.

Source

pub fn encode_as<E: Encoding, C: Into<WriteConfig<E>>>( &self, config: C, ) -> IonResult<E::Output>

Encodes this element as an Ion stream with itself as the only top-level value. If the stream’s encoding is binary Ion, returns a Vec<u8> containing the encoded bytes. If the stream’s encoding is text Ion, returns a String containing the UTF-8 encoded text.

use ion_rs::Element;
use ion_rs::v1_0::Binary;

let ion_data = r#"{foo: "hello", bar: quux::5, baz: null, bar: false}"#;
let element_before = Element::read_one(ion_data)?;

// Encode the element as a binary Ion stream
let ion_bytes: Vec<u8> = element_before.encode_as(Binary)?;
// Read the element back from the binary stream
let element_after = Element::read_one(ion_bytes)?;

// Confirm that the value we read back is identical to the one we serialized
assert_eq!(element_before, element_after);
Source

pub fn encode_to<E: Encoding, C: Into<WriteConfig<E>>, W: Write>( &self, output: W, config: C, ) -> IonResult<W>

Encodes this element as an Ion stream with itself as the only top-level value. The encoded bytes are written to the provided io::Write implementation.

use ion_rs::Element;
use ion_rs::v1_0::Binary;

let ion_data = r#"{foo: "hello", bar: quux::5, baz: null, bar: false}"#;
let element_before = Element::read_one(ion_data)?;

// Encode the element as a binary Ion stream. The bytes will be written to the provided
//  Vec<u8>, and the Vec<u8> will be returned when encoding is complete.
let ion_bytes: Vec<u8> = element_before.encode_to(Vec::new(), Binary)?;
// Read the element back from the binary stream
let element_after = Element::read_one(ion_bytes)?;

// Confirm that the value we read back is identical to the one we serialized
assert_eq!(element_before, element_after);

Trait Implementations§

Source§

impl AsRef<Element> for Element

Source§

fn as_ref(&self) -> &Element

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Element

Source§

fn clone(&self) -> Element

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 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 Element
where T: Into<Value>,

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Element> for Sequence

Source§

fn from_iter<T: IntoIterator<Item = Element>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl FromIterator<Element> for List

Source§

fn from_iter<T: IntoIterator<Item = Element>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl FromIterator<Element> for SExp

Source§

fn from_iter<T: IntoIterator<Item = Element>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl PartialEq for Element

Source§

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

Source§

type Error = ConversionOperationError<Element, f64>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, f64>

Performs the conversion.
Source§

impl TryFrom<Element> for Decimal

Source§

type Error = ConversionOperationError<Element, Decimal>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, Decimal>

Performs the conversion.
Source§

impl TryFrom<Element> for Timestamp

Source§

type Error = ConversionOperationError<Element, Timestamp>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, Timestamp>

Performs the conversion.
Source§

impl TryFrom<Element> for String

Source§

type Error = ConversionOperationError<Element, String>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, String>

Performs the conversion.
Source§

impl TryFrom<Element> for Str

Source§

type Error = ConversionOperationError<Element, Str>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, Str>

Performs the conversion.
Source§

impl TryFrom<Element> for Symbol

Source§

type Error = ConversionOperationError<Element, Symbol>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, Symbol>

Performs the conversion.
Source§

impl TryFrom<Element> for bool

Source§

type Error = ConversionOperationError<Element, bool>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, bool>

Performs the conversion.
Source§

impl TryFrom<Element> for Bytes

Source§

type Error = ConversionOperationError<Element, Bytes>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, Bytes>

Performs the conversion.
Source§

impl TryFrom<Element> for Sequence

Source§

type Error = ConversionOperationError<Element, Sequence>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, Sequence>

Performs the conversion.
Source§

impl TryFrom<Element> for Struct

Source§

type Error = ConversionOperationError<Element, Struct>

The type returned in the event of a conversion error.
Source§

fn try_from(element: Element) -> ConversionOperationResult<Element, Struct>

Performs the conversion.

Auto Trait Implementations§

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> ToCompactString for T
where T: Display,

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> ToString for T
where T: Display + ?Sized,

Source§

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

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.