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}
116
117impl FromStr for CheckDirection {
118    type Err = UnknownError;
119
120    fn from_str(s: &str) -> Result<Self, Self::Err> {
121        match s {
122            "v" | "V" | "Vertical" | "vertical" => Ok(CheckDirection::Vertical),
123            "h" | "H" | "Horizontal" | "horizontal" => Ok(CheckDirection::Horizontal),
124            "s" | "S" | "Sideways" | "sideways" => Ok(CheckDirection::Sideways),
125            value => Err(UnknownError {
126                name: "CheckDirection".into(),
127                value: value.into(),
128                expected: unknown_error_expected!(
129                    "v" | "V" | "Vertical" | "vertical" => "Vertical",
130                    "h" | "H" | "Horizontal" | "horizontal" => "Horizontal",
131                    "s" | "S" | "Sideways" | "sideways" => "Sideways"
132                ),
133            }),
134        }
135    }
136}
137
138#[derive(Debug, PartialEq, Eq, Clone, Copy)]
139pub enum Order {
140    Ordered,
141    Unordered,
142}
143
144impl FromStr for Order {
145    type Err = UnknownError;
146
147    fn from_str(s: &str) -> Result<Self, Self::Err> {
148        match s {
149            "o" | "O" | "Ordered" | "ordered" => Ok(Order::Ordered),
150            "u" | "U" | "Unordered" | "unordered" => Ok(Order::Unordered),
151            value => Err(UnknownError {
152                name: "CheckDirection".into(),
153                value: value.into(),
154                expected: unknown_error_expected!(
155                    "o" | "O" | "Ordered" | "ordered" => "Ordered",
156                    "u" | "U" | "Unordered" | "unordered" => "Unordered"
157                ),
158            }),
159        }
160    }
161}