[][src]Struct jomini::Scalar

pub struct Scalar<'a> { /* fields omitted */ }

Single value encapsulating windows-1252 data.

Since windows-1252 is a single byte character encoding, a scalar will never fail to be created.

use jomini::Scalar;

let v1 = Scalar::new(b"10");
assert_eq!(v1.to_utf8(), "10");
assert_eq!(v1.to_u64(), Ok(10));
assert_eq!(v1.to_i64(), Ok(10));
assert_eq!(v1.to_f64(), Ok(10.0));
assert!(v1.to_bool().is_err());

Implementations

impl<'a> Scalar<'a>[src]

pub fn new(data: &'a [u8]) -> Scalar<'a>[src]

Create a new scalar backed by windows-1252 encoded byte slice

pub fn view_data(&self) -> &[u8][src]

View the underlying windows-1252 encoded data

pub fn to_f64(&self) -> Result<f64, ScalarError>[src]

Try converting the scalar to f64

use jomini::Scalar;

let v1 = Scalar::new(b"1.000");
assert_eq!(v1.to_f64(), Ok(1.0));

let v2 = Scalar::new(b"-5.67821");
assert_eq!(v2.to_f64(), Ok(-5.67821));

pub fn to_bool(&self) -> Result<bool, ScalarError>[src]

Try converting the scalar to boolean, only "yes" and "no" can be mapped:

use jomini::Scalar;

let v1 = Scalar::new(b"yes");
assert_eq!(v1.to_bool(), Ok(true));

let v2 = Scalar::new(b"no");
assert_eq!(v2.to_bool(), Ok(false));

pub fn to_i64(&self) -> Result<i64, ScalarError>[src]

Try converting the scalar to i64

use jomini::Scalar;

let v1 = Scalar::new(b"-50");
assert_eq!(v1.to_i64(), Ok(-50));

let v2 = Scalar::new(b"120");
assert_eq!(v2.to_i64(), Ok(120));

pub fn to_u64(&self) -> Result<u64, ScalarError>[src]

Try converting the scalar to u64

use jomini::Scalar;

let v1 = Scalar::new(b"50");
assert_eq!(v1.to_i64(), Ok(50));

let v2 = Scalar::new(b"120");
assert_eq!(v2.to_i64(), Ok(120));

pub fn to_utf8(&self) -> Cow<'a, str>[src]

Convert scalar data into utf8. Several transformations take place:

  • trailing whitespace is removed
  • escape sequences are unescaped
  • windows-1252 specific characters encoded as their utf-8 equivalent.

This function is optimized for the typical scenario, where the utf-8 string can be calculated without allocation. If escape sequences or windows-1252 specific characters are encountered, then allocation is necessary.

use jomini::Scalar;

let v1 = Scalar::new(b"Common Sense");
assert_eq!(v1.to_utf8(), "Common Sense");

let v2 = Scalar::new(b"\xa7GRichard Plantagenet\xa7 ( 2 / 4 / 3 / 0 )");
assert_eq!(v2.to_utf8(), "§GRichard Plantagenet§ ( 2 / 4 / 3 / 0 )");

let v3 = Scalar::new(br#"Captain \"Joe\" Rogers"#);
assert_eq!(v3.to_utf8(), r#"Captain "Joe" Rogers"#);

let v4 = Scalar::new(b"1444.11.11\n");
assert_eq!(v4.to_utf8(), "1444.11.11");

pub fn to_utf8_owned(&self) -> String[src]

Convert scalar data into an owned string

use jomini::Scalar;

let v1 = Scalar::new(b"a");
assert_eq!(v1.to_utf8(), String::from("a"));

let v2 = Scalar::new(&[255][..]);
assert_eq!(v2.to_utf8(), String::from("ÿ"));

pub fn is_ascii(&self) -> bool[src]

Returns if the scalar contains only ascii values

use jomini::Scalar;

let v1 = Scalar::new(b"a");
assert!(v1.is_ascii());

let v2 = Scalar::new(&[255][..]);
assert!(!v2.is_ascii());

Trait Implementations

impl<'a> Clone for Scalar<'a>[src]

impl<'a> Copy for Scalar<'a>[src]

impl<'a> Debug for Scalar<'a>[src]

impl<'a> Display for Scalar<'a>[src]

impl<'a> PartialEq<Scalar<'a>> for Scalar<'a>[src]

impl<'a> StructuralPartialEq for Scalar<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Scalar<'a>

impl<'a> Send for Scalar<'a>

impl<'a> Sync for Scalar<'a>

impl<'a> Unpin for Scalar<'a>

impl<'a> UnwindSafe for Scalar<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.