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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::path::PathBuf;
use serde::Serialize;
use crate::{format, Size};
#[derive(Debug, Serialize, Clone, Copy)]
pub struct Rect {
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
}
#[derive(Debug, Serialize, Clone)]
pub struct Entry {
pub name: String,
pub path: String,
pub rect: Rect,
}
#[derive(Debug, Serialize)]
pub struct Dictionary {
width: u32,
height: u32,
items: Vec<Entry>,
}
impl Dictionary {
pub fn new(size: Size) -> Self {
Self {
width: size.width,
height: size.height,
items: vec![],
}
}
#[allow(clippy::ptr_arg)]
pub fn record(&mut self, path: &PathBuf, rect: &crate::Rect) -> Entry {
let name = path
.file_stem()
.or_else(|| path.file_name())
.unwrap_or(path.as_os_str())
.to_string_lossy()
.to_string();
let entry = Entry {
name: self.pick_name(&name),
path: path.to_string_lossy().to_string(),
rect: self::Rect {
x: rect.origin.x,
y: rect.origin.y,
width: rect.size.width,
height: rect.size.height,
},
};
self.items.push(entry.clone());
entry
}
fn pick_name(&self, name: &str) -> String {
let exists = |name: &str| self.items.iter().any(|v| v.name == name);
if !exists(name) {
return String::from(name);
}
for i in 0.. {
let new_name = format!("{}-{}", name, i);
if !exists(&new_name) {
return new_name;
}
}
String::from(name)
}
pub fn save(&self, name: &str, dict: format::DictionaryFormat) -> anyhow::Result<()> {
let content = match dict {
format::DictionaryFormat::Toml => toml::to_vec(self)?,
format::DictionaryFormat::Json => serde_json::to_vec_pretty(self)?,
format::DictionaryFormat::Yaml => serde_yaml::to_string(self)?.into_bytes(),
format::DictionaryFormat::Ron => ron::to_string(self)?.into_bytes(),
};
std::fs::write(format!("{}.{}", name, dict.ext()), content)?;
Ok(())
}
}