fish_oxide/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#![allow(dead_code, unused)]
use clap::ValueEnum;
use custom_rand::{seed_rand, str_to_seed};
use draw::{cleanup, draw_svg, fish, reframe};

pub use crate::params::generate_params;
pub use crate::params::Params;

pub mod custom_rand;
pub mod draw;
pub mod geometry;
pub mod hershey;
pub mod params;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum Format {
    Svg,
    Json,
    Smil,
    Csv,
    Ps,
}

const DEFAULT_SEED: &str = "x11:11 make a fish";

pub fn generate(
    format: Format,
    seed: String,
    caption: Option<String>,
    _speed_opt: Option<f64>,
    pad_opt: Option<f64>,
    fish_opts: Option<params::Params>,
) -> String {
    seed_rand(str_to_seed(seed.to_owned()).into());

    // let mut speed = 0.005 / speed_opt.unwrap_or(1.);
    let drawing = fish(fish_opts.unwrap_or_else(generate_params));
    let polylines = cleanup(reframe(drawing, pad_opt, Some(caption.unwrap_or(seed))));

    match format {
        Format::Svg => draw_svg(polylines),
        Format::Json => serde_json::to_string(&polylines).unwrap(),
        Format::Smil => {
            todo!("Smil format is not yet supported")

            // draw_svg_anim(polylines, speed)
        }
        Format::Csv => polylines
            .iter()
            .map(|x| {
                x.iter()
                    .map(|z| format!("{:?}", *z))
                    .collect::<Vec<String>>()
                    .join(",")
            })
            .collect::<Vec<String>>()
            .join("\n"),
        Format::Ps => {
            todo!("Ps format is not yet supported")
            // draw_ps(polylines)
        }
    }
}

pub fn generate_svg() -> String {
    generate(
        Format::Svg,
        DEFAULT_SEED.to_string(),
        Some(DEFAULT_SEED.to_string()),
        None,
        Some(20.),
        None,
    )
}

pub fn generate_json() -> String {
    generate(
        Format::Json,
        DEFAULT_SEED.to_string(),
        Some(DEFAULT_SEED.to_string()),
        None,
        Some(20.),
        None,
    )
}

pub fn generate_csv() -> String {
    generate(
        Format::Csv,
        DEFAULT_SEED.to_string(),
        Some(DEFAULT_SEED.to_string()),
        None,
        Some(20.),
        None,
    )
}