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
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::{Arc, RwLock};
use serde_json::{Error, Value};
use crate::core::button::Button;
use crate::core::{ButtonPanel, RawButtonPanel, UniqueButton};
pub mod rendering;
pub fn make_button_unique(button: Button) -> UniqueButton {
Arc::new(RwLock::new(button))
}
pub fn serialize_panel(panel: ButtonPanel) -> Result<Value, Error> {
let mut buttons = HashMap::new();
for (key, button) in panel {
buttons.insert(key, button_to_raw(&button));
}
serialize_panel_raw(buttons)
}
pub fn serialize_panel_raw(panel: RawButtonPanel) -> Result<Value, Error> {
serde_json::to_value(panel)
}
pub fn deserialize_panel(value: Value) -> Result<ButtonPanel, Error> {
Ok(make_panel_unique(deserialize_panel_raw(value)?))
}
pub fn deserialize_panel_raw(value: Value) -> Result<RawButtonPanel, Error> {
Ok(serde_json::from_value::<HashMap<u8, Button>>(value)?)
}
pub fn make_panel_unique(raw_panel: RawButtonPanel) -> ButtonPanel {
raw_panel.into_iter().map(|(key, button)| (key, make_button_unique(button))).collect()
}
pub fn panel_to_raw(panel: &ButtonPanel) -> RawButtonPanel {
panel.iter().map(|(k, b)| (*k, b.read().unwrap().deref().clone())).collect()
}
pub fn button_to_raw(button: &UniqueButton) -> Button {
button.read().unwrap().deref().clone()
}
pub fn hash_image(data: &String) -> String {
let mut hasher = DefaultHasher::new();
data.hash(&mut hasher);
hasher.finish().to_string()
}