diff --git a/Cargo.toml b/Cargo.toml index bccfb3c..c6ff266 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,19 +1,19 @@ [package] -name = "accelerometer" -version = "0.12.0" # Also update html_root_url in lib.rs when bumping this +name = "accelerometer" +version = "0.12.0" # Also update html_root_url in lib.rs when bumping this description = """ Generic, embedded-friendly accelerometer support, including traits and types for taking readings from 2 or 3-axis accelerometers and tracking device orientations. """ -authors = ["Tony Arcieri "] -license = "Apache-2.0 OR MIT" -repository = "https://github.com/NeoBirth/accelerometer.rs" -readme = "README.md" -edition = "2018" +authors = ["Tony Arcieri "] +license = "Apache-2.0 OR MIT" +repository = "https://github.com/NeoBirth/accelerometer.rs" +readme = "README.md" +edition = "2018" rust-version = "1.47" -categories = ["embedded", "hardware-support", "no-std"] -keywords = ["acceleration", "position", "tracking"] +categories = ["embedded", "hardware-support", "no-std"] +keywords = ["acceleration", "position", "tracking"] [badges] maintenance = { status = "passively-maintained" } @@ -22,6 +22,15 @@ maintenance = { status = "passively-maintained" } version = "2" features = ["vector"] +[dependencies.defmt] +version = "0.3.8" +optional = true + [features] default = ["orientation"] orientation = [] +defmt = ["dep:defmt"] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] diff --git a/src/error.rs b/src/error.rs index 5dc85d5..6dc67cd 100644 --- a/src/error.rs +++ b/src/error.rs @@ -100,3 +100,39 @@ where Self::new_with_cause(ErrorKind::Bus, cause) } } + +#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))] +#[cfg(feature = "defmt")] +impl defmt::Format for Error { + fn format(&self, fmt: defmt::Formatter<'_>) { + // Implemented manually so that the docs.rs marker can be applied. + defmt::write!( + fmt, + "Error {{ kind = {}, cause = {} }}", + self.kind, + self.cause + ) + } +} + +#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))] +#[cfg(feature = "defmt")] +impl defmt::Format for ErrorKind { + fn format(&self, fmt: defmt::Formatter<'_>) { + // Implemented manually so that the docs.rs marker can be applied. + match self { + ErrorKind::Device => { + defmt::write!(fmt, "Device") + } + ErrorKind::Bus => { + defmt::write!(fmt, "Bus") + } + ErrorKind::Mode => { + defmt::write!(fmt, "Mode") + } + ErrorKind::Param => { + defmt::write!(fmt, "Param") + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 2690fb6..bb57770 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,8 +39,12 @@ #![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms, unused_qualifications)] +// Enables the `doc_cfg` feature when the `docsrs` configuration attribute is defined. +#![cfg_attr(docsrs, feature(doc_cfg))] + mod accelerometer; pub mod error; +#[cfg_attr(docsrs, doc(cfg(feature = "orientation")))] #[cfg(feature = "orientation")] pub mod orientation; diff --git a/src/orientation.rs b/src/orientation.rs index b8ba633..e8d9267 100644 --- a/src/orientation.rs +++ b/src/orientation.rs @@ -45,3 +45,34 @@ impl Orientation { matches!(self, Orientation::PortraitUp | Orientation::PortraitDown) } } + +#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))] +#[cfg(feature = "defmt")] +impl defmt::Format for Orientation { + fn format(&self, fmt: defmt::Formatter<'_>) { + // Implemented manually so that the docs.rs marker can be applied. + match self { + Orientation::Unknown => { + defmt::write!(fmt, "Unknown") + } + Orientation::PortraitUp => { + defmt::write!(fmt, "PortraitUp") + } + Orientation::PortraitDown => { + defmt::write!(fmt, "PortraitDown") + } + Orientation::LandscapeUp => { + defmt::write!(fmt, "LandscapeUp") + } + Orientation::LandscapeDown => { + defmt::write!(fmt, "LandscapeDown") + } + Orientation::FaceUp => { + defmt::write!(fmt, "FaceUp") + } + Orientation::FaceDown => { + defmt::write!(fmt, "FaceDown") + } + } + } +} diff --git a/src/orientation/tracker.rs b/src/orientation/tracker.rs index 67a6cc2..021cedf 100644 --- a/src/orientation/tracker.rs +++ b/src/orientation/tracker.rs @@ -92,3 +92,17 @@ impl Tracker { self.last_orientation } } + +#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))] +#[cfg(feature = "defmt")] +impl defmt::Format for Tracker { + fn format(&self, fmt: defmt::Formatter<'_>) { + // Implemented manually so that the docs.rs marker can be applied. + defmt::write!( + fmt, + "Tracker {{ threshold = {}, last_orientation = {} }}", + self.threshold, + self.last_orientation + ) + } +}