Repository navigation
Expand file tree
/
Copy pathresource.rs
More file actions
177 lines (157 loc) · 5.47 KB
/
Copy pathresource.rs
File metadata and controls
177 lines (157 loc) · 5.47 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use super::Parsable;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Resource {
pub images: Vec<ResourceItem>,
pub sounds: Vec<ResourceItem>,
pub fonts: Vec<ResourceItem>,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ResourceSection {
Images,
Sounds,
Fonts,
None,
}
impl Parsable for Resource {
type ParseError = ResourceParseError;
type ReconstructionError = ResourceReconstructionError;
fn parse(decrypted_content: &str) -> Result<Self, Self::ParseError> {
let mut iterator = decrypted_content.split("\n");
let mut cur_section = ResourceSection::None;
let mut result = Resource {
images: vec![],
sounds: vec![],
fonts: vec![],
};
let mut line_count = 0u32;
loop {
let line = iterator.next();
if line.is_none() {
break;
}
let line = line.unwrap();
if line == "@images" {
cur_section = ResourceSection::Images;
} else if line == "@sounds" {
cur_section = ResourceSection::Sounds;
} else if line == "@fonts" {
cur_section = ResourceSection::Fonts
} else if cur_section != ResourceSection::None {
// parse the resource item if the line isn't empty
if line.is_empty() {
break;
}
let resource_item = ResourceItem::parse(line).map_err(|err| {
ResourceParseError::ResourceItemParseError {
source: err,
section: cur_section,
line: line_count,
}
})?;
// push the resource item to the appropriate section
if cur_section == ResourceSection::Images {
&mut result.images
} else if cur_section == ResourceSection::Sounds {
&mut result.sounds
} else if cur_section == ResourceSection::Fonts {
&mut result.fonts
} else {
break;
}
.push(resource_item)
}
line_count += 1;
}
Ok(result)
}
fn reconstruct(&self) -> Result<String, Self::ReconstructionError> {
Ok(format!(
"@images\n{}@sounds\n{}@fonts\n{}",
// a macro might be handy
self.images
.iter()
.try_fold(String::new(), |acc, i| Ok(format!(
"{}{}\n",
acc,
i.reconstruct().map_err(|err| {
ResourceReconstructionError::ResourceItemReconstructionError {
source: err,
section: ResourceSection::Images,
item: i.to_owned(),
}
})?
)))?,
self.sounds
.iter()
.try_fold(String::new(), |acc, i| Ok(format!(
"{}{}\n",
acc,
i.reconstruct().map_err(|err| {
ResourceReconstructionError::ResourceItemReconstructionError {
source: err,
section: ResourceSection::Sounds,
item: i.to_owned(),
}
})?
)))?,
self.fonts
.iter()
.try_fold(String::new(), |acc, i| Ok(format!(
"{}{}\n",
acc,
i.reconstruct().map_err(|err| {
ResourceReconstructionError::ResourceItemReconstructionError {
source: err,
section: ResourceSection::Fonts,
item: i.to_owned(),
}
})?
)))?
.trim(), // i don't want the extra \n at the end of the file
))
}
}
#[derive(Error, Debug)]
pub enum ResourceParseError {
#[error("error while parsing a resource item of {section:?} at line {line}")]
ResourceItemParseError {
#[source]
source: serde_json::Error,
section: ResourceSection,
line: u32,
},
}
#[derive(Error, Debug)]
pub enum ResourceReconstructionError {
#[error("error while reconstruction resource item `{item:?}` of section {section:?}")]
ResourceItemReconstructionError {
#[source]
source: serde_json::Error,
section: ResourceSection,
item: ResourceItem,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub struct ResourceItem {
/// The filename of the resource
#[serde(rename = "resFullName")]
pub full_name: String,
/// The resource name of this resource
#[serde(rename = "resName")]
pub name: String,
// Unknown usage, this value is always 1
#[serde(rename = "resType")]
pub r#type: u8,
}
impl Parsable for ResourceItem {
type ParseError = serde_json::Error;
type ReconstructionError = serde_json::Error;
fn parse(decrypted_content: &str) -> Result<Self, Self::ParseError> {
serde_json::from_str(decrypted_content)
}
fn reconstruct(&self) -> Result<String, Self::ReconstructionError> {
serde_json::to_string(self)
}
}