JSONInput

Enum JSONInput 

Source
pub enum JSONInput {
    PlotOne {
        rules: JSONDrawRules,
        iteration: usize,
        plot_param: PlotParameters,
        filename: String,
        comment: Option<String>,
    },
    PlotMany {
        rules: JSONDrawRules,
        iterations: Vec<usize>,
        row_len: usize,
        plot_param: PlotParameters,
        filename: String,
        comment: Option<String>,
    },
    OutputStrings {
        rules: HashMap<char, String>,
        start: String,
        iteration: usize,
        filename: String,
        comment: Option<String>,
    },
}
Expand description

Represents the JSON input for an input file.

This provides the details of the L-system as well as instructions for printing.

The three variants of this enum correspond to the three commands (and are tagged using the command field in the JSON): PlotOne, PlotMany, and OutputStrings.

Variants§

§

PlotOne

Produces a single plot, showing the specified iteration of the fractal.

§Example

use std::collections::HashMap;

use l_system_fractals::parse_files::{
    JSONDrawAction, JSONInput, JSONDrawRules
};
use l_system_fractals::paths::PlotParameters;
use l_system_fractals::num_validity::AlmostEq;

let json_string = r#"{
  "command": "PlotOne",
  "rules": {
    "type": "Advanced",
    "replacement": { "A": "A+BA-AA-A-AA+B+AA-BA+AA+A+AA-B-AAA", "B": "BBBBBB" },
    "draw_step_sizes": { "A": 1.0 },
    "move_step_sizes": { "B": 1.0 },
    "angle_numerator": 1,
    "angle_denominator": 2,
    "start": "A-A-A-A-"
  },
  "iteration": 3,
  "plot_param": {
    "width": 700.0,
    "height": 700.0,
    "border": 50.0,
    "fill": "none",
    "stroke": "magenta",
    "stroke_width": 1
  },
  "filename": "islands.svg",
  "comment": "Benoit B. Mandelbrot (1983), _The Fractal Geometry of Nautre (Updated and Agumented)_, New York: W.H. Freeman and Company, p. 121"
}"#;
let json_input1: JSONInput = serde_json::from_str(json_string).unwrap();

let rules = JSONDrawRules::Advanced {
    replacement: HashMap::from([
            ('A', "A+BA-AA-A-AA+B+AA-BA+AA+A+AA-B-AAA".into()),
            ('B', "BBBBBB".into())
        ]),
    draw_step_sizes: HashMap::from([('A', 1.0)]),
    move_step_sizes: HashMap::from([('B', 1.0)]),
    angle_numerator: 1,
    angle_denominator: 2,
    start: "A-A-A-A-".into()
};
let plot_param = PlotParameters {
  width: 700.0,
  height: 700.0,
  border: 50.0,
  fill: "none".into(),
  stroke: "magenta".into(),
  stroke_width: 1.0,
  background: None
};

let json_input2 = JSONInput::PlotOne {
    rules,
    iteration: 3,
    plot_param,
    filename: "islands.svg".into(),
    comment: Some("Benoit B. Mandelbrot (1983), _The Fractal Geometry of Nautre".to_string()
            + " (Updated and Agumented)_, New York: W.H. Freeman and Company, p. 121")
};
assert!(json_input1.almost_eq(&json_input2, 0.001));

Fields

§rules: JSONDrawRules

The rules for the L-system

§iteration: usize

The iteration to display

§plot_param: PlotParameters

The parameters for the output SVG file

§filename: String

The filename to which the SVG will be saved

§comment: Option<String>

An optional comment

§

PlotMany

Produces multiple plots, showing the specified iterations of the fractal.

§Example

use std::collections::HashMap;

use l_system_fractals::parse_files::{
    JSONDrawAction, JSONInput, JSONDrawRules
};
use l_system_fractals::paths::PlotParameters;
use l_system_fractals::num_validity::AlmostEq;

let json_string = r#"{
  "command": "PlotMany",
  "rules": {
    "type": "Advanced",
    "replacement": { "A": "A+BA-AA-A-AA+B+AA-BA+AA+A+AA-B-AAA", "B": "BBBBBB" },
    "draw_step_sizes": { "A": 1.0 },
    "move_step_sizes": { "B": 1.0 },
    "angle_numerator": 1,
    "angle_denominator": 2,
    "start": "A-A-A-A-"
  },
  "iterations": [0, 1, 2, 3],
  "row_len": 2,
  "plot_param": {
    "width": 700.0,
    "height": 700.0,
    "border": 50.0,
    "fill": "none",
    "stroke": "magenta",
    "stroke_width": 1
  },
  "filename": "islands.svg",
  "comment": "Benoit B. Mandelbrot (1983), _The Fractal Geometry of Nautre (Updated and Agumented)_, New York: W.H. Freeman and Company, p. 121"
}"#;
let json_input1: JSONInput = serde_json::from_str(json_string).unwrap();

let rules = JSONDrawRules::Advanced {
    replacement: HashMap::from([
            ('A', "A+BA-AA-A-AA+B+AA-BA+AA+A+AA-B-AAA".into()),
            ('B', "BBBBBB".into())
        ]),
    draw_step_sizes: HashMap::from([('A', 1.0)]),
    move_step_sizes: HashMap::from([('B', 1.0)]),
    angle_numerator: 1,
    angle_denominator: 2,
    start: "A-A-A-A-".into()
};
let plot_param = PlotParameters {
  width: 700.0,
  height: 700.0,
  border: 50.0,
  fill: "none".into(),
  stroke: "magenta".into(),
  stroke_width: 1.0,
  background: None
};

let json_input2 = JSONInput::PlotMany {
    rules,
    iterations: vec![0, 1, 2, 3],
    row_len: 2,
    plot_param,
    filename: "islands.svg".into(),
    comment: Some("Benoit B. Mandelbrot (1983), _The Fractal Geometry of Nautre".to_string()
            + " (Updated and Agumented)_, New York: W.H. Freeman and Company, p. 121")
};
assert!(json_input1.almost_eq(&json_input2, 0.001));

Fields

§rules: JSONDrawRules

The rules for the L-system

§iterations: Vec<usize>

The iterations to display

§row_len: usize

Number of plots in a row

§plot_param: PlotParameters

The parameters for the output SVG file

§filename: String

The filename to which the SVG will be saved

§comment: Option<String>

An optional comment

§

OutputStrings

Does not produce any plots, but rather just performs the string replacement rules.

§Example

use std::collections::HashMap;

use l_system_fractals::parse_files::JSONInput;

let json_string = r#"{
     "command": "OutputStrings",
     "rules": { "A": "AB", "B": "BAB" },
     "start": "A",
     "iteration": 2,
     "filename": "text_output.txt"
}"#;

let json_input1: JSONInput = serde_json::from_str(json_string).unwrap();

let json_input2 = JSONInput::OutputStrings {
    rules: HashMap::from([('A', "AB".into()), ('B', "BAB".into())]),
    start: "A".into(),
    iteration: 2,
    filename: "text_output.txt".into(),
    comment: None
};

Fields

§rules: HashMap<char, String>

The replacement rules

§start: String

The initial string

§iteration: usize

The iteration to calculate

§filename: String

The name of the file to gernerate with the output

§comment: Option<String>

An optional comment

Implementations§

Source§

impl JSONInput

Source

pub fn run(&self) -> Result<String, LSystemError>

Runs the command specified by the input.

Trait Implementations§

Source§

impl AlmostEq for JSONInput

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 JSONInput

Source§

fn clone(&self) -> JSONInput

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 JSONInput

Source§

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

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

impl<'de> Deserialize<'de> for JSONInput

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 JSONInput

Source§

fn eq(&self, other: &JSONInput) -> 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 StructuralPartialEq for JSONInput

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>,