Skip to main content

CanonicalValue

Enum CanonicalValue 

Source
pub enum CanonicalValue {
    Null,
    Bool(bool),
    Number(Number),
    String(String),
    Array(Vec<CanonicalValue>),
    Object(Vec<(String, CanonicalValue)>),
}
Expand description

Format-agnostic intermediate value used by the Sheeter hub.

Every reader (CSV, TSV, JSON, YAML, XML) produces a CanonicalValue, and every writer consumes one. Adding a format requires exactly one reader and one writer, not N-1 pairwise converters, because every spoke composes through this hub.

Tabular data is represented as Array<Object>. XML documents are represented as a single-key Object whose key is the root element name, with attributes prefixed by @ and text stored under #text. Repeated sibling elements become Array.

§Examples

use sheeter_engine::CanonicalValue;

let v = CanonicalValue::from_json(r#"[{"a":1}]"#).unwrap();
assert_eq!(v.to_csv().unwrap(), "a\n1\n");

Variants§

§

Null

§

Bool(bool)

§

Number(Number)

§

String(String)

§

Array(Vec<CanonicalValue>)

§

Object(Vec<(String, CanonicalValue)>)

Implementations§

Source§

impl CanonicalValue

Source

pub fn from_json(input: &str) -> Result<Self, Box<dyn Error>>

Parses JSON into the canonical hub.

§Errors

Returns an error if the JSON is malformed.

§Examples
use sheeter_engine::CanonicalValue;
assert!(CanonicalValue::from_json(r#"{"a":1}"#).is_ok());
Source

pub fn to_json(&self) -> Result<String, Box<dyn Error>>

Serializes the canonical hub to JSON.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::String("x".into());
assert_eq!(v.to_json().unwrap(), r#""x""#);
Source

pub fn from_toml(input: &str) -> Result<Self, Box<dyn Error>>

Source

pub fn to_toml(&self) -> Result<String, Box<dyn Error>>

Source

pub fn from_xlsx_workbook( bytes: &[u8], ) -> Result<Vec<(String, Self)>, Box<dyn Error>>

Source

pub fn to_xlsx_workbook( sheets: &[(String, Self)], ) -> Result<Vec<u8>, Box<dyn Error>>

Source

pub fn from_csv(input: &str) -> Result<Self, Box<dyn Error>>

Parses CSV into the canonical hub.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::from_csv("a\n1").unwrap();
assert_eq!(v.to_json().unwrap(), r#"[{"a":"1"}]"#);
Source

pub fn to_csv(&self) -> Result<String, Box<dyn Error>>

Serializes the canonical hub to CSV.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::from_json(r#"[{"a":1}]"#).unwrap();
assert_eq!(v.to_csv().unwrap(), "a\n1\n");
Source

pub fn from_tsv(input: &str) -> Result<Self, Box<dyn Error>>

Parses TSV into the canonical hub.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::from_tsv("a\tb\n1\t2").unwrap();
assert_eq!(v.to_json().unwrap(), r#"[{"a":"1","b":"2"}]"#);
Source

pub fn to_tsv(&self) -> Result<String, Box<dyn Error>>

Serializes the canonical hub to TSV.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::from_json(r#"[{"a":1}]"#).unwrap();
assert_eq!(v.to_tsv().unwrap(), "a\n1\n");
Source

pub fn from_yaml(input: &str) -> Result<Self, Box<dyn Error>>

Parses YAML into the canonical hub.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::from_yaml("- a: 1\n").unwrap();
assert_eq!(v.to_json().unwrap(), r#"[{"a":1}]"#);
Source

pub fn to_yaml(&self) -> Result<String, Box<dyn Error>>

Serializes the canonical hub to YAML.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::from_json(r#"[{"a":1}]"#).unwrap();
assert!(v.to_yaml().unwrap().contains("a: 1"));
Source

pub fn from_xml(input: &str) -> Result<Self, Box<dyn Error>>

Parses XML into the canonical hub.

Attributes become @name keys, text content becomes #text, and repeated sibling elements become arrays. The returned value is an object with exactly one key: the root element name.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::from_xml("<r><a>1</a><a>2</a></r>").unwrap();
assert_eq!(v.to_json().unwrap(), r#"{"r":{"a":["1","2"]}}"#);
Source

pub fn to_xml(&self) -> Result<String, Box<dyn Error>>

Serializes the canonical hub to XML.

§Examples
use sheeter_engine::CanonicalValue;
let v = CanonicalValue::from_json(r#"[{"a":"1"}]"#).unwrap();
let xml = v.to_xml().unwrap();
assert!(xml.contains("<root>"));
assert!(xml.contains("<a>1</a>"));

Trait Implementations§

Source§

impl Clone for CanonicalValue

Source§

fn clone(&self) -> Self

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 CanonicalValue

Source§

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

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

impl PartialEq for CanonicalValue

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CanonicalValue

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.