[−][src]Struct kserd::Kserd
The atomic value.
Kserd
contains an optional identity (id
), and the Value
. Taken together the Kserd
object can
describe (almost) all Rust data structures. The identity can be mostly ignored for self-explanatory
data such as primitives. It can be used to name structures, and plays a special
role in unit structs and enum variants.
Kserd
dereferences mutably to Value
so all reading and mutating methods on Value
can
use directly. It is encouraged to use the methods rather than accessing the fields directly.
Kserd
implements ordering and equality, it is important to note that ordering and equality is
done only on the value, the identity is ignored.
Example
Use the methods to quickly see the data if the type is known. Mutating can be done directly.
let mut kserd = Kserd::new_str("Hi"); // access the data without needing to pattern match assert_eq!(kserd.str(), Some("Hi")); // using ctors will name your ds for you assert_eq!(kserd.id(), Some("str")); // if not of the type, nothing returned. assert_eq!(kserd.int(), None); kserd.str_mut().map(|s| { s.pop(); s.push_str("ello, world!"); }); // mutate directly. // this method has the additional effect of cloning the string. assert_eq!(kserd.str(), Some("Hello, world!"));
Fields
id: Option<Kstr<'a>>
The identity of the value.
This can be optional. Typically the identity is something like the struct or enum name.
val: Value<'a>
The Value
.
This store the actual value, which comprises of primitive types or more complex nested types such as sequences or maps.
Methods
impl Kserd<'static>
[src]
Static lifetime constructors. These do not need a Serialize
trait.
pub const fn new_unit() -> Self
[src]
A new unit value ()
.
Example
let kserd = Kserd::new_unit(); assert_eq!(kserd.unit(), true); assert_eq!(kserd.id(), None);
pub fn new_bool(value: bool) -> Self
[src]
A new boolean value.
Example
let kserd = Kserd::new_bool(true); assert_eq!(kserd.bool(), Some(true)); assert_eq!(kserd.id(), Some("bool"));
pub fn new_num<T: NumberType>(value: T) -> Self
[src]
A new number value. The trait NumberType
is implemented on all Rust primitive
numbers so number literals can be used.
Example
let kserd = Kserd::new_num(123456); assert_eq!(kserd.uint(), Some(123456)); let kserd = Kserd::new_num(-123456); assert_eq!(kserd.int(), Some(-123456)); let kserd = Kserd::new_num(3.14); assert_eq!(kserd.float(), Some(3.14));
pub fn new_string(string: String) -> Self
[src]
A new string value. The ownership of the string is transferred and as such
the Kserd
has a static lifetime.
Example
let kserd = Kserd::new_string(String::from("Hello, world!")); assert_eq!(kserd.str(), Some("Hello, world!")); assert_eq!(kserd.id(), Some("String"));
pub fn new_barrv(byte_array: Vec<u8>) -> Self
[src]
A new byte array value. The ownership of the vector is transferred and as such the Kserd
has a static lifetime.
Example
let kserd = Kserd::new_barrv(vec![0,1,2,3]); assert_eq!(kserd.barr(), Some([0,1,2,3].as_ref())); assert_eq!(kserd.id(), Some("ByteVec"));
impl<'a> Kserd<'a>
[src]
General lifetime contructors. These do not need a Serialize
trait.
pub const fn new(value: Value<'a>) -> Self
[src]
A new Kserd
with the specified Value
.
No identity is ascribed to the Kserd
.
Example
let kserd = Kserd::new(Value::Unit); assert_eq!(kserd.val, Value::Unit); assert_eq!(kserd.id, None);
pub fn with_id<S: Into<Kstr<'a>>>(
identity: S,
value: Value<'a>
) -> Result<Self, InvalidId>
[src]
identity: S,
value: Value<'a>
) -> Result<Self, InvalidId>
A new Kserd
with a specified identity and Value
.
The identity is taken as a Kstr
, meaning it can be a reference or owned.
String
and &str
can be used as the identity. The identity must also be
valid, that is, it cannot contain certain characters or Err
will be returned.
It is recommended to construct
Kserd
in this manner rather than manually via setting theid
andval
fields as an invalid identity will not parse back from text.
Example
let kserd = Kserd::with_id("an-identity", Value::Bool(true)).unwrap(); assert_eq!(kserd.bool(), Some(true)); assert_eq!(kserd.id(), Some("an-identity")); let kserd = Kserd::with_id("<an,> in(valid.)/{identity}\\=", Value::Unit); assert_eq!(kserd.is_err(), true);
pub fn new_str(string: &'a str) -> Self
[src]
A new string value. The Kserd
borrows the string and has the same lifetime.
Example
let kserd = Kserd::new_str("Hello, world!"); assert_eq!(kserd.str(), Some("Hello, world!")); assert_eq!(kserd.id(), Some("str"));
pub fn new_barr(byte_array: &'a [u8]) -> Self
[src]
A new byte array value. The Kserd
borrows the array and has the same lifetime.
Example
let kserd = Kserd::new_barr([0,1,2,5,10].as_ref()); assert_eq!(kserd.barr(), Some([0,1,2,5,10].as_ref())); assert_eq!(kserd.id(), Some("barr"));
pub fn new_cntr<I, S>(iter: I) -> Result<Self, InvalidFieldName> where
S: Into<Kstr<'a>>,
I: IntoIterator<Item = (S, Kserd<'a>)>,
[src]
S: Into<Kstr<'a>>,
I: IntoIterator<Item = (S, Kserd<'a>)>,
Construct a new container Kserd
from a list of field-value pairs.
Example
let pass = Kserd::new_cntr(vec![ ("a", Kserd::new_num(0)) ]).unwrap(); let fail = Kserd::new_cntr(vec![ ("1 wrong/name", Kserd::new_num(0)) ]); assert_eq!(fail.is_err(), true);
pub fn new_map<I>(iter: I) -> Self where
I: IntoIterator<Item = (Kserd<'a>, Kserd<'a>)>,
[src]
I: IntoIterator<Item = (Kserd<'a>, Kserd<'a>)>,
Construct a new map value from a list of key-value pairs.
Example
let kserd = Kserd::new_map(vec![ (Kserd::new_unit(), Kserd::new_num(0)) ]);
impl<'a> Kserd<'a>
[src]
pub fn id(&self) -> Option<&str>
[src]
The identity. Same as the .id
field but mapped as a &str
.
Example
let kserd = Kserd::with_id("Hello", Value::Unit).unwrap(); assert_eq!(kserd.id(), Some("Hello"));
impl<'a> Kserd<'a>
[src]
Conversions.
pub fn into_owned(self) -> Kserd<'static>
[src]
Clones all data to make a static Kserd
.
pub fn mk_brw(&self) -> Kserd
[src]
Makes a copy of this Kserd
that references data in the this Kserd
.
This is particularly useful if you want to gaurantee that all data is of the borrowed
variety when decoding back to a data structure (see Decoder
for explanation).
There is a performance penalty as nested structures have to be rebuilt.
Example
let kserd = Kserd::new_string("Hello, world!".to_owned()); let brwed = kserd.mk_brw(); assert_eq!(kserd, brwed);
impl Kserd<'static>
[src]
pub fn enc<T: Serialize>(data: &T) -> Result<Self, Error>
[src]
Encode T
into a Kserd
.
Requires the encode feature.
Convenience function for data.serialize(Encoder)
.
See Encoder
for usage.
impl<'a> Kserd<'a>
[src]
pub fn decode<T: Deserialize<'a>>(self) -> Result<T, Error>
[src]
Attempt to decode a Kserd
into type T
.
Requires the encode feature.
Convenience function for <T as Deserialize>::deserialize(Decoder(self))
.
See Decoder
for usage.
impl<'a> Kserd<'a>
[src]
String representation.
pub fn as_str(&self) -> String
[src]
Format the Kserd
as a string using the default
FormattingConfig
.
Example
let kserd = Kserd::with_id("AStruct", Value::new_cntr(vec![ ("a", Kserd::new_num(1_010_101)), ("b", Kserd::new_num(3.14)), ("c", Kserd::new_str("Hello, world!")), ("d", Kserd::new_barrv(vec![100,150,200,225])) ]).unwrap()).unwrap(); let s = kserd.as_str(); assert_eq!( &s, r#"AStruct ( a = 1010101 b = 3.14 c = "Hello, world!" d = b91'"!Mo4' )"#);
pub fn as_str_with_config(&self, config: FormattingConfig) -> String
[src]
Format the Kserd
as a string using the specified
FormattingConfig
.
Example
use kserd::fmt::FormattingConfig; let kserd = Kserd::with_id("AStruct", Value::new_cntr(vec![ ("a", Kserd::new_num(1_010_101)), ("b", Kserd::new_num(3.14)), ("c", Kserd::new_str("Hello, world!")), ("d", Kserd::new_barrv(vec![100,150,200,225])) ]).unwrap()).unwrap(); let config = FormattingConfig { id_on_containers: false, // don't give container name width_limit: None, // all inline ..Default::default() }; let s = kserd.as_str_with_config(config); assert_eq!( &s, r#"(a = 1010101, b = 3.14, c = "Hello, world!", d = b91'"!Mo4')"# );
Methods from Deref<Target = Value<'a>>
pub fn unit(&self) -> bool
[src]
Value
is a unit value (Value::Unit
).
Example
let value = Value::Unit; assert_eq!(value.unit(), true);
pub fn bool(&self) -> Option<bool>
[src]
Value
is a boolean value.
Example
let value = Value::Bool(true); assert_eq!(value.bool(), Some(true));
pub fn bool_mut(&mut self) -> Option<&mut bool>
[src]
Value
is a boolean value. Can be altered.
Example
let mut value = Value::Bool(false); value.bool_mut().map(|x| *x = true); assert_eq!(value.bool(), Some(true));
pub fn ch(&self) -> Option<char>
[src]
Value
is a string with a single character.
Example
let value = Value::new_str("A"); assert_eq!(value.ch(), Some('A')); let value = Value::new_str("Hello, world!"); assert_eq!(value.ch(), None);
pub fn uint(&self) -> Option<u128>
[src]
Value
is an unsigned integer.
Example
let value = Value::new_num(123456); assert_eq!(value.uint(), Some(123456));
pub fn int(&self) -> Option<i128>
[src]
Value
is a signed integer. A positive integer can be both signed and unsigned
up to i128::max_value()
.
Example
let value = Value::new_num(-123456); assert_eq!(value.int(), Some(-123456));
pub fn float(&self) -> Option<f64>
[src]
Value
is a floating point number. Both signed and unsigned integers can be represented
as floats.
Example
let value = Value::new_num(-3.14); assert_eq!(value.float(), Some(-3.14));
pub fn num_mut(&mut self) -> Option<&mut Number>
[src]
Value
is a numerical value, and can be altered.
Example
let mut value = Value::new_num(123456); value.num_mut().map(|x| *x = Number::from(100)); assert_eq!(value.uint(), Some(100));
pub fn str(&self) -> Option<&str>
[src]
Value
is a string.
Example
let value = Value::new_str("Hello, world!"); assert_eq!(value.str(), Some("Hello, world!"));
pub fn str_mut(&mut self) -> Option<&mut String>
[src]
Value
is a string. Can be altered.
Clones string value if not owned.
Example
let mut value = Value::new_str("Hello"); value.str_mut().map(|x| { x.push_str(", world!"); }); assert_eq!(value.str(), Some("Hello, world!"));
pub fn barr(&self) -> Option<&[u8]>
[src]
Value
is a byte array.
Example
let value = Value::new_barr([0,1,2,5,10].as_ref()); assert_eq!(value.barr(), Some([0,1,2,5,10].as_ref()));
pub fn barr_mut(&mut self) -> Option<&mut Vec<u8>>
[src]
Value
is a byte array. Can be altered.
Clones data if not already owned.
Example
let mut value = Value::new_barr([0,1,2].as_ref()); value.barr_mut().map(|x| { x.push(3); }); assert_eq!(value.barr(), Some([0,1,2,3].as_ref()));
pub fn mk_brw(&self) -> Value
[src]
Makes a copy of this Value
that references data in the this Value
.
This is particularly useful if you want to gaurantee that all data is of the borrowed
variety when decoding back to a data structure (see Decoder
for explanation).
There is a performance penalty as nested structures have to be rebuilt.
Example
let value = Value::new_string("Hello, world!".to_owned()); let brwed = value.mk_brw(); assert_eq!(value, brwed);
Trait Implementations
impl<'a> Clone for Kserd<'a>
[src]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<'a> Debug for Kserd<'a>
[src]
impl<'a> Deref for Kserd<'a>
[src]
impl<'a> DerefMut for Kserd<'a>
[src]
impl<'a> Display for Kserd<'a>
[src]
impl<'a> Eq for Kserd<'a>
[src]
impl<'a> Ord for Kserd<'a>
[src]
fn cmp(&self, other: &Kserd<'a>) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
fn min(self, other: Self) -> Self
1.21.0[src]
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<'a> PartialEq<Kserd<'a>> for Kserd<'a>
[src]
impl<'a> PartialOrd<Kserd<'a>> for Kserd<'a>
[src]
fn partial_cmp(&self, other: &Kserd<'a>) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<'a> ToKserd<'a> for Kserd<'a>
[src]
fn into_kserd(self) -> Result<Kserd<'a>, ToKserdErr>
[src]
Auto Trait Implementations
impl<'a> RefUnwindSafe for Kserd<'a>
impl<'a> Send for Kserd<'a>
impl<'a> Sync for Kserd<'a>
impl<'a> Unpin for Kserd<'a>
impl<'a> UnwindSafe for Kserd<'a>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,