Skip to content
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

Add impls for the Defmt logging crate #1632

Open
wants to merge 1 commit into
base: main
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ now = ["std"]
oldtime = []
wasmbind = ["wasm-bindgen", "js-sys"]
unstable-locales = ["pure-rust-locales"]
defmt = ["dep:defmt"]
# Note that rkyv-16, rkyv-32, and rkyv-64 are mutually exclusive.
rkyv = ["dep:rkyv", "rkyv/size_32"]
rkyv-16 = ["dep:rkyv", "rkyv?/size_16"]
Expand All @@ -43,6 +44,7 @@ serde = { version = "1.0.99", default-features = false, optional = true }
pure-rust-locales = { version = "0.8", optional = true }
rkyv = { version = "0.7.43", optional = true, default-features = false }
arbitrary = { version = "1.0.0", features = ["derive"], optional = true }
defmt = { version = "0.3.8", optional = true }

[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
wasm-bindgen = { version = "0.2", optional = true }
Expand Down
30 changes: 19 additions & 11 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@

//! ISO 8601 date and time with time zone.

#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
use alloc::string::String;
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt::Write;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::time::Duration;
use core::{fmt, hash, str};
#[cfg(feature = "std")]
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(all(feature = "unstable-locales", feature = "alloc"))]
use crate::format::Locale;
use crate::format::{
Expand All @@ -30,6 +19,18 @@ use crate::offset::{FixedOffset, LocalResult, Offset, TimeZone, Utc};
use crate::Date;
use crate::{expect, try_opt};
use crate::{Datelike, Months, TimeDelta, Timelike, Weekday};
#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
use alloc::string::String;
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt::Write;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::time::Duration;
use core::{fmt, hash, str};
#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};
#[cfg(feature = "std")]
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};
Expand Down Expand Up @@ -1093,6 +1094,13 @@ impl DateTime<FixedOffset> {
}
}

#[cfg(feature = "defmt")]
impl<Tz: TimeZone> Format for DateTime<Tz> {
fn format(&self, fmt: Formatter) {
defmt::write!(fmt, "{=i32:04}-{=u32:02}-{=u32:02}", self.year(), self.month(), self.day());
}
}

impl<Tz: TimeZone> DateTime<Tz>
where
Tz::Offset: fmt::Display,
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ pub use time_delta::TimeDelta;
pub type Duration = TimeDelta;

use core::fmt;
#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};

/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
pub mod prelude {
Expand Down Expand Up @@ -678,6 +680,13 @@ impl fmt::Display for OutOfRange {
}
}

#[cfg(feature = "defmt")]
impl Format for OutOfRange {
fn format(&self, fmt: Formatter) {
defmt::write!(fmt, "out of range");
}
}

impl fmt::Debug for OutOfRange {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "out of range")
Expand Down
10 changes: 9 additions & 1 deletion src/month.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt;

#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -240,6 +241,13 @@ impl Months {
}
}

#[cfg(feature = "defmt")]
impl Format for Months {
fn format(&self, fmt: Formatter) {
defmt::write!(fmt, "Months({=u32})", self.as_u32());
}
}

/// An error resulting from reading `<Month>` value with `FromStr`.
#[derive(Clone, PartialEq, Eq)]
pub struct ParseMonthError {
Expand Down
10 changes: 9 additions & 1 deletion src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use core::iter::FusedIterator;
use core::num::NonZeroI32;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::{fmt, str};

#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -2238,6 +2239,13 @@ impl fmt::Debug for NaiveDate {
}
}

#[cfg(feature = "defmt")]
impl Format for NaiveDate {
fn format(&self, fmt: Formatter) {
defmt::write!(fmt, "{=i32:04}-{=u32:02}-{=u32:02}", self.year(), self.month(), self.day());
}
}

/// The `Display` output of the naive date `d` is the same as
/// [`d.format("%Y-%m-%d")`](crate::format::strftime).
///
Expand Down
10 changes: 9 additions & 1 deletion src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use core::fmt::Write;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::time::Duration;
use core::{fmt, str};

#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -2149,3 +2150,10 @@ impl Default for NaiveDateTime {
Self::UNIX_EPOCH
}
}

#[cfg(feature = "defmt")]
impl Format for NaiveDateTime {
fn format(&self, fmt: Formatter) {
defmt::write!(fmt, "{}T{}", self.date(), self.time());
}
}
10 changes: 9 additions & 1 deletion src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use core::borrow::Borrow;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::time::Duration;
use core::{fmt, str};

#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -1641,3 +1642,10 @@ impl Default for NaiveTime {
NaiveTime::from_hms_opt(0, 0, 0).unwrap()
}
}

#[cfg(feature = "defmt")]
impl Format for NaiveTime {
fn format(&self, fmt: Formatter) {
defmt::write!(fmt, "{=u32}:{=u32}:{=u32}", self.hour(), self.minute(), self.second());
}
}
10 changes: 9 additions & 1 deletion src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

use core::fmt;
use core::str::FromStr;

#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -152,6 +153,13 @@ impl Offset for FixedOffset {
}
}

#[cfg(feature = "defmt")]
impl Format for FixedOffset {
fn format(&self, fmt: Formatter) {
defmt::write!(fmt, "FixedOffset({=i32})", self.local_minus_utc());
}
}

impl fmt::Debug for FixedOffset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let offset = self.local_minus_utc;
Expand Down
14 changes: 11 additions & 3 deletions src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
//! The UTC (Coordinated Universal Time) time zone.

use core::fmt;
#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};
#[cfg(all(
feature = "now",
not(all(
Expand All @@ -14,9 +18,6 @@ use core::fmt;
))]
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

use super::{FixedOffset, MappedLocalTime, Offset, TimeZone};
use crate::naive::{NaiveDate, NaiveDateTime};
#[cfg(feature = "now")]
Expand Down Expand Up @@ -150,3 +151,10 @@ impl fmt::Display for Utc {
write!(f, "UTC")
}
}

#[cfg(feature = "defmt")]
impl Format for Utc {
fn format(&self, fmt: Formatter) {
defmt::write!(fmt, "UTC");
}
}
17 changes: 15 additions & 2 deletions src/time_delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

//! Temporal quantification

use crate::{expect, try_opt};
use core::fmt;
use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
use core::time::Duration;
#[cfg(feature = "defmt")]
use defmt::{Format, Formatter};
#[cfg(feature = "std")]
use std::error::Error;

use crate::{expect, try_opt};

#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -593,6 +594,18 @@ impl fmt::Display for TimeDelta {
}
}

#[cfg(feature = "defmt")]
impl Format for TimeDelta {
fn format(&self, fmt: Formatter) {
defmt::write!(
fmt,
"TimeDelta {{ secs: {=i64}, nanos: {=i32} }}",
self.num_seconds(),
self.subsec_nanos()
);
}
}

/// Represents error when converting `TimeDelta` to/from a standard library
/// implementation
///
Expand Down
Loading