custom_display/behavior.rs
1// SPDX-FileCopyrightText: 2026 Marissa (cuddle puddle) <dev@princess.lgbt>
2//
3// SPDX-License-Identifier: MPL-2.0
4
5use std::fmt::{self, Display, Formatter};
6
7/**
8 * The behavior of a [`Displayable`] when a [precision] is set.
9 *
10 * [`Displayable`]: crate::Displayable
11 * [precision]: std::fmt#precision
12 */
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14#[non_exhaustive]
15pub enum PrecisionBehavior {
16 /**
17 * Automatically truncate the [formatted] text to the number of characters
18 * specified as the [precision].
19 *
20 * This is the behavior specified in [`std::fmt`] for non-numeric types.
21 *
22 * [formatted]: crate::CustomDisplay::fmt()
23 * [precision]: std::fmt#precision
24 */
25 AutoTruncate,
26
27 /**
28 * The [precision] will be manually handled (or ignored) by
29 * [`CustomDisplay::fmt()`] and [`CustomDisplay::width_in_chars()`].
30 *
31 * [precision]: std::fmt#precision
32 * [`CustomDisplay::fmt()`]: crate::CustomDisplay::fmt()
33 * [`CustomDisplay::width_in_chars()`]: crate::CustomDisplay::width_in_chars()
34 */
35 Manual,
36}
37
38impl Display for PrecisionBehavior {
39 #[inline]
40 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
41 match *self {
42 Self::AutoTruncate => f.pad("automatically truncate"),
43 Self::Manual => f.pad("handle manually"),
44 }
45 }
46}