Skip to content

Gate defmt behind target_os="none" #19

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ rust-version = "1.85"
[package.metadata.docs.rs]
all-features = true

[dependencies]
log = "0.4.26"
byteorder = { version = "1.5.0", default-features = false }
defmt = {version = "1.0.1", optional = true}

[features]
default = ["tcp", "rtu"]
tcp = []
rtu = []
std = ["byteorder/std"]
defmt = ["dep:defmt"]

[dependencies]
log = { version = "0.4.26" }
byteorder = { version = "1.5.0", default-features = false }

[target.'cfg(target_os = "none")'.dependencies]
defmt = { version = "1.0.1", optional = true }




[badges]
maintenance = { status = "actively-developed" }
2 changes: 1 addition & 1 deletion src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod rtu;
pub mod tcp;

/// The type of decoding
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DecoderType {
Request,
Expand Down
4 changes: 2 additions & 2 deletions src/codec/rtu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ pub use crate::frame::rtu::*;
const MAX_FRAME_LEN: usize = 256;

/// An extracted RTU PDU frame.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecodedFrame<'a> {
pub slave: SlaveId,
pub pdu: &'a [u8],
}

/// The location of all bytes that belong to the frame.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameLocation {
/// The index where the frame starts
Expand Down
4 changes: 2 additions & 2 deletions src/codec/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use crate::frame::tcp::*;
const MAX_FRAME_LEN: usize = 256;

/// An extracted TCP PDU frame.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecodedFrame<'a> {
pub transaction_id: TransactionId,
Expand All @@ -21,7 +21,7 @@ pub struct DecodedFrame<'a> {
}

/// The location of all bytes that belong to the frame.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FrameLocation {
/// The index where the frame starts
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "defmt")]
#[cfg(all(feature = "defmt", target_os = "none"))]
impl defmt::Format for Error {
fn format(&self, f: defmt::Formatter) {
match self {
Expand Down
4 changes: 2 additions & 2 deletions src/frame/coils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::error::*;

/// Packed coils
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Coils<'c> {
pub(crate) data: RawData<'c>,
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<'c> Coils<'c> {

/// Coils iterator.
// TODO: crate an generic iterator
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CoilsIter<'c> {
cnt: usize,
Expand Down
4 changes: 2 additions & 2 deletions src/frame/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::error::*;

/// Modbus data (u16 values)
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Data<'d> {
pub(crate) data: RawData<'d>,
Expand Down Expand Up @@ -58,7 +58,7 @@ impl<'d> Data<'d> {

/// Data iterator
// TODO: crate a generic iterator
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataIter<'d> {
cnt: usize,
Expand Down
14 changes: 7 additions & 7 deletions src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use byteorder::{BigEndian, ByteOrder};
/// A Modbus function code.
///
/// It is represented by an unsigned 8 bit integer.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FunctionCode {
/// Modbus Function Code: `01` (`0x01`).
Expand Down Expand Up @@ -158,7 +158,7 @@ pub(crate) type Quantity = u16;
type RawData<'r> = &'r [u8];

/// A request represents a message from the client (master) to the server (slave).
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Request<'r> {
ReadCoils(Address, Quantity),
Expand Down Expand Up @@ -193,20 +193,20 @@ pub enum Request<'r> {
}

/// A server (slave) exception response.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExceptionResponse {
pub function: FunctionCode,
pub exception: Exception,
}

/// Represents a message from the client (slave) to the server (master).
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RequestPdu<'r>(pub Request<'r>);

/// Represents a message from the server (slave) to the client (master).
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResponsePdu<'r>(pub Result<Response<'r>, ExceptionResponse>);

Expand All @@ -218,7 +218,7 @@ type EventCount = u16;
type MessageCount = u16;

/// The response data of a successfull request.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Response<'r> {
ReadCoils(Coils<'r>),
Expand Down Expand Up @@ -345,7 +345,7 @@ impl fmt::Display for Exception {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.get_name()) }
}

#[cfg(feature = "defmt")]
#[cfg(all(feature = "defmt", target_os = "none"))]
impl defmt::Format for Exception {
fn format(&self, fmt: defmt::Formatter) { defmt::write!(fmt, "{}", self.get_name()) }
}
Expand Down
6 changes: 3 additions & 3 deletions src/frame/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ use super::*;
pub type SlaveId = u8;

/// RTU header
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Header {
pub slave: SlaveId,
}

/// RTU Request ADU
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RequestAdu<'r> {
pub hdr: Header,
pub pdu: RequestPdu<'r>,
}

/// RTU Response ADU
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResponseAdu<'r> {
pub hdr: Header,
Expand Down
6 changes: 3 additions & 3 deletions src/frame/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ use super::*;
pub type TransactionId = u16;
pub type UnitId = u8;

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Header {
pub transaction_id: TransactionId,
pub unit_id: UnitId,
}

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RequestAdu<'r> {
pub hdr: Header,
pub pdu: RequestPdu<'r>,
}

#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(all(feature = "defmt", target_os = "none"), derive(defmt::Format))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResponseAdu<'r> {
pub hdr: Header,
Expand Down
Loading