Skip to content

Commit

Permalink
Merge pull request #11 from SarthakSingh31/sar/feature/wasm-pack
Browse files Browse the repository at this point in the history
Minor code style fixes
  • Loading branch information
maebli authored Jun 13, 2024
2 parents daa096c + ba31ea3 commit f9a50e2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ bindgen = "0.69.4"
[features]
std = []
plaintext-before-extension = []
serde = ["dep:serde", "std", "arrayvec/serde"]
serde = ["dep:serde", "std", "arrayvec/serde", "bitflags/serde"]

[profile.release]
opt-level = 'z' # Optimize for size
lto = true # Enable Link Time Optimization
codegen-units = 1 # Reduce codegen units to improve optimizations

[profile.release.package."m-bus-parser-wasm-pack"]
opt-level = "s"

[dependencies]
serde = { version = "1.0", features = ["derive"], optional = true }
bitflags = "2.4.2"
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ pub mod frames;
pub mod user_data;

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize),
derive(serde::Serialize, serde::Deserialize),
serde(bound(deserialize = "'de: 'a"))
)]
pub struct MbusData<'a> {
Expand Down
41 changes: 21 additions & 20 deletions src/user_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,31 @@ impl Default for DataRecords {
DataRecords::new()
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for StatusField {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_u8(self.bits())
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for StatusField {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bits = u8::deserialize(deserializer)?;
StatusField::from_bits(bits)
.ok_or_else(|| serde::de::Error::custom("Invalid bits for StatusField"))
}
}
// #[cfg(feature = "serde")]
// impl serde::Serialize for StatusField {
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
// where
// S: serde::Serializer,
// {
// serializer.serialize_u8(self.bits())
// }
// }
// #[cfg(feature = "serde")]
// impl<'de> serde::Deserialize<'de> for StatusField {
// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
// where
// D: serde::Deserializer<'de>,
// {
// let bits = u8::deserialize(deserializer)?;
// StatusField::from_bits(bits)
// .ok_or_else(|| serde::de::Error::custom("Invalid bits for StatusField"))
// }
// }

bitflags::bitflags! {
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StatusField: u8 {
const COUNTER_BINARY_SIGNED = 0b00000001;
const COUNTER_FIXED_DATE = 0b00000010;
Expand Down
4 changes: 0 additions & 4 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,3 @@ console_error_panic_hook = { version = "0.1.7", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.34"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

0 comments on commit f9a50e2

Please sign in to comment.