platform-switch

Crates.ioplatform-switch
lib.rsplatform-switch
version0.1.0
sourcesrc
created_at2023-05-14 17:57:05.700825
updated_at2023-05-14 17:57:05.700825
descriptionNamespace facades for switching between platforms.
homepage
repository
max_upload_size
id864399
size25,103
Jake Swensen (jdswensen)

documentation

README

platform-switch

Namespace facades for switching between platforms.

Motivation

Sometimes code needs to be shared between a contrainted platform (such as an embedded system) and a target that has a more fully featured environment. This crate provides a series of module wrappers that can be used in combination with cargo manifest feature definitions to switch between options at compile time.

Example

Using thiserror with defmt enabled on an embedded system:

[features]
default = ["std"]
std = ["platform-switch/std_error"]
mcu = ["platform-switch/core_error", "platform-switch/defmt"] # Requires nightly
use platform_switch::log as log;
use platform_switch::thiserror as thiserror;

#[derive(thiserror::Error, Debug)]
enum ExampleError {
    #[error("Error #1")]
    Error1,
    #[error("Error #2")]
    Error2,
}

fn error_logger() {
    log::trace!("Trace");
    log::debug!("Debug");
    log::info!("Info");
    log::warn!("Warn");
    log::error!("Error");

    log::error!("Error: {}", ExampleError::Error1);
    log::error!("Error: {}", ExampleError::Error2);
}

Stable Toolchain

If a stable toolchain is required, thiserror can be disabled with the following features and attributes:

[features]
default = ["std"]
std = ["thiserror", "platform-switch/std_error"]
mcu = ["platform-switch/defmt"]
thiserror = []
use platform_switch::log as log;
use platform_switch::thiserror as thiserror;

#[derive(Debug)]
#[cfg_attr(feature = "thiserror", derive(thiserror::Error))]
enum ExampleError {
    #[cfg_attr(feature = "thiserror", error("Error #1"))]
    Error1,

    #[cfg_attr(feature = "thiserror", error("Error #2"))]
    Error2,
}

License

Mozilla Public License Version 2.0

Commit count: 0

cargo fmt