4 use serde_derive::Deserialize; |
4 use serde_derive::Deserialize; |
5 |
5 |
6 use std::collections::hash_map::HashMap; |
6 use std::collections::hash_map::HashMap; |
7 |
7 |
8 #[derive(Debug, Deserialize)] |
8 #[derive(Debug, Deserialize)] |
9 #[serde(remote = "EdgeDescription")] |
|
10 pub struct EdgeDesc { |
|
11 pub name: String, |
|
12 pub reversed: Option<bool>, |
|
13 pub symmetrical: Option<bool>, |
|
14 } |
|
15 |
|
16 #[derive(Debug, Deserialize)] |
|
17 #[serde(remote = "EdgesDescription")] |
|
18 pub struct EdgesDesc { |
|
19 #[serde(with = "EdgeDesc")] |
|
20 pub top: EdgeDescription, |
|
21 #[serde(with = "EdgeDesc")] |
|
22 pub right: EdgeDescription, |
|
23 #[serde(with = "EdgeDesc")] |
|
24 pub bottom: EdgeDescription, |
|
25 #[serde(with = "EdgeDesc")] |
|
26 pub left: EdgeDescription, |
|
27 } |
|
28 |
|
29 #[derive(Debug, Deserialize)] |
|
30 #[serde(remote = "TileDescription")] |
|
31 pub struct TileDesc { |
9 pub struct TileDesc { |
32 pub name: String, |
10 pub name: String, |
33 #[serde(with = "EdgesDesc")] |
11 pub edges: [String; 4], |
34 pub edges: EdgesDescription, |
|
35 pub is_negative: Option<bool>, |
12 pub is_negative: Option<bool>, |
36 pub can_flip: Option<bool>, |
13 pub can_flip: Option<bool>, |
37 pub can_mirror: Option<bool>, |
14 pub can_mirror: Option<bool>, |
38 pub can_rotate90: Option<bool>, |
15 pub can_rotate90: Option<bool>, |
39 pub can_rotate180: Option<bool>, |
16 pub can_rotate180: Option<bool>, |
40 pub can_rotate270: Option<bool>, |
17 pub can_rotate270: Option<bool>, |
41 } |
18 } |
42 |
19 |
43 #[derive(Debug, Deserialize)] |
20 #[derive(Debug, Deserialize)] |
44 pub struct TileDescriptionHelper(#[serde(with = "TileDesc")] TileDescription); |
|
45 #[derive(Debug, Deserialize)] |
|
46 pub struct EdgeDescriptionHelper(#[serde(with = "EdgeDesc")] EdgeDescription); |
|
47 |
|
48 #[derive(Debug, Deserialize)] |
|
49 pub struct ComplexEdgeDesc { |
21 pub struct ComplexEdgeDesc { |
50 pub begin: Option<EdgeDescriptionHelper>, |
22 pub begin: Option<String>, |
51 pub fill: Option<EdgeDescriptionHelper>, |
23 pub fill: Option<String>, |
52 pub end: Option<EdgeDescriptionHelper>, |
24 pub end: Option<String>, |
53 } |
25 } |
54 #[derive(Debug, Deserialize)] |
26 #[derive(Debug, Deserialize)] |
55 pub struct NonStrictComplexEdgesDesc { |
27 pub struct NonStrictComplexEdgesDesc { |
56 pub top: Option<ComplexEdgeDesc>, |
28 pub top: Option<ComplexEdgeDesc>, |
57 pub right: Option<ComplexEdgeDesc>, |
29 pub right: Option<ComplexEdgeDesc>, |
67 pub is_negative: bool, |
39 pub is_negative: bool, |
68 pub put_girders: bool, |
40 pub put_girders: bool, |
69 pub max_hedgehogs: u8, |
41 pub max_hedgehogs: u8, |
70 pub wrap: bool, |
42 pub wrap: bool, |
71 pub edges: Option<NonStrictComplexEdgesDesc>, |
43 pub edges: Option<NonStrictComplexEdgesDesc>, |
72 pub tiles: Vec<TileDescriptionHelper>, |
44 pub tiles: Vec<TileDesc>, |
73 } |
45 } |
74 |
46 |
75 #[derive(Debug, Deserialize)] |
47 #[derive(Debug, Deserialize)] |
76 pub struct TemplateCollectionDesc { |
48 pub struct TemplateCollectionDesc { |
77 pub templates: Vec<TemplateDesc>, |
49 pub templates: Vec<TemplateDesc>, |
78 pub template_types: HashMap<String, Vec<usize>>, |
50 pub template_types: HashMap<String, Vec<usize>>, |
79 } |
51 } |
80 |
52 |
81 impl From<&TemplateDesc> for TemplateDescription { |
53 impl From<&TemplateDesc> for TemplateDescription { |
82 fn from(desc: &TemplateDesc) -> Self { |
54 fn from(desc: &TemplateDesc) -> Self { |
83 let [top, right, bottom, left]:[Option<ComplexEdgeDescription>; 4] = if let Some(edges) = &desc.edges { |
55 let [top, right, bottom, left]: [Option<ComplexEdgeDescription>; 4] = |
84 [ |
56 if let Some(edges) = &desc.edges { |
85 &edges.top, |
57 [&edges.top, &edges.right, &edges.bottom, &edges.left] |
86 &edges.right, |
58 .map(|e| e.as_ref().map(Into::into)) |
87 &edges.bottom, |
59 } else { |
88 &edges.left, |
60 [None, None, None, None] |
89 ] |
61 }; |
90 .map(|e| e.as_ref().map(Into::into)) |
|
91 } else { |
|
92 [None, None, None, None] |
|
93 }; |
|
94 |
62 |
95 Self { |
63 Self { |
96 size: Size::new(desc.width, desc.height), |
64 size: Size::new(desc.width, desc.height), |
97 tiles: desc |
65 tiles: desc.tiles.iter().map(|t| t.into()).collect(), |
98 .tiles |
|
99 .iter() |
|
100 .map(|TileDescriptionHelper(t)| t.clone()) |
|
101 .collect(), |
|
102 wrap: desc.wrap, |
66 wrap: desc.wrap, |
103 can_invert: desc.can_invert, |
67 can_invert: desc.can_invert, |
104 is_negative: desc.is_negative, |
68 is_negative: desc.is_negative, |
105 edges: NonStrictComplexEdgesDescription { |
69 edges: NonStrictComplexEdgesDescription { |
106 top, |
70 top, |
110 }, |
74 }, |
111 } |
75 } |
112 } |
76 } |
113 } |
77 } |
114 |
78 |
|
79 impl From<&TileDesc> for TileDescription { |
|
80 fn from(desc: &TileDesc) -> Self { |
|
81 let [top, right, bottom, left]: [EdgeDescription; 4] = desc.edges.clone().map(|e| e.into()); |
|
82 |
|
83 Self { |
|
84 name: desc.name.clone(), |
|
85 edges: EdgesDescription { |
|
86 top, |
|
87 right, |
|
88 bottom, |
|
89 left, |
|
90 }, |
|
91 is_negative: desc.is_negative, |
|
92 can_flip: desc.can_flip, |
|
93 can_mirror: desc.can_mirror, |
|
94 can_rotate90: desc.can_rotate90, |
|
95 can_rotate180: desc.can_rotate180, |
|
96 can_rotate270: desc.can_rotate270, |
|
97 } |
|
98 } |
|
99 } |
|
100 |
115 impl From<&ComplexEdgeDesc> for ComplexEdgeDescription { |
101 impl From<&ComplexEdgeDesc> for ComplexEdgeDescription { |
116 fn from(value: &ComplexEdgeDesc) -> Self { |
102 fn from(value: &ComplexEdgeDesc) -> Self { |
117 Self { |
103 Self { |
118 begin: value.begin.as_ref().map(|EdgeDescriptionHelper(e)| e.clone()), |
104 begin: value.begin.as_ref().map(|e| e.into()), |
119 fill: value.fill.as_ref().map(|EdgeDescriptionHelper(e)| e.clone()), |
105 fill: value.fill.as_ref().map(|e| e.into()), |
120 end: value.end.as_ref().map(|EdgeDescriptionHelper(e)| e.clone()), |
106 end: value.end.as_ref().map(|e| e.into()), |
121 } |
107 } |
122 } |
108 } |
123 } |
109 } |