Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/wasm pack #10

Merged
merged 30 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fa42a89
starting wasm feature
maebli Jun 2, 2024
ea594d3
fixing clippy error
maebli Jun 9, 2024
2bdaf58
moving module up
maebli Jun 10, 2024
6270206
wasm looking better
maebli Jun 10, 2024
9ee8af9
improving "full parse" function to be re-used in wasm and also in cli
maebli Jun 11, 2024
c0262b8
using serde in main package now and successfully used in wasm
maebli Jun 11, 2024
a9423f5
fixing serde feature naming
maebli Jun 12, 2024
1eabe9e
removing wee_alloc
maebli Jun 12, 2024
a44fed4
adding a web page parser
maebli Jun 12, 2024
9d6bb1d
adjusting pipeline
maebli Jun 12, 2024
055ee5c
adding wasm pack
maebli Jun 12, 2024
233bccc
adding some files that were missing
maebli Jun 12, 2024
e4553bb
removing test that does not work
maebli Jun 12, 2024
6632f48
fixing clippy warning in wasm pack
maebli Jun 12, 2024
90b287d
removing leftover weealoc
maebli Jun 12, 2024
a1afd3b
bumping version for release
maebli Jun 12, 2024
ba1a73f
fixing pipeline
maebli Jun 12, 2024
02af844
finally found why tag triggers not working
maebli Jun 12, 2024
daa096c
trying again to npm publish
maebli Jun 12, 2024
f262ecc
Minor code style fixes
SarthakSingh31 Jun 13, 2024
ba31ea3
Moved the profile for wasm crate to root Cargo.toml
SarthakSingh31 Jun 13, 2024
f9a50e2
Merge pull request #11 from SarthakSingh31/sar/feature/wasm-pack
maebli Jun 13, 2024
e69bd71
removing dead code
maebli Jun 13, 2024
22949fb
trying again to publish to npm
maebli Jun 13, 2024
4b9e1d5
next attempt
maebli Jun 13, 2024
4d7107d
trying again
maebli Jun 13, 2024
2a14d94
adding possibly missing npm install
maebli Jun 13, 2024
e0ebf64
fixing last step
maebli Jun 13, 2024
1e899ed
adding permissions
maebli Jun 13, 2024
373a6c3
adding documentation
maebli Jun 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ bindgen = "0.69.4"
[features]
std = []
plaintext-before-extension = []
serde_support = ["serde", "std", "arrayvec/serde"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this feature to just serde than make the array
["dep:serde", "std", "arrayvec/serde"]
also it is necessary to enable std to use serde? I don't think you need std to use serde


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

[dependencies]
serde = { version = "1.0", features = ["derive"], optional = true }
bitflags = "2.4.2"
arrayvec = "0.7.4"
arrayvec = { version = "0.7.4", features = [
"serde",
], optional = true, default-features = false }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is arrayvec really optional? You seem to use it without a feature gate in the code.
I think you just want
arrayvec = { version = "0.7.4", default-features = false }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The arrayvec/serde feature flag will enable the serde feature on arrayvec when necessary


[workspace]
members = ["cli"]
members = ["cli", "wasm"]
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "m-bus-parser-cli"
version = "0.0.4"
version = "0.0.3"
edition = "2021"
description = "A cli to use the library for parsing M-Bus frames"
license = "MIT"
Expand Down
14 changes: 12 additions & 2 deletions src/frames/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! is part of the MBUS data link layer
//! It is used to encapsulate the application layer data

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub enum Frame<'a> {
SingleCharacter {
Expand All @@ -22,6 +25,10 @@ pub enum Frame<'a> {
},
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub enum Function {
SndNk,
Expand Down Expand Up @@ -63,7 +70,10 @@ impl TryFrom<u8> for Function {
}
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub enum Address {
Uninitalized,
Expand Down
38 changes: 29 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ pub mod frames;
pub mod user_data;

#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(serde::Serialize))]
#[cfg_attr(
feature = "serde_support",
derive(serde::Deserialize),
serde(bound(deserialize = "'de: 'a"))
)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can probably be the same cfg_attr. You don't need a separate one for each derive.

#[cfg_attr(
    feature = "serde_support",
    derive(serde::Serialize, serde::Deserialize),
    serde(bound(deserialize = "'de: 'a"))
)]

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was not possible

pub struct MbusData<'a> {
pub frame: frames::Frame<'a>,
pub user_data: Option<user_data::UserDataBlock<'a>>,
pub data_records: Option<user_data::DataRecords>,
}

#[derive(Debug)]
Expand All @@ -77,9 +84,7 @@ impl From<FrameError> for MbusError {

impl From<ApplicationLayerError> for MbusError {
fn from(error: ApplicationLayerError) -> MbusError {
match error {
_ => MbusError::ApplicationLayerError(error),
}
MbusError::ApplicationLayerError(error)
}
}

Expand All @@ -88,15 +93,30 @@ impl<'a> TryFrom<&'a [u8]> for MbusData<'a> {

fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
let frame = frames::Frame::try_from(data)?;

let user_data = match &frame {
let mut user_data = None;
let mut data_records = None;
match &frame {
frames::Frame::LongFrame { data, .. } => {
Some(user_data::UserDataBlock::try_from(*data)?)
if let Ok(x) = user_data::UserDataBlock::try_from(*data) {
user_data = Some(x);
if let Ok(user_data::UserDataBlock::VariableDataStructure {
fixed_data_header: _,
variable_data_block,
}) = user_data::UserDataBlock::try_from(*data)
{
data_records = user_data::DataRecords::try_from(variable_data_block).ok();
}
}
}
frames::Frame::SingleCharacter { .. } => None,
frames::Frame::ShortFrame { .. } | frames::Frame::ControlFrame { .. } => None,
frames::Frame::SingleCharacter { .. } => (),
frames::Frame::ShortFrame { .. } => (),
frames::Frame::ControlFrame { .. } => (),
};

Ok(MbusData { frame, user_data })
Ok(MbusData {
frame,
user_data,
data_records,
})
}
}
48 changes: 41 additions & 7 deletions src/user_data/data_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ use super::variable_user_data::DataRecordError;
use arrayvec::ArrayVec;

const MAX_DIFE_RECORDS: usize = 10;
#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub struct DataInformationBlock {
pub data_information_field: DataInformationField,
#[serde(skip_serializing, skip_deserializing)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should also be in a cfg_attr

pub data_information_field_extension:
Option<ArrayVec<DataInformationFieldExtension, MAX_DIFE_RECORDS>>,
}
Expand All @@ -19,7 +24,10 @@ impl DataInformationBlock {
size
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub struct DataInformationField {
pub data: u8,
Expand All @@ -42,7 +50,10 @@ impl From<u8> for DataInformationFieldExtension {
DataInformationFieldExtension { data }
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub struct DataInformationFieldExtension {
pub data: u8,
Expand Down Expand Up @@ -94,7 +105,10 @@ impl DataInformationFieldExtension {
self.data & 0x80 != 0
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, Clone, PartialEq)]
pub struct DataInformation {
pub storage_number: u64,
Expand All @@ -116,7 +130,10 @@ impl std::fmt::Display for DataInformation {
}

const MAXIMUM_DATA_INFORMATION_SIZE: usize = 11;

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, Clone, PartialEq)]
pub struct DataInformationExtensionField {}

Expand Down Expand Up @@ -205,11 +222,19 @@ impl TryFrom<&DataInformationBlock> for DataInformation {
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub enum DataType {
Text(ArrayVec<u8, 18>),
Number(f64),
}
#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(PartialEq, Debug)]
pub struct Data {
value: Option<DataType>,
Expand Down Expand Up @@ -509,7 +534,10 @@ impl DataInformation {
self.size
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FunctionField {
InstantaneousValue,
Expand All @@ -529,7 +557,10 @@ impl std::fmt::Display for FunctionField {
}
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SpecialFunctions {
ManufacturerSpecific,
Expand Down Expand Up @@ -655,7 +686,10 @@ impl DataFieldCoding {
}
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DataFieldCoding {
NoData,
Expand Down
20 changes: 16 additions & 4 deletions src/user_data/data_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ use super::{
value_information::{ValueInformation, ValueInformationBlock},
variable_user_data::DataRecordError,
};

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub struct RawDataRecordHeader {
pub data_information_block: DataInformationBlock,
pub value_information_block: ValueInformationBlock,
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub struct ProcessedDataRecordHeader {
pub data_information: DataInformation,
pub value_information: ValueInformation,
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub struct DataRecord {
pub data_record_header: DataRecordHeader,
Expand All @@ -27,7 +36,10 @@ impl DataRecord {
self.data_record_header.get_size() + self.data.get_size()
}
}

#[cfg_attr(
feature = "serde_support",
derive(serde::Serialize, serde::Deserialize)
)]
#[derive(Debug, PartialEq)]
pub struct DataRecordHeader {
pub raw_data_record_header: RawDataRecordHeader,
Expand Down
Loading