summaryrefslogtreecommitdiff
path: root/crates/windlass/src/dictionary.rs
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-10-01 15:42:50 +0100
committertcmal <me@aria.rip>2024-10-11 23:57:19 +0100
commit34572a8f3839e37063641fa693029ad52265730a (patch)
treedd81dfba798039acb638aa1945a58cff023f1c38 /crates/windlass/src/dictionary.rs
parentd7275a13989b82ea8fc7b2320ef0cf2f427f7d5d (diff)
Stricter clippy lints
Diffstat (limited to 'crates/windlass/src/dictionary.rs')
-rw-r--r--crates/windlass/src/dictionary.rs28
1 files changed, 15 insertions, 13 deletions
diff --git a/crates/windlass/src/dictionary.rs b/crates/windlass/src/dictionary.rs
index 270b0e5..2ff9481 100644
--- a/crates/windlass/src/dictionary.rs
+++ b/crates/windlass/src/dictionary.rs
@@ -71,21 +71,23 @@ impl Dictionary {
}
/// Extra data returned in the dictionary response
- pub fn extra(&self) -> &BTreeMap<String, serde_json::Value> {
+ pub const fn extra(&self) -> &BTreeMap<String, serde_json::Value> {
&self.extra
}
/// Figure out the actual value of a command tag
- fn map_tag(tag: i16) -> Result<u16, DictionaryError> {
+ #[allow(clippy::cast_sign_loss, clippy::cast_possible_wrap)]
+ fn map_tag(tag: i16) -> Result<u16, Error> {
+ // TODO: Hacky, and heap allocation is inefficient
let mut buf = vec![];
encode_vlq_int(&mut buf, tag as u32);
let v = if buf.len() > 1 {
- ((buf[0] as u16) & 0x7F) << 7 | (buf[1] as u16) & 0x7F
+ ((u16::from(buf[0])) & 0x7F) << 7 | (u16::from(buf[1])) & 0x7F
} else {
- (buf[0] as u16) & 0x7F
+ (u16::from(buf[0])) & 0x7F
};
if v >= 1 << 14 {
- Err(DictionaryError::InvalidCommandTag(v))
+ Err(Error::InvalidCommandTag(v))
} else {
Ok(v)
}
@@ -93,7 +95,7 @@ impl Dictionary {
}
impl TryFrom<RawDictionary> for Dictionary {
- type Error = DictionaryError;
+ type Error = Error;
fn try_from(raw: RawDictionary) -> Result<Self, Self::Error> {
let mut message_ids = BTreeMap::new();
@@ -101,9 +103,9 @@ impl TryFrom<RawDictionary> for Dictionary {
for (cmd, tag) in raw.commands {
let mut split = cmd.split(' ');
- let name = split.next().ok_or(DictionaryError::EmptyCommand)?;
+ let name = split.next().ok_or(Error::EmptyCommand)?;
let parser = MessageParser::new(name, split)
- .map_err(|e| DictionaryError::InvalidCommandFormat(name.to_string(), e))?;
+ .map_err(|e| Error::InvalidCommandFormat(name.to_string(), e))?;
let tag = Self::map_tag(tag)?;
message_parsers.insert(tag, parser);
message_ids.insert(name.to_string(), tag);
@@ -111,9 +113,9 @@ impl TryFrom<RawDictionary> for Dictionary {
for (resp, tag) in raw.responses {
let mut split = resp.split(' ');
- let name = split.next().ok_or(DictionaryError::EmptyCommand)?;
+ let name = split.next().ok_or(Error::EmptyCommand)?;
let parser = MessageParser::new(name, split)
- .map_err(|e| DictionaryError::InvalidCommandFormat(name.to_string(), e))?;
+ .map_err(|e| Error::InvalidCommandFormat(name.to_string(), e))?;
let tag = Self::map_tag(tag)?;
message_parsers.insert(tag, parser);
message_ids.insert(name.to_string(), tag);
@@ -121,12 +123,12 @@ impl TryFrom<RawDictionary> for Dictionary {
for (msg, tag) in raw.output {
let parser = MessageParser::new_output(&msg)
- .map_err(|e| DictionaryError::InvalidCommandFormat(msg.to_string(), e))?;
+ .map_err(|e| Error::InvalidCommandFormat(msg.to_string(), e))?;
let tag = Self::map_tag(tag)?;
message_parsers.insert(tag, parser);
}
- Ok(Dictionary {
+ Ok(Self {
message_ids,
message_parsers,
config: raw.config,
@@ -189,7 +191,7 @@ impl EnumDef {
/// Error encountered when getting dictionary from microcontroller
#[derive(thiserror::Error, Debug)]
-pub enum DictionaryError {
+pub enum Error {
/// Found an empty command
#[error("empty command found")]
EmptyCommand,