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
use crate::axis_aligned_rectangle::AxisAlignedRectangle;
use crate::component::Component;
use crate::dividing::Dividing;
use crate::point::Point;
use crate::rectangle::{Rectangle, RectangleSize};
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen;
use wasm_bindgen::prelude::*;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub struct JSRect {
    pub x: f32,
    pub y: f32,
    pub w: f32,
    pub h: f32,
}

#[wasm_bindgen]
pub fn dividing(
    rect: JsValue,
    weights: &[f32],
    aspect_ratio: f32,
    vertical_first: bool,
    boustrophedron: bool,
) -> Result<JsValue, JsValue> {
    let Ok(rect) = serde_wasm_bindgen::from_value::<JSRect>(rect) else {
        return Err(JsValue::from_str("failed to parse rect"));
    };
    let rect =
        AxisAlignedRectangle::new(&Point::new(rect.x, rect.y), &Rectangle::new(rect.w, rect.h));
    let rects = match vertical_first {
        true => {
            rect.divide_vertical_then_horizontal_with_weights(weights, aspect_ratio, boustrophedron)
        }
        false => {
            rect.divide_horizontal_then_vertical_with_weights(weights, aspect_ratio, boustrophedron)
        }
    };

    let js_rects = rects
        .iter()
        .map(|rect| JSRect {
            x: rect.x(),
            y: rect.y(),
            w: rect.width(),
            h: rect.height(),
        })
        .collect::<Vec<_>>();

    serde_wasm_bindgen::to_value(&js_rects).map_err(|e| e.into())
}

#[cfg(test)]
mod tests {
    use wasm_bindgen_test::wasm_bindgen_test;

    use super::*;

    #[wasm_bindgen_test]
    fn test_basis() {
        let result = dividing(
            serde_wasm_bindgen::to_value(&JSRect {
                x: 0.0,
                y: 0.0,
                w: 100.0,
                h: 100.0,
            })
            .unwrap(),
            &[1.0, 1.0],
            1.0,
            true,
            false,
        )
        .unwrap();
        let result: Vec<JSRect> = serde_wasm_bindgen::from_value(result).unwrap();
        assert_eq!(
            result,
            vec![
                JSRect {
                    x: 0.0,
                    y: 0.0,
                    w: 100.0,
                    h: 50.0
                },
                JSRect {
                    x: 0.0,
                    y: 50.0,
                    w: 100.0,
                    h: 50.0
                }
            ]
        );
    }
}