Skip to content

Commit a7abfbb

Browse files
committed
Move read_dmi_headers off Icon impl
1 parent b80a6d9 commit a7abfbb

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

src/icon.rs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ struct DmiHeaders {
5353
/// The second string cannot be empty (a value must exist), or a DmiError is returned.
5454
/// Only one set of quotes is allowed if allow_quotes is true, and it must wrap the entire value.
5555
/// If require_quotes is set, will error if there are not quotes around the value.
56-
///
56+
///
5757
/// Other details about this function:
58-
///
58+
///
5959
/// Keys have very little validation and are meant to be checked against a known value in most cases.
60-
/// Spaces are only allowed in the value if they are inside quotes or directly after the equals sign (where they are removed).
60+
/// Spaces are only allowed in the value if they are inside quotes or directly after the equals sign (where they are removed).
6161
/// Tabs and equals signs are only allowed in the value if they are not inside quotes.
6262
/// Removes quotes around values and removes backslashes for quotes inside the quotes.
6363
/// Removes backslashes used to escape other backslashes.
@@ -161,73 +161,73 @@ fn parse_dmi_line(
161161
Ok((prior_equals, post_equals))
162162
}
163163

164-
impl Icon {
165-
fn read_dmi_headers(
166-
decompressed_text: &mut std::iter::Peekable<std::str::Lines<'_>>,
167-
) -> Result<DmiHeaders, DmiError> {
168-
let current_line = decompressed_text.next();
169-
if current_line != Some("# BEGIN DMI") {
170-
return Err(DmiError::Generic(format!(
171-
"Error loading icon: no DMI header found. Beginning: {current_line:#?}"
172-
)));
173-
};
164+
fn read_dmi_headers(
165+
decompressed_text: &mut std::iter::Peekable<std::str::Lines<'_>>,
166+
) -> Result<DmiHeaders, DmiError> {
167+
let current_line = decompressed_text.next();
168+
if current_line != Some("# BEGIN DMI") {
169+
return Err(DmiError::Generic(format!(
170+
"Error loading icon: no DMI header found. Beginning: {current_line:#?}"
171+
)));
172+
};
174173

175-
let current_line = match decompressed_text.next() {
176-
Some(thing) => thing,
174+
let current_line = match decompressed_text.next() {
175+
Some(thing) => thing,
176+
None => {
177+
return Err(DmiError::Generic(String::from(
178+
"Error loading icon: no version header found.",
179+
)))
180+
}
181+
};
182+
let (key, value) = parse_dmi_line(current_line, false, false)?;
183+
if key != "version" {
184+
return Err(DmiError::Generic(format!(
185+
"Error loading icon: improper version header found: {key} = {value} ('{current_line}')"
186+
)));
187+
};
188+
let version = value;
189+
190+
let mut width = None;
191+
let mut height = None;
192+
for _ in 0..2 {
193+
let current_line = match decompressed_text.peek() {
194+
Some(thing) => *thing,
177195
None => {
178196
return Err(DmiError::Generic(String::from(
179-
"Error loading icon: no version header found.",
197+
"Error loading icon: DMI definition abruptly ends.",
180198
)))
181199
}
182200
};
183201
let (key, value) = parse_dmi_line(current_line, false, false)?;
184-
if key != "version" {
185-
return Err(DmiError::Generic(format!(
186-
"Error loading icon: improper version header found: {key} = {value} ('{current_line}')"
187-
)));
188-
};
189-
let version = value;
190-
191-
let mut width = None;
192-
let mut height = None;
193-
for _ in 0..2 {
194-
let current_line = match decompressed_text.peek() {
195-
Some(thing) => *thing,
196-
None => {
197-
return Err(DmiError::Generic(String::from(
198-
"Error loading icon: DMI definition abruptly ends.",
199-
)))
200-
}
201-
};
202-
let (key, value) = parse_dmi_line(current_line, false, false)?;
203-
match key.as_str() {
204-
"\twidth" => {
205-
width = Some(value.parse::<u32>()?);
206-
decompressed_text.next(); // consume the peeked value
207-
}
208-
"\theight" => {
209-
height = Some(value.parse::<u32>()?);
210-
decompressed_text.next(); // consume the peeked value
211-
}
212-
_ => {
213-
break;
214-
}
202+
match key.as_str() {
203+
"\twidth" => {
204+
width = Some(value.parse::<u32>()?);
205+
decompressed_text.next(); // consume the peeked value
206+
}
207+
"\theight" => {
208+
height = Some(value.parse::<u32>()?);
209+
decompressed_text.next(); // consume the peeked value
210+
}
211+
_ => {
212+
break;
215213
}
216214
}
215+
}
217216

218-
if width == Some(0) || height == Some(0) {
219-
return Err(DmiError::Generic(format!(
220-
"Error loading icon: invalid width ({width:#?}) / height ({height:#?}) values."
221-
)));
222-
};
217+
if width == Some(0) || height == Some(0) {
218+
return Err(DmiError::Generic(format!(
219+
"Error loading icon: invalid width ({width:#?}) / height ({height:#?}) values."
220+
)));
221+
};
223222

224-
Ok(DmiHeaders {
225-
version,
226-
width,
227-
height,
228-
})
229-
}
223+
Ok(DmiHeaders {
224+
version,
225+
width,
226+
height,
227+
})
228+
}
230229

230+
impl Icon {
231231
pub fn load<R: Read + Seek>(reader: R) -> Result<Icon, DmiError> {
232232
Self::load_internal(reader, true)
233233
}
@@ -271,7 +271,7 @@ impl Icon {
271271
let decompressed_text = String::from_utf8(decompressed_text)?;
272272
let mut decompressed_text = decompressed_text.lines().peekable();
273273

274-
let dmi_headers = Self::read_dmi_headers(&mut decompressed_text)?;
274+
let dmi_headers = read_dmi_headers(&mut decompressed_text)?;
275275
let version = dmi_headers.version;
276276

277277
// yes you can make a DMI without a width or height. it defaults to 32x32

0 commit comments

Comments
 (0)