embedded_interfaces/
lib.rs

1//! A comprehensive framework for building type-safe and ergonomic embedded device drivers.
2//!
3#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
4#![cfg_attr(not(feature = "std"), no_std)]
5#[cfg(not(any(feature = "sync", feature = "async")))]
6compile_error!("You must enable at least one of the create features `sync` or `async`");
7
8// Re-exports for codegen macro
9pub use bitvec;
10pub use bytemuck;
11pub use const_format;
12
13pub mod bitdump;
14pub mod commands;
15pub mod i2c;
16pub mod packable;
17pub mod registers;
18pub mod spi;
19
20/// A combined error type for Codec or Bus errors
21#[derive(Debug, thiserror::Error)]
22pub enum TransportError<CodecError, BusError> {
23    /// The codec failed to encode or decode something
24    #[error("codec error")]
25    Codec(CodecError),
26    /// An unexpected error
27    #[error("unexpected: {0}")]
28    Unexpected(&'static str),
29    /// An error ocurred on the underlying interface
30    #[error("bus error")]
31    Bus(#[from] BusError),
32}
33
34/// Objects that implement this trait can produce a pretty-printed bit-dump of it's internal
35/// structure.
36#[cfg(feature = "std")]
37pub trait BitdumpFormattable {
38    /// Returns a bitdump formatter for the current object.
39    fn bitdump(&self) -> bitdump::BitdumpFormatter;
40}
41
42#[cfg(feature = "std")]
43impl BitdumpFormattable for () {
44    fn bitdump(&self) -> bitdump::BitdumpFormatter {
45        bitdump::BitdumpFormatter::new("()".to_string(), vec![], vec![])
46    }
47}
48
49#[cfg(feature = "std")]
50/// A helper trait that conditionally binds to BitdumpFormattable depending on whether the
51/// necessary feature "std" is enabled.
52pub trait MaybeBitdumpFormattable: crate::BitdumpFormattable {}
53
54#[cfg(feature = "std")]
55// Blanket impl
56impl<T: crate::BitdumpFormattable> MaybeBitdumpFormattable for T {}
57
58#[cfg(not(feature = "std"))]
59/// A helper trait that conditionally binds to BitdumpFormattable depending on whether the
60/// necessary feature "std" is enabled.
61pub trait MaybeBitdumpFormattable {}
62
63#[cfg(not(feature = "std"))]
64// Blanket impl
65impl<T> MaybeBitdumpFormattable for T {}
66
67pub mod codegen {
68    // re-export the derive stuff
69    pub use embedded_interfaces_codegen::*;
70}