summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2025-09-07 23:08:56 -0400
committerpommicket <pommicket@gmail.com>2025-09-07 23:08:56 -0400
commit858ee035533a5bcbe6e68ea7054b36083752b563 (patch)
treebdd03fe17efd3f9b635374b5ec13f1bfd5bf2f27 /src/lib.rs
parent39b31c18e9ff16c12cbb723a9d897ca34d9aef6d (diff)
Fix various bugs
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs41
1 files changed, 6 insertions, 35 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 16329bd..8a03384 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,7 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
-use alloc::borrow::Cow;
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap as Map;
use alloc::sync::Arc;
@@ -40,37 +39,9 @@ pub struct Configuration {
impl fmt::Display for Configuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut lines = vec![];
- fn format_value(val: &str) -> Cow<str> {
- if val.chars().all(|c| !c.is_ascii_control())
- && !val.starts_with(['\'', '"', '`'])
- && !val.starts_with(char::is_whitespace)
- {
- return Cow::Borrowed(val);
- }
- let mut quoted = String::from("\"");
- for c in val.chars() {
- if c == '"' {
- quoted.push_str("\\\"");
- } else if c == '\n' {
- quoted.push_str("\\n");
- } else if c == '\r' {
- quoted.push_str("\\r");
- } else if c == '\t' {
- quoted.push_str("\\t");
- } else if c == '\\' {
- quoted.push('\\');
- } else if c.is_ascii_control() {
- quoted.push_str(&format!("\\x{:02x}", c as u32));
- } else {
- quoted.push(c);
- }
- }
- quoted.push('"');
- Cow::Owned(quoted)
- }
fn add_lines(lines: &mut Vec<String>, prefix: &str, conf: &Configuration) {
for (key, val) in conf.values.iter() {
- lines.push(format!("{prefix}{key}: {}", format_value(&val.value)));
+ lines.push(format!("{prefix}{key} = {:?}", val.value));
}
for (key, child) in conf.children.iter() {
add_lines(lines, &format!("{prefix}{key}."), child);
@@ -359,7 +330,7 @@ impl Parser {
loop {
let line = if first {
first = false;
- quoted
+ &quoted[1..]
} else {
line_buf.truncate(0);
if !reader.read_until_lf(&mut line_buf)? {
@@ -420,7 +391,7 @@ impl Parser {
self.nonfatal_errors
.push(Error::InvalidValue(location(line_number)));
}
- unquoted.push(char::try_from(nibble1 << 8 | nibble2).unwrap());
+ unquoted.push(char::try_from(nibble1 << 4 | nibble2).unwrap());
}
'u' => {
let mut c = chars.next();
@@ -467,6 +438,7 @@ impl Parser {
unquoted.push(c);
}
}
+ unquoted.push('\n');
}
Err(Error::UnterminatedString(start_location.clone(), delimiter))
}
@@ -603,13 +575,12 @@ impl Configuration {
.map(|x| x.as_ref())
}
fn get_val(&self, key: &str) -> Option<&Value> {
- let Some(last_dot) = key.rfind('.') else {
+ let Some((path, last_component)) = key.rsplit_once('.') else {
return self.values.get(key);
};
- let (path, last_component) = key.split_at(last_dot);
let mut node = self;
for component in path.split('.') {
- node = self.children.get(component)?;
+ node = node.children.get(component)?;
}
node.values.get(last_component)
}