pub struct Element { /* private fields */ }Expand description
An (annotations, value) pair representing an Ion value.
Implementations§
source§impl Element
impl Element
pub fn null(null_type: IonType) -> Element
pub fn boolean(value: bool) -> Element
pub fn string<I: Into<Str>>(str: I) -> Element
pub fn symbol<I: Into<Symbol>>(symbol: I) -> Element
pub fn integer<I: Into<Int>>(integer: I) -> Element
pub fn decimal(decimal: Decimal) -> Element
pub fn timestamp(timestamp: Timestamp) -> Element
pub fn float(float: f64) -> Element
pub fn clob<A: AsRef<[u8]>>(bytes: A) -> Element
pub fn blob<A: AsRef<[u8]>>(bytes: A) -> Element
pub fn sequence_builder() -> SequenceBuilder
pub fn struct_builder() -> StructBuilder
pub fn ion_type(&self) -> IonType
pub fn annotations(&self) -> &Annotations
pub fn with_annotations<I: IntoAnnotations>(self, annotations: I) -> Self
pub fn is_null(&self) -> bool
pub fn as_int(&self) -> Option<&Int>
pub fn as_float(&self) -> Option<f64>
pub fn as_decimal(&self) -> Option<&Decimal>
pub fn as_timestamp(&self) -> Option<&Timestamp>
pub fn as_text(&self) -> Option<&str>
pub fn as_string(&self) -> Option<&str>
pub fn as_symbol(&self) -> Option<&Symbol>
pub fn as_bool(&self) -> Option<bool>
pub fn as_lob(&self) -> Option<&[u8]>
pub fn as_blob(&self) -> Option<&[u8]>
pub fn as_clob(&self) -> Option<&[u8]>
pub fn as_sequence(&self) -> Option<&Sequence>
pub fn as_struct(&self) -> Option<&Struct>
sourcepub fn read_first<A: AsRef<[u8]>>(data: A) -> IonResult<Option<Element>>
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.
sourcepub fn read_one<A: AsRef<[u8]>>(data: A) -> IonResult<Element>
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).
sourcepub fn read_all<A: AsRef<[u8]>>(data: A) -> IonResult<Vec<Element>>
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).
sourcepub fn write_to<W: ElementWriter>(&self, writer: &mut W) -> IonResult<()>
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);sourcepub fn write_as<W: Write>(&self, format: Format, output: W) -> IonResult<()>
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);sourcepub fn to_binary(&self) -> IonResult<Vec<u8>>
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:
- It must allocate a new
Vec<u8>to fill and return. - It encodes this
Elementas 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);sourcepub fn to_text(&self, text_kind: TextKind) -> IonResult<String>
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 IntAccess for Element
impl IntAccess for Element
source§fn as_i64(&self) -> Option<i64>
fn as_i64(&self) -> Option<i64>
i64 if it can be represented as such. Read moresource§fn as_big_int(&self) -> Option<&BigInt>
fn as_big_int(&self) -> Option<&BigInt>
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