Skip to content

Commit

Permalink
Merge pull request #15 from dark0dave/feature/variableCharArraysAreSt…
Browse files Browse the repository at this point in the history
…rings

feat(strings): Treat varriable char arrays as strings
  • Loading branch information
dark0dave authored Mar 9, 2024
2 parents 1a8c1b8 + c8388ff commit 1e117a5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 73 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-added-large-files
args: ['--maxkb=5000']
Expand All @@ -17,7 +17,7 @@ repos:
- id: git-dirty

- repo: https://github.com/commitizen-tools/commitizen
rev: v2.39.1
rev: v3.18.0
hooks:
- id: commitizen
stages: [commit-msg]
Expand Down
116 changes: 58 additions & 58 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions models/src/common/variable_char_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use serde::{de::Visitor, Deserialize, Serialize, Serializer};

#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct VariableCharArray(pub Rc<[u8]>);

impl Display for VariableCharArray {
Expand Down Expand Up @@ -43,28 +43,24 @@ impl Serialize for VariableCharArray {
where
S: Serializer,
{
serializer.collect_seq(self.0.iter())
serializer.collect_str(self)
}
}

struct VarriableCharArrayVisitor;
struct VariableCharArrayVisitor;

impl<'de> Visitor<'de> for VarriableCharArrayVisitor {
impl<'de> Visitor<'de> for VariableCharArrayVisitor {
type Value = VariableCharArray;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "struct VarriableCharArray")
write!(formatter, "struct VariableCharArray")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
A: serde::de::SeqAccess<'de>,
E: serde::de::Error,
{
let mut destination = Vec::with_capacity(seq.size_hint().unwrap_or(0));
while let Ok(Some(item)) = seq.next_element::<u8>() {
destination.push(item);
}
Ok(VariableCharArray(destination.into()))
Ok(VariableCharArray::from(v))
}
}

Expand All @@ -73,7 +69,7 @@ impl<'de> Deserialize<'de> for VariableCharArray {
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_seq(VarriableCharArrayVisitor)
deserializer.deserialize_str(VariableCharArrayVisitor)
}
}

Expand Down

0 comments on commit 1e117a5

Please sign in to comment.