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
|
use std::ops::Add;
use serde_json::{Value, json};
use serde::{Serialize, Serializer, ser::SerializeTuple};
use super::util::{clamp, ToJson};
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub struct Pos {
pub x: i32,
pub y: i32
}
impl Pos {
pub fn new(x: i32, y: i32) -> Pos {
Pos {x, y}
}
pub fn clamp(self, smaller: Pos, larger: Pos) -> Pos {
Pos {
x: clamp(self.x, smaller.x, larger.x),
y: clamp(self.y, smaller.y, larger.y)
}
}
}
impl Serialize for Pos {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut tup = serializer.serialize_tuple(2)?;
tup.serialize_element(&self.x)?;
tup.serialize_element(&self.y)?;
tup.end()
}
}
impl Add<Pos> for Pos {
type Output = Pos;
fn add(self, other: Pos) -> Pos {
Pos {
x: self.x + other.x,
y: self.y + other.y
}
}
}
impl ToJson for Pos {
fn to_json(&self) -> Value {
json!([self.x, self.y])
}
}
|