From fd9d0ddc07143aef68845b8c3cd8c85ebd571369 Mon Sep 17 00:00:00 2001 From: Ben Ford Date: Tue, 26 Nov 2024 17:08:30 +0000 Subject: [PATCH] Add impls for the Defmt logging crate - Add impls for the main structs where it is obvious how to do so - Gate this behind a new cargo feature called "defmt" --- Cargo.toml | 2 ++ src/datetime/mod.rs | 30 +++++++++++++++++++----------- src/lib.rs | 9 +++++++++ src/month.rs | 10 +++++++++- src/naive/date/mod.rs | 10 +++++++++- src/naive/datetime/mod.rs | 10 +++++++++- src/naive/time/mod.rs | 10 +++++++++- src/offset/fixed.rs | 10 +++++++++- src/offset/utc.rs | 14 +++++++++++--- src/time_delta.rs | 17 +++++++++++++++-- 10 files changed, 101 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ab4b045c48..ccc47becb7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] @@ -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 } diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index db91561450..36ee6f433f 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -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::{ @@ -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}; @@ -1093,6 +1094,13 @@ impl DateTime { } } +#[cfg(feature = "defmt")] +impl Format for DateTime { + fn format(&self, fmt: Formatter) { + defmt::write!(fmt, "{=i32:04}-{=u32:02}-{=u32:02}", self.year(), self.month(), self.day()); + } +} + impl DateTime where Tz::Offset: fmt::Display, diff --git a/src/lib.rs b/src/lib.rs index a7c603a3e9..5e70a74b7c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { @@ -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") diff --git a/src/month.rs b/src/month.rs index f8033fccd6..5f4881c796 100644 --- a/src/month.rs +++ b/src/month.rs @@ -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}; @@ -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 `` value with `FromStr`. #[derive(Clone, PartialEq, Eq)] pub struct ParseMonthError { diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index bd70d52b31..c4e0c26d03 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -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}; @@ -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). /// diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index a2ffc69674..25711dffec 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -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}; @@ -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()); + } +} diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index f32d828080..f9af5fc192 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -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}; @@ -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()); + } +} diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index e7382bed1d..5d2f17c2cd 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -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}; @@ -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; diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 5ae26ed86d..a5b32cdc68 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -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( @@ -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")] @@ -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"); + } +} diff --git a/src/time_delta.rs b/src/time_delta.rs index 3eb041ad57..b6f072a17b 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -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}; @@ -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 ///