add no-std

This commit is contained in:
2025-12-06 22:30:42 +01:00
parent f3b79bb9aa
commit c2b9eaa8d3
5 changed files with 47 additions and 12 deletions

View File

@@ -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]