chinese_format/gregorian/date/pattern/flags.rs
1use std::fmt::Display;
2
3/// Flags describing the components in a [DatePattern](super::DatePattern).
4///
5/// This data structure is used, for example, by [DatePattern::validate](super::DatePattern::validate).
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct DatePatternFlags {
8 pub year: bool,
9
10 pub month: bool,
11
12 pub day: bool,
13
14 pub week_day: bool,
15}
16
17/// [DatePatternFlags] can be converted to a [String]
18/// consisting of cumulated single characters - one for each
19/// enabled date component:
20///
21/// ```
22/// use chinese_format::{*, gregorian::*};
23///
24/// # fn main() -> GenericResult<()> {
25/// assert_eq!(
26/// DatePatternFlags {
27/// year: false,
28/// month: false,
29/// day: false,
30/// week_day: false
31/// }.to_string(),
32/// ""
33/// );
34///
35/// assert_eq!(
36/// DatePatternFlags {
37/// year: true,
38/// month: true,
39/// day: true,
40/// week_day: true
41/// }.to_string(),
42/// "ymdw"
43/// );
44///
45/// # Ok(())
46/// # }
47/// ```
48impl Display for DatePatternFlags {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 if self.year {
51 write!(f, "y")?;
52 }
53
54 if self.month {
55 write!(f, "m")?;
56 }
57
58 if self.day {
59 write!(f, "d")?;
60 }
61
62 if self.week_day {
63 write!(f, "w")?;
64 }
65
66 Ok(())
67 }
68}