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/// Sum of absolute differences over the matched window, lower is a better match.
96#[derive(Debug, Clone)]
97pub struct OverlapScore {
98    pub score: u64,
99    pub flipped: bool,
100    pub position: Position,
101}
102
103impl Deref for OverlapScore {
104    type Target = Position;
105
106    fn deref(&self) -> &Self::Target {
107        &self.position
108    }
109}
110
111#[derive(Debug, PartialEq, Eq, Clone, Copy)]
112pub enum CheckDirection {
113    Vertical,
114    Horizontal,
115    Sideways,
116    SidewaysRight,
117    SidewaysLeft,
118}
119
120impl FromStr for CheckDirection {
121    type Err = UnknownError;
122
123    fn from_str(s: &str) -> Result<Self, Self::Err> {
124        match s {
125            "v" | "V" | "Vertical" | "vertical" => Ok(CheckDirection::Vertical),
126            "h" | "H" | "Horizontal" | "horizontal" => Ok(CheckDirection::Horizontal),
127            "s" | "S" | "Sideways" | "sideways" => Ok(CheckDirection::Sideways),
128            "sr" | "SR" | "SidewaysRight" | "sidewaysRight" => Ok(CheckDirection::SidewaysRight),
129            "sl" | "SL" | "SidewaysLeft" | "sidewaysLeft" => Ok(CheckDirection::SidewaysLeft),
130            value => Err(UnknownError {
131                name: "CheckDirection".into(),
132                value: value.into(),
133                expected: unknown_error_expected!(
134                    "v" | "V" | "Vertical" | "vertical" => "Vertical",
135                    "h" | "H" | "Horizontal" | "horizontal" => "Horizontal",
136                    "s" | "S" | "Sideways" | "sideways" => "Sideways",
137                    "sr" | "SR" | "SidewaysRight" | "sidewaysRight" => "SidewaysRight",
138                    "sl" | "SL" | "SidewaysLeft" | "sidewaysLeft" => "SidewaysLeft"
139                ),
140            }),
141        }
142    }
143}
144
145#[derive(Debug, PartialEq, Eq, Clone, Copy)]
146pub enum Order {
147    Ordered,
148    Unordered,
149}
150
151impl FromStr for Order {
152    type Err = UnknownError;
153
154    fn from_str(s: &str) -> Result<Self, Self::Err> {
155        match s {
156            "o" | "O" | "Ordered" | "ordered" => Ok(Order::Ordered),
157            "u" | "U" | "Unordered" | "unordered" => Ok(Order::Unordered),
158            value => Err(UnknownError {
159                name: "Order".into(),
160                value: value.into(),
161                expected: unknown_error_expected!(
162                    "o" | "O" | "Ordered" | "ordered" => "Ordered",
163                    "u" | "U" | "Unordered" | "unordered" => "Unordered"
164                ),
165            }),
166        }
167    }
168}