-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Unicode SSID in status responses
This replaces the INI config parser from the config crate with a simplified parser that only support k=v pairs, but does proper printf unsecaping for non printable/on ASCII characters.
- Loading branch information
1 parent
40b8eae
commit 94ff56e
Showing
6 changed files
with
137 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::collections::HashMap; | ||
use std::fmt::Display; | ||
|
||
use serde::de::value::MapDeserializer; | ||
use serde::Deserialize; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error, PartialEq, Eq, Clone)] | ||
pub enum ConfigError { | ||
#[error("Missing '=' delimiter in config line")] | ||
MissingDelimterEqual, | ||
#[error("escape code is not made up of valid hex code")] | ||
InvalidEscape, | ||
#[error("escape code is incomplete")] | ||
IncompleteEscape, | ||
#[error("escaped value is not valid uft8 after unescaping")] | ||
NonUtf8Escape, | ||
#[error("Value could not be decoded")] | ||
SerdeError(String), | ||
} | ||
|
||
impl serde::de::Error for ConfigError { | ||
fn custom<T>(msg: T) -> Self | ||
where | ||
T: Display, | ||
{ | ||
Self::SerdeError(msg.to_string()) | ||
} | ||
} | ||
|
||
pub(crate) fn to_map(response: &str) -> Result<HashMap<&str, String>, ConfigError> { | ||
let mut map = HashMap::new(); | ||
for line in response.trim().lines() { | ||
let (k, v) = line | ||
.split_once('=') | ||
.ok_or(ConfigError::MissingDelimterEqual)?; | ||
map.insert(k, unprintf(v)?); | ||
} | ||
Ok(map) | ||
} | ||
|
||
pub(crate) fn deserialize_str<'de, T: Deserialize<'de>>(response: &str) -> Result<T, ConfigError> { | ||
let map = to_map(response)?; | ||
Ok(T::deserialize(MapDeserializer::new(map.into_iter()))?) | ||
} | ||
|
||
pub(crate) fn unprintf(escaped: &str) -> std::result::Result<String, ConfigError> { | ||
let mut bytes = escaped.as_bytes().iter().copied(); | ||
let mut unescaped = vec![]; | ||
// undo "printf_encode" | ||
loop { | ||
unescaped.push(match bytes.next() { | ||
Some(b'\\') => match bytes.next().ok_or(ConfigError::IncompleteEscape)? { | ||
b'n' => b'\n', | ||
b'r' => b'\r', | ||
b't' => b'\t', | ||
b'e' => b'\x1b', | ||
b'x' => { | ||
let hex = [ | ||
bytes.next().ok_or(ConfigError::IncompleteEscape)?, | ||
bytes.next().ok_or(ConfigError::IncompleteEscape)?, | ||
]; | ||
u8::from_str_radix( | ||
std::str::from_utf8(&hex).or(Err(ConfigError::InvalidEscape))?, | ||
16, | ||
) | ||
.or(Err(ConfigError::InvalidEscape))? | ||
} | ||
c => c, | ||
}, | ||
Some(c) => c, | ||
None => break, | ||
}) | ||
} | ||
String::from_utf8(unescaped).or(Err(ConfigError::NonUtf8Escape)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters