From c2b9eaa8d3274b8485ab1d7c7cee020460d11e60 Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Sat, 6 Dec 2025 22:30:42 +0100 Subject: [PATCH] add no-std --- Cargo.toml | 6 +++++- src/enums.rs | 12 ++++++------ src/lib.rs | 2 ++ src/message.rs | 31 ++++++++++++++++++++++++++----- src/utils.rs | 8 ++++++++ 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 92926bf..443b042 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,10 @@ categories = ["encoding", "decoding"] # [features] # json = ["serde_json"] +[features] +default = ["std"] +std = [] + [dependencies] serde = { version = "1.0"} # serde_json = { version = "1.0", optional = true } @@ -19,7 +23,7 @@ thiserror = "2.0.17" chrono = { version = "0.4", features = ["serde"] } enum_stringify = "0.6.4" num_enum = "0.7.5" -log = "0.4.28" +log = { version = "0.4" } [lints.clippy] diff --git a/src/enums.rs b/src/enums.rs index 3f41457..0ed8fa7 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -1,10 +1,9 @@ -use enum_stringify::EnumStringify; use num_enum::{IntoPrimitive, TryFromPrimitive}; /// Message Identifier used in the communication protocol. This is specified by Viessmann /// and UNACKED is rarely to never used. #[repr(u8)] -#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, EnumStringify)] +#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq)] pub enum MessageIdentifier { Request = 0x00, Response = 0x01, @@ -12,11 +11,12 @@ pub enum MessageIdentifier { CommError = 0x03, } -/// Function Code used in the communication protocol. This is specified by Viessmann -/// and Read and Write are usually used for simpler values, RPC can maybe be used for -/// more complex things, but this is currently unclear +/// Function Code used in the communication protocol. +/// +/// This is specified by Viessmann and Read and Write are usually used for simpler values, +/// RPC can maybe be used for more complex things, but this is currently unclear #[repr(u8)] -#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, EnumStringify)] +#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq)] pub enum FunctionCode { VirtualREAD = 0x01, VirtualWRITE = 0x02, diff --git a/src/lib.rs b/src/lib.rs index 241e0bc..a162d32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "std"), no_std)] + mod message; mod enums; mod utils; diff --git a/src/message.rs b/src/message.rs index 6f68cbf..ef15ceb 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,6 +1,19 @@ -use log::warn; -use thiserror::Error; +use log::{warn, error}; +// Use std::vec when std exists, otherwise alloc::vec +#[cfg(feature = "std")] +use std::vec; + +#[cfg(not(feature = "std"))] +extern crate alloc; + +#[cfg(not(feature = "std"))] +use alloc::vec; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +use thiserror::Error; use crate::{enums::{FunctionCode, MessageIdentifier}, utils::calculate_checksum}; #[derive(PartialEq, Eq, Debug)] @@ -167,7 +180,7 @@ impl Protocol300Message { pub enum MessageCreationError { /// The data length is longer than u8 #[error("Data too long")] - DataTooLong(#[from] std::num::TryFromIntError), + DataTooLong(#[from] core::num::TryFromIntError), } @@ -190,7 +203,7 @@ pub enum MessageDecodingError { InvalidTelegramStart(u8), /// Error getiting the length of the bytes slice, should never happen #[error("Error getting the length of the bytes slice")] - NoLength(#[from] std::num::TryFromIntError), + NoLength(#[from] core::num::TryFromIntError), /// The advertiesed telegram length and actual length do not match #[error("The advertiesed telegram length and actual length do not match")] LengthMismatch(), @@ -204,7 +217,15 @@ pub enum MessageDecodingError { #[cfg(test)] mod tests { - use crate::{enums::{FunctionCode, MessageIdentifier}, message::{MessageCreationError, Protocol300Message}, utils::calculate_checksum}; + use crate::{enums::{FunctionCode, MessageIdentifier}, message::{MessageCreationError, Protocol300Message}}; + #[cfg(not(feature = "std"))] + extern crate alloc; + + #[cfg(not(feature = "std"))] + use alloc::vec; + + #[cfg(not(feature = "std"))] + use alloc::vec::Vec; #[test] diff --git a/src/utils.rs b/src/utils.rs index fc1d522..e95fdf5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,6 +18,14 @@ pub fn calculate_checksum(length: u8, message_identifier: MessageIdentifier, fun #[cfg(test)] mod tests { use crate::{enums::{FunctionCode, MessageIdentifier}, message::Protocol300Message, utils::calculate_checksum}; + #[cfg(not(feature = "std"))] + extern crate alloc; + + #[cfg(not(feature = "std"))] + use alloc::vec; + + #[cfg(not(feature = "std"))] + use alloc::vec::Vec; #[test] fn test_check_checksum_1() {