Skip to content

Commit

Permalink
byteserde_types always derives serde for included ascii types (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
softstream-link authored Jan 11, 2024
1 parent 8462a2e commit 0cad1f7
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
rustdocflags = ["-D", "warnings"] # denies warnings in docs
7 changes: 6 additions & 1 deletion .github/workflows/pull-request-stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ on:
jobs:
test-matrix:
uses: softstream-link/workflows/.github/workflows/rust-test-matrix-toolchain-os.yml@v1
with:
toolchain: '["stable"]'
fail-fast: false
clippy:
uses: softstream-link/workflows/.github/workflows/rust-clippy-toolchain.yml@v1
bench-matrix:
uses: softstream-link/workflows/.github/workflows/rust-bench-matrix-toolchain-os.yml@v1

with:
fail-fast: false

7 changes: 5 additions & 2 deletions .github/workflows/push-feature-branch.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Feature Branch
name: Feature Branch Build
on:
push:
branches:
Expand All @@ -8,8 +8,11 @@ on:
jobs:
test-matrix:
uses: softstream-link/workflows/.github/workflows/rust-test-matrix-toolchain-os.yml@v1
with:
fail-fast: false
clippy:
uses: softstream-link/workflows/.github/workflows/rust-clippy-toolchain.yml@v1
bench-matrix:
uses: softstream-link/workflows/.github/workflows/rust-bench-matrix-toolchain-os.yml@v1

with:
fail-fast: false
8 changes: 6 additions & 2 deletions .github/workflows/push-master-stable.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# samples https://github.com/actions/starter-workflows/blob/main/ci/rust.yml
name: Push to master Build On Stable
on:
push:
Expand All @@ -8,7 +7,12 @@ on:
jobs:
test-matrix:
uses: softstream-link/workflows/.github/workflows/rust-test-matrix-toolchain-os.yml@v1
with:
toolchain: '["stable"]'
fail-fast: false
clippy:
uses: softstream-link/workflows/.github/workflows/rust-clippy-toolchain.yml@v1
bench-matrix:
uses: softstream-link/workflows/.github/workflows/rust-bench-matrix-toolchain-os.yml@v1
uses: softstream-link/workflows/.github/workflows/rust-bench-matrix-toolchain-os.yml@v1
with:
fail-fast: false
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bytestream",
"clippy",
"endianess",
"endianness",
"flds",
"nextest",
"nonoverlapping",
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ resolver = "2"

[workspace.package]
# NOTE remember to update this and below [workspace.dependencies] for byteserde and byteserde_derive for the release
version = "0.6.0"
version = "0.6.1"
authors = ["Softstream <[email protected]>"]
readme = "readme.md"
license-file = "LICENSE"
Expand All @@ -21,9 +21,9 @@ categories = ["encoding"]

[workspace.dependencies]
# workspace members
byteserde = { version = "0.6.0", path = "./byteserde" }
byteserde_derive = { version = "0.6.0", path = "./byteserde_derive" }
byteserde_types = { version = "0.6.0", path = "./byteserde_types" }
byteserde = { version = "0.6.1", path = "./byteserde" }
byteserde_derive = { version = "0.6.1", path = "./byteserde_derive" }
byteserde_types = { version = "0.6.1", path = "./byteserde_types" }


bytes = "1.4"
Expand Down
55 changes: 53 additions & 2 deletions byteserde_types/src/strings/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use byteserde::error::Result;
use byteserde::prelude::*;
use byteserde::utils::hex::{to_hex_line, to_hex_pretty};
use byteserde_derive::{ByteDeserializeSlice, ByteSerializeHeap, ByteSerializeStack, ByteSerializedLenOf, ByteSerializedSizeOf};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::any::type_name;
use std::cmp::min;
use std::fmt;
Expand Down Expand Up @@ -48,6 +49,17 @@ impl<const LEN: usize, const PADDING: u8, const RIGHT_ALIGN: bool> StringAsciiFi
&self.0[0..]
}
}
impl<const LEN: usize, const PADDING: u8, const RIGHT_ALIGN: bool> Serialize for StringAsciiFixed<LEN, PADDING, RIGHT_ALIGN> {
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
serializer.serialize_str(self.to_string().as_str())
}
}
impl<'de, const LEN: usize, const PADDING: u8, const RIGHT_ALIGN: bool> Deserialize<'de> for StringAsciiFixed<LEN, PADDING, RIGHT_ALIGN> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
let ascii_str = String::deserialize(deserializer)?;
Ok(Self::from(ascii_str.as_bytes()))
}
}
impl<const LEN: usize, const PADDING: u8, const RIGHT_ALIGN: bool> Default for StringAsciiFixed<LEN, PADDING, RIGHT_ALIGN> {
fn default() -> Self {
Self([PADDING; LEN])
Expand Down Expand Up @@ -125,6 +137,7 @@ mod test_string_ascii_fixed {
use crate::unittest::setup;
use byteserde::prelude::*;
use log::info;
use serde_json::{to_string, from_str};

#[test]
fn test_take() {
Expand Down Expand Up @@ -187,6 +200,18 @@ mod test_string_ascii_fixed {
fn test_from_u16_fail() {
let _: StringAsciiFixed<4, b'0', true> = u16::MAX.into();
}
#[test]
fn test_json(){
setup::log::configure();
let inp_str: StringAsciiFixed<5, b'0', true> = 12345_u16.into();
info!("inp_str:? {:?}", inp_str);
let out_json = to_string(&inp_str).unwrap();
info!("out_json: {}", out_json);
assert_eq!(out_json, r#""12345""#);
let out_str: StringAsciiFixed<5, b'0', true> = from_str(&out_json).unwrap();
info!("out_str:? {:?}", out_str);
assert_eq!(out_str, inp_str);
}
}

/// A string of ascii characters with a variable length allocated on heap using `Vec<u8>`
Expand Down Expand Up @@ -265,13 +290,17 @@ impl fmt::Display for StringAscii {
}
impl serde::Serialize for StringAscii {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where S: serde::Serializer {
where
S: serde::Serializer,
{
String::from_utf8_lossy(&self.0).serialize(serializer)
}
}
impl<'de> serde::Deserialize<'de> for StringAscii {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where D: serde::Deserializer<'de> {
where
D: serde::Deserializer<'de>,
{
let ascii_str = String::deserialize(deserializer)?;
Ok(Self::from(ascii_str.as_bytes()))
}
Expand Down Expand Up @@ -436,6 +465,20 @@ impl<const CHAR: u8> ByteDeserializeSlice<ConstCharAscii<CHAR>> for ConstCharAsc
}
}
}
impl<const CHAR: u8> Serialize for ConstCharAscii<CHAR> {
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
serializer.serialize_str(self.to_string().as_str())
}
}
impl<'de, const CHAR: u8> Deserialize<'de> for ConstCharAscii<CHAR> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
let c = char::deserialize(deserializer)?;
match c == Self::to_char() {
true => Ok(Default::default()),
false => Err(serde::de::Error::custom(format!("Type {:?} expected: 0x{:02x} actual: 0x{:02x}", type_name::<Self>(), CHAR, c as u8))),
}
}
}
impl<const CHAR: u8> Default for ConstCharAscii<CHAR> {
fn default() -> Self {
Self(CHAR)
Expand Down Expand Up @@ -468,6 +511,7 @@ mod test_const_char_ascii {
use byteserde::prelude::*;
use byteserde_derive::ByteSerializeStack;
use log::info;
use serde_json::{from_str, to_string};

#[test]
fn test_const_char_ascii() {
Expand All @@ -485,5 +529,12 @@ mod test_const_char_ascii {
let out_res: byteserde::error::Result<ConstCharAscii<b'+'>> = des.deserialize();
info!("out_res: {:?}", out_res);
assert!(out_res.is_err());

let out_json = to_string(&out_plus).unwrap();
info!("out_json: {}", out_json);
assert_eq!(out_json, r#""+""#);
let out_plus: ConstCharAscii<b'+'> = from_str(&out_json).unwrap();
info!("out_plus: {}", out_plus);
assert_eq!(out_plus, ConstCharAscii::<b'+'>::default());
}
}

0 comments on commit 0cad1f7

Please sign in to comment.