[][src]Enum inkling::Variable

pub enum Variable {
    Address(Address),
    Bool(bool),
    Divert(Address),
    Float(f32),
    Int(i32),
    String(String),
}

Variable in a story.

Variables are typed and come in several variants, covering the basic needs of number and string processing in inkling. When encountered in a story the processor will parse a string from the value.

Be aware though, not all variants will evaluate to a string. Numbers and strings make perfect sense to print: a divert to another location, not as much since it has no meaning in text, just in the story internals. Take care not to use variables that contain divert addresses in the text flow.

Type safety

As the enum consists of variants, the compiler cannot enforce static type checking of Variables. However, these variables are supposed to be static. Type safety is enforced at runtime when values are updated.

Examples

Any variant can be constructed (although Address and Divert are typically meant to be created by the story processor, not the user):

let variable = Variable::Int(5);
let other_variable = Variable::String("I love you!".to_string());

The From trait is implemented for integer, floating point, boolean and string types.

assert_eq!(Variable::from(5), Variable::Int(5));
assert_eq!(Variable::from(3.0), Variable::Float(3.0));
assert_eq!(Variable::from(true), Variable::Bool(true));
assert_eq!(Variable::from("💜"), Variable::String("💜".to_string()));

Variants

Address(Address)

Address to a stitch or other variable.

If the address is another variable in the story it will evaluate to that. If it is a location in the story it will evaluate to the number of times it has been visited.

Example

If a line in the story contains the expression {hazardous} this will be treated as an address to either a knot/stitch or a global variable. The processor will take the value at the address and print that.

Bool(bool)

True or false.

When printed, the string representation of true is the number 1 and false is the number 0.

Divert(Address)

Divert to another address.

This is fully internal and will never print to the story. If encountered as a variable in the text flow it will raise an error, since it should not be there.

Float(f32)

Decimal number.

Will print as that number, although floating point numbers can print weirdly sometimes.

Int(i32)

Integer number.

Will print to that number.

String(String)

Text string.

Methods

impl Variable[src]

pub fn assign<T: Into<Variable>>(
    &mut self,
    value: T
) -> Result<(), VariableError>
[src]

Assign a new value to the variable.

Variables are type static: assigning a new variable type (variant) is not allowed. This is checked before the assignment is made and an error will be raised.

The given variable type is Into<Variable> which is implemented for all integer, floating point, boolean and string types.

Examples

Assigning a new value

let mut variable = Variable::Bool(true);

variable.assign(Variable::Bool(false));
assert_eq!(variable, Variable::Bool(false));

Inferring input type

let mut variable = Variable::Float(13.3);

variable.assign(5.0);
assert_eq!(variable, Variable::Float(5.0));

Invalid other variable type assignment

let mut variable = Variable::Int(10);

assert!(variable.assign(Variable::Bool(true)).is_err());
assert!(variable.assign(Variable::Float(5.0)).is_err());

Errors

pub fn add(&self, other: &Variable) -> Result<Variable, VariableError>[src]

Add the value of a variable to that of another.

This operation is valid for integer, floating point and string variables. Integer and floating point variables simply adds the numbers together. String variables concatenate their strings.

Integer and floating point values can be added to one another. If so, the integer is cast into a floating point number before the operation and the variable is returned as a floating point type.

Examples

Numeric addition

assert_eq!(
    Variable::Int(1).add(&Variable::Int(2)).unwrap(),
    Variable::Int(3)
);

assert_eq!(
    Variable::Float(1.0).add(&Variable::Float(2.0)).unwrap(),
    Variable::Float(3.0)
);

assert_eq!(
    Variable::Int(1).add(&Variable::Float(2.0)).unwrap(),
    Variable::Float(3.0)
);

String concatenation

let string1 = Variable::from("hi");
let string2 = Variable::from("ya!");

assert_eq!(
    string1.add(&string2).unwrap(),
    Variable::String("hiya!".to_string())
);

Errors

pub fn subtract(&self, other: &Variable) -> Result<Variable, VariableError>[src]

Subtract the value of a variable from that of another.

This operation is valid for integer and floating point variables.

Integer and floating point values can be subtracted from one another. If so, the integer is cast into a floating point number before the operation and the variable is returned as a floating point type.

Examples

assert_eq!(
    Variable::Int(1).subtract(&Variable::Int(2)).unwrap(),
    Variable::Int(-1)
);

assert_eq!(
    Variable::Float(1.0).subtract(&Variable::Float(2.0)).unwrap(),
    Variable::Float(-1.0)
);

assert_eq!(
    Variable::Int(1).subtract(&Variable::Float(2.0)).unwrap(),
    Variable::Float(-1.0)
);

Errors

pub fn multiply(&self, other: &Variable) -> Result<Variable, VariableError>[src]

Multiply the value of a variable with that of another.

This operation is valid for integer and floating point variables.

Integer and floating point values can be multiplied with one another. If so, the integer is cast into a floating point number before the operation and the variable is returned as a floating point type.

Examples

assert_eq!(
    Variable::Int(2).multiply(&Variable::Int(3)).unwrap(),
    Variable::Int(6)
);

assert_eq!(
    Variable::Float(2.0).multiply(&Variable::Float(3.0)).unwrap(),
    Variable::Float(6.0)
);

assert_eq!(
    Variable::Int(2).multiply(&Variable::Float(3.0)).unwrap(),
    Variable::Float(6.0)
);

Errors

pub fn divide(&self, other: &Variable) -> Result<Variable, VariableError>[src]

Divide the value of a variable with that of another.

This operation is valid for integer and floating point variables.

Integer and floating point values can be divided with one another. If so, the integer is cast into a floating point number before the operation and the variable is returned as a floating point type.

Examples

assert_eq!(
    Variable::Int(5).divide(&Variable::Int(2)).unwrap(),
    Variable::Int(2)
);

assert_eq!(
    Variable::Float(5.0).divide(&Variable::Float(2.0)).unwrap(),
    Variable::Float(2.5)
);

assert_eq!(
    Variable::Int(5).divide(&Variable::Float(2.0)).unwrap(),
    Variable::Float(2.5)
);

Errors

pub fn remainder(&self, other: &Variable) -> Result<Variable, VariableError>[src]

Find the remainder after dividing the value of a variable with that of another.

This operation is valid for integer and floating point variables.

Integer and floating point values can perform this operation with one another. If so, the integer is cast into a floating point number before the operation and the variable is returned as a floating point type.

Examples

assert_eq!(
    Variable::Int(5).remainder(&Variable::Int(2)).unwrap(),
    Variable::Int(1)
);

assert_eq!(
    Variable::Float(5.0).remainder(&Variable::Float(2.0)).unwrap(),
    Variable::Float(1.0)
);

assert_eq!(
    Variable::Int(5).remainder(&Variable::Float(2.0)).unwrap(),
    Variable::Float(1.0)
);

Errors

pub fn equal_to(&self, other: &Variable) -> Result<bool, VariableError>[src]

Assert whether a variable is equal to another.

Different variable variants cannot be compared to each other, with one exception: integer and floating point numbers can be compared. If an integer is compared to a floating point number the integer will be cast to a float, then the comparison is made.

Examples

Valid comparisons

assert!(Variable::Int(5).equal_to(&Variable::Int(5)).unwrap());
assert!(Variable::Int(5).equal_to(&Variable::Float(5.0)).unwrap());
assert!(!Variable::Int(5).equal_to(&Variable::Float(5.1)).unwrap());
assert!(!Variable::Bool(true).equal_to(&Variable::Bool(false)).unwrap());

Invalid comparisons between types

assert!(Variable::Int(1).equal_to(&Variable::Bool(true)).is_err());
assert!(Variable::String("1".to_string()).equal_to(&Variable::Int(1)).is_err());

Errors

pub fn greater_than(&self, other: &Variable) -> Result<bool, VariableError>[src]

Assert whether a numeric variable value is greater than that of another.

This operation is only valid for Int and Float variants. Those variants can be compared to each other. If an integer is compared to a floating point number the integer will be cast to a float, then the comparison is made.

Examples

Valid comparisons between numbers

assert!(Variable::Int(6).greater_than(&Variable::Int(5)).unwrap());
assert!(!Variable::Int(4).greater_than(&Variable::Int(5)).unwrap());
assert!(Variable::Int(5).greater_than(&Variable::Float(4.9)).unwrap());
assert!(Variable::Float(5.1).greater_than(&Variable::Int(5)).unwrap());

Invalid comparisons between non-numbers

assert!(Variable::Int(1).greater_than(&Variable::Bool(false)).is_err());
assert!(Variable::Bool(true).greater_than(&Variable::Bool(false)).is_err());
assert!(Variable::from("hiya").greater_than(&Variable::from("hi")).is_err());

Errors

pub fn less_than(&self, other: &Variable) -> Result<bool, VariableError>[src]

Assert whether a numeric variable value is less than that of another.

This operation is only valid for Int and Float variants. Those variants can be compared to each other. If an integer is compared to a floating point number the integer will be cast to a float, then the comparison is made.

Examples

Valid comparisons between numbers

assert!(Variable::Int(5).less_than(&Variable::Int(6)).unwrap());
assert!(!Variable::Int(5).less_than(&Variable::Int(4)).unwrap());
assert!(Variable::Int(5).less_than(&Variable::Float(5.1)).unwrap());
assert!(Variable::Float(4.9).less_than(&Variable::Int(5)).unwrap());

Invalid comparisons between non-numbers

assert!(Variable::Int(0).less_than(&Variable::Bool(true)).is_err());
assert!(Variable::Bool(false).less_than(&Variable::Bool(true)).is_err());
assert!(Variable::from("hi").less_than(&Variable::from("hiya")).is_err());

Errors

Trait Implementations

impl Clone for Variable[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl From<f32> for Variable[src]

impl From<f64> for Variable[src]

impl From<u8> for Variable[src]

impl From<u16> for Variable[src]

impl From<u32> for Variable[src]

impl From<u64> for Variable[src]

impl From<u128> for Variable[src]

impl From<usize> for Variable[src]

impl From<i8> for Variable[src]

impl From<i16> for Variable[src]

impl From<i32> for Variable[src]

impl From<i64> for Variable[src]

impl From<i128> for Variable[src]

impl From<isize> for Variable[src]

impl From<bool> for Variable[src]

impl<'_> From<&'_ str> for Variable[src]

impl<'_> From<&'_ String> for Variable[src]

impl From<String> for Variable[src]

impl PartialEq<Variable> for Variable[src]

impl Debug for Variable[src]

Auto Trait Implementations

Blanket Implementations

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

type Owned = T

The resulting type after obtaining ownership.

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[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.

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

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

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