Struct Config
pub struct Config<C> { /* private fields */ }Expand description
Configuration for customizing the behavior of formatting or parsing.
One important use case enabled by this type is the ability to set a
Custom trait implementation to use when calling
BrokenDownTime::format_with_config
or BrokenDownTime::to_string_with_config.
It is generally expected that most callers should not need to use this. At present, the only reasons to use this are:
- If you specifically need to provide locale aware formatting within
the context of
strtime-style APIs. Unless you specifically need this, you should prefer using theicucrate viajiff-icuto do type conversions. More specifically, follow the examples in theicu::datetimemodule for a modern approach to datetime localization that leverages Unicode. - If you specifically need to opt into “lenient” parsing such that most errors when formatting are silently ignored.
§Example
This example shows how to use PosixCustom via strtime formatting:
use jiff::{civil, fmt::strtime::{BrokenDownTime, PosixCustom, Config}};
let config = Config::new().custom(PosixCustom::new());
let dt = civil::date(2025, 7, 1).at(17, 30, 0, 0);
let tm = BrokenDownTime::from(dt);
assert_eq!(
tm.to_string_with_config(&config, "%c")?,
"Tue Jul 1 17:30:00 2025",
);
Implementations§
§impl Config<DefaultCustom>
impl Config<DefaultCustom>
pub const fn new() -> Config<DefaultCustom>
pub const fn new() -> Config<DefaultCustom>
Create a new default Config that uses DefaultCustom.
§impl<C> Config<C>
impl<C> Config<C>
pub fn custom<U>(self, custom: U) -> Config<U>where
U: Custom,
pub fn custom<U>(self, custom: U) -> Config<U>where
U: Custom,
Set the implementation of Custom to use in strtime-style APIs
that use this configuration.
pub fn lenient(self, yes: bool) -> Config<C>
pub fn lenient(self, yes: bool) -> Config<C>
Enable lenient formatting.
When this is enabled, most errors that occur during formatting are
silently ignored. For example, if you try to format %z with a
BrokenDownTime that lacks a time zone offset, this would normally
result in an error. In contrast, when lenient mode is enabled, this
would just result in %z being written literally.
This currently has no effect on parsing, although this may change in the future.
Lenient formatting is disabled by default. It is strongly recommended to keep it disabled in order to avoid mysterious failure modes for end users. You should only enable this if you have strict requirements to conform to legacy software behavior.
§API stability
An artifact of lenient parsing is that most error behaviors are
squashed in favor of writing the errant conversion specifier literally.
This means that if you use something like %+, which is currently
unrecognized, then that will result in a literal %+ in the string
returned. But Jiff may one day add support for %+ in a semver
compatible release.
Stated differently, the set of unknown or error conditions is not fixed and may decrease with time. This in turn means that the precise conditions under which a conversion specifier gets written literally to the resulting string may change over time in semver compatible releases of Jiff.
The alternative would be that Jiff could never add any new conversion
specifiers without making a semver incompatible release. The intent
of this policy is to avoid that scenario and permit reasonable
evolution of Jiff’s strtime support.
§Example
This example shows how %z will be written literally if it would
otherwise fail:
use jiff::{civil, fmt::strtime::{BrokenDownTime, Config}};
let tm = BrokenDownTime::from(civil::date(2025, 4, 30));
assert_eq!(
tm.to_string("%F %z").unwrap_err().to_string(),
"strftime formatting failed: %z failed: \
requires offset to format time zone offset",
);
// Now enable lenient mode:
let config = Config::new().lenient(true);
assert_eq!(
tm.to_string_with_config(&config, "%F %z").unwrap(),
"2025-04-30 %z",
);
// Lenient mode also applies when using an unsupported
// or unrecognized conversion specifier. This would
// normally return an error for example:
assert_eq!(
tm.to_string_with_config(&config, "%+ %0").unwrap(),
"%+ %0",
);Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for Config<C>where
C: Freeze,
impl<C> RefUnwindSafe for Config<C>where
C: RefUnwindSafe,
impl<C> Send for Config<C>where
C: Send,
impl<C> Sync for Config<C>where
C: Sync,
impl<C> Unpin for Config<C>where
C: Unpin,
impl<C> UnwindSafe for Config<C>where
C: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);