JSONDrawAction

Enum JSONDrawAction 

Source
pub enum JSONDrawAction {
    DrawForward {
        dist: f64,
    },
    MoveForward {
        dist: f64,
    },
    RotateCW {
        numerator: isize,
        denominator: usize,
    },
    Push {
        stack: usize,
    },
    Pop {
        stack: usize,
    },
    Null,
}
Expand description

Represents a DrawAction, coming from JSON deserialization.

§Examples

use std::collections::HashMap;
use std::f64::consts::PI;

use l_system_fractals::parse_files::JSONDrawAction;
use l_system_fractals::rules::DrawAction;
use l_system_fractals::num_validity::AlmostEq;

let json_string = r#"{
                        "A": {"type": "RotateCW", "numerator": 1, "denominator": 4},
                        "B": {"type": "Push", "stack": 0}
                    }"#;
let actions: HashMap<char, JSONDrawAction> = serde_json::from_str(&json_string).unwrap();

let action_a: DrawAction = actions.get(&'A')
    .unwrap()
    .to_owned()
    .try_into()
    .unwrap();
assert!(
    action_a.almost_eq(
        &DrawAction::RotateCW(0.25 * PI),
        0.001
    )
);

let action_b: DrawAction = actions.get(&'B')
    .unwrap()
    .to_owned()
    .try_into()
    .unwrap();
assert!(
    action_b.almost_eq(
        &DrawAction::Push(0),
        0.001
    )
);

Variants§

§

DrawForward

Deserializable version of DrawAction::DrawForward.

§Example

use std::collections::HashMap;

use l_system_fractals::parse_files::JSONDrawAction;
use l_system_fractals::rules::DrawAction;
use l_system_fractals::num_validity::AlmostEq;

let json_string = r#"{ "type": "DrawForward", "dist": 1.5 }"#;
let json_df: JSONDrawAction = serde_json::from_str(json_string).unwrap();
let df: DrawAction = json_df.try_into().unwrap();
assert!(df.almost_eq(&DrawAction::DrawForward(1.5), 0.001));

Fields

§dist: f64

Distance to draw forward.

§

MoveForward

Deserializable version of DrawAction::MoveForward.

§Example

use std::collections::HashMap;

use l_system_fractals::parse_files::JSONDrawAction;
use l_system_fractals::rules::DrawAction;
use l_system_fractals::num_validity::AlmostEq;

let json_string = r#"{ "type": "MoveForward", "dist": 1.5 }"#;
let json_mf: JSONDrawAction = serde_json::from_str(json_string).unwrap();
let mf: DrawAction = json_mf.try_into().unwrap();
assert!(mf.almost_eq(&DrawAction::MoveForward(1.5), 0.001));

Fields

§dist: f64

Distance to move forward.

§

RotateCW

Deserializable version of DrawAction::RotateCW.

§Example

use std::collections::HashMap;
use std::f64::consts::PI;

use l_system_fractals::parse_files::JSONDrawAction;
use l_system_fractals::rules::DrawAction;
use l_system_fractals::num_validity::AlmostEq;

let json_string = r#"{ "type": "RotateCW", "numerator": 1, "denominator": 4 }"#;
let json_rc: JSONDrawAction = serde_json::from_str(json_string).unwrap();
let rc: DrawAction = json_rc.try_into().unwrap();
assert!(rc.almost_eq(&DrawAction::RotateCW(0.25 * PI), 0.001));

Fields

§numerator: isize

Numerator of the fraction of π to rotate.

§denominator: usize

Denominator of the fraction of π to rotate.

§

Push

Deserializable version of DrawAction::Push.

§Example

use std::collections::HashMap;

use l_system_fractals::parse_files::JSONDrawAction;
use l_system_fractals::rules::DrawAction;

let json_string = r#"{ "type": "Push", "stack": 3 }"#;
let json_p: JSONDrawAction = serde_json::from_str(json_string).unwrap();
let p: DrawAction = json_p.try_into().unwrap();
assert_eq!(p, DrawAction::Push(3));

Fields

§stack: usize

Identifier for the stack to push onto.

§

Pop

Deserializable version of DrawAction::Pop.

§Example

use std::collections::HashMap;

use l_system_fractals::parse_files::JSONDrawAction;
use l_system_fractals::rules::DrawAction;

let json_string = r#"{ "type": "Pop", "stack": 3 }"#;
let json_p: JSONDrawAction = serde_json::from_str(json_string).unwrap();
let p: DrawAction = json_p.try_into().unwrap();
assert_eq!(p, DrawAction::Pop(3));

Fields

§stack: usize

Identifier for the stack to pop from.

§

Null

Deserializable version of DrawAction::Null.

§Example

use std::collections::HashMap;

use l_system_fractals::parse_files::JSONDrawAction;
use l_system_fractals::rules::DrawAction;

let json_string = r#"{ "type": "Null"}"#;
let json_n: JSONDrawAction = serde_json::from_str(json_string).unwrap();
let n: DrawAction = json_n.try_into().unwrap();
assert_eq!(n, DrawAction::Null);

Trait Implementations§

Source§

impl AlmostEq for JSONDrawAction

Source§

fn almost_eq(&self, other: &Self, epsilon: f64) -> bool

Returns true if the distance between objects is less than epsilon.
Source§

impl Clone for JSONDrawAction

Source§

fn clone(&self) -> JSONDrawAction

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JSONDrawAction

Source§

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

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

impl<'de> Deserialize<'de> for JSONDrawAction

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for JSONDrawAction

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<JSONDrawAction> for DrawAction

Source§

type Error = LSystemError

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

fn try_from(j: JSONDrawAction) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for JSONDrawAction

Source§

impl StructuralPartialEq for JSONDrawAction

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

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,