Skip to main content

wonfy_tools/tool/stitcher/
params.rs

1use std::{
2    ops::{Add, AddAssign, Deref},
3    str::FromStr,
4};
5
6use crate::error::{UnknownError, unknown_error_expected};
7
8#[derive(Debug, Clone)]
9pub enum MatchMode {
10    Normal,
11    Edges,
12}
13
14impl FromStr for MatchMode {
15    type Err = UnknownError;
16
17    fn from_str(s: &str) -> Result<Self, Self::Err> {
18        match s {
19            "n" | "N" | "Normal" | "normal" => Ok(MatchMode::Normal),
20            "e" | "E" | "Edges" | "edges" => Ok(MatchMode::Edges),
21            value => Err(UnknownError {
22                name: "MatchMode".into(),
23                value: value.into(),
24                expected: unknown_error_expected!(
25                    "n" | "N" | "Normal" | "normal" => "Normal",
26                    "e" | "E" | "Edges" | "edges" => "Edges"
27                ),
28            }),
29        }
30    }
31}
32
33#[derive(Debug, Default, Clone)]
34pub struct Position {
35    pub x: i32,
36    pub y: i32,
37}
38
39impl Add for Position {
40    type Output = Self;
41    fn add(self, other: Self) -> Self {
42        Position {
43            x: self.x + other.x,
44            y: self.y + other.y,
45        }
46    }
47}
48
49impl<'a> Add<&'a Position> for Position {
50    type Output = Self;
51    fn add(self, other: &'a Position) -> Self {
52        Position {
53            x: self.x + other.x,
54            y: self.y + other.y,
55        }
56    }
57}
58
59impl AddAssign for Position {
60    fn add_assign(&mut self, rhs: Self) {
61        self.x += rhs.x;
62        self.y += rhs.y;
63    }
64}
65
66impl<'a> AddAssign<&'a Position> for Position {
67    fn add_assign(&mut self, rhs: &'a Position) {
68        self.x += rhs.x;
69        self.y += rhs.y;
70    }
71}
72
73#[cfg(target_arch = "wasm32")]
74impl Position {
75    pub fn to_json(self) -> js_sys::Object {
76        let obj = js_sys::Object::new();
77
78        js_sys::Reflect::set(
79            &obj,
80            &wasm_bindgen::JsValue::from_str("x"),
81            &wasm_bindgen::JsValue::from(self.x),
82        )
83        .ok();
84        js_sys::Reflect::set(
85            &obj,
86            &wasm_bindgen::JsValue::from_str("y"),
87            &wasm_bindgen::JsValue::from(self.y),
88        )
89        .ok();
90
91        obj
92    }
93}
94
95#[derive(Debug, Default)]
96pub struct OverlapScore {
97    pub score: u64,
98    pub flipped: bool,
99    pub position: Position,
100}
101
102impl Deref for OverlapScore {
103    type Target = Position;
104
105    fn deref(&self) -> &Self::Target {
106        &self.position
107    }
108}
109
110#[derive(Debug, PartialEq, Eq, Clone, Copy)]
111pub enum CheckDirection {
112    Vertical,
113    Horizontal,
114    Sideways,
115    SidewaysRight,
116    SidewaysLeft,
117}
118
119impl FromStr for CheckDirection {
120    type Err = UnknownError;
121
122    fn from_str(s: &str) -> Result<Self, Self::Err> {
123        match s {
124            "v" | "V" | "Vertical" | "vertical" => Ok(CheckDirection::Vertical),
125            "h" | "H" | "Horizontal" | "horizontal" => Ok(CheckDirection::Horizontal),
126            "s" | "S" | "Sideways" | "sideways" => Ok(CheckDirection::Sideways),
127            "sr" | "SR" | "SidewaysRight" | "sidewaysRight" => Ok(CheckDirection::SidewaysRight),
128            "sl" | "SL" | "SidewaysLeft" | "sidewaysLeft" => Ok(CheckDirection::SidewaysLeft),
129            value => Err(UnknownError {
130                name: "CheckDirection".into(),
131                value: value.into(),
132                expected: unknown_error_expected!(
133                    "v" | "V" | "Vertical" | "vertical" => "Vertical",
134                    "h" | "H" | "Horizontal" | "horizontal" => "Horizontal",
135                    "s" | "S" | "Sideways" | "sideways" => "Sideways",
136                    "sr" | "SR" | "SidewaysRight" | "sidewaysRight" => "SidewaysRight",
137                    "sl" | "SL" | "SidewaysLeft" | "sidewaysLeft" => "SidewaysLeft"
138                ),
139            }),
140        }
141    }
142}
143
144#[derive(Debug, PartialEq, Eq, Clone, Copy)]
145pub enum Order {
146    Ordered,
147    Unordered,
148}
149
150impl FromStr for Order {
151    type Err = UnknownError;
152
153    fn from_str(s: &str) -> Result<Self, Self::Err> {
154        match s {
155            "o" | "O" | "Ordered" | "ordered" => Ok(Order::Ordered),
156            "u" | "U" | "Unordered" | "unordered" => Ok(Order::Unordered),
157            value => Err(UnknownError {
158                name: "Order".into(),
159                value: value.into(),
160                expected: unknown_error_expected!(
161                    "o" | "O" | "Ordered" | "ordered" => "Ordered",
162                    "u" | "U" | "Unordered" | "unordered" => "Unordered"
163                ),
164            }),
165        }
166    }
167}