time/format_description/
owned_format_item.rs1use alloc::boxed::Box;
4use alloc::string::String;
5use alloc::vec::Vec;
6use core::fmt;
7
8use crate::error;
9use crate::format_description::{BorrowedFormatItem, Component};
10
11#[non_exhaustive]
13#[derive(Clone, PartialEq, Eq)]
14pub enum OwnedFormatItem {
15 Literal(Box<[u8]>),
20 Component(Component),
22 Compound(Box<[Self]>),
25 Optional(Box<Self>),
30 First(Box<[Self]>),
34}
35
36impl fmt::Debug for OwnedFormatItem {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 match self {
39 Self::Literal(literal) => f.write_str(&String::from_utf8_lossy(literal)),
40 Self::Component(component) => component.fmt(f),
41 Self::Compound(compound) => compound.fmt(f),
42 Self::Optional(item) => f.debug_tuple("Optional").field(item).finish(),
43 Self::First(items) => f.debug_tuple("First").field(items).finish(),
44 }
45 }
46}
47
48impl From<BorrowedFormatItem<'_>> for OwnedFormatItem {
50 fn from(item: BorrowedFormatItem<'_>) -> Self {
51 (&item).into()
52 }
53}
54
55impl From<&BorrowedFormatItem<'_>> for OwnedFormatItem {
56 fn from(item: &BorrowedFormatItem<'_>) -> Self {
57 match item {
58 BorrowedFormatItem::Literal(literal) => {
59 Self::Literal(literal.to_vec().into_boxed_slice())
60 }
61 BorrowedFormatItem::Component(component) => Self::Component(*component),
62 BorrowedFormatItem::Compound(compound) => Self::Compound(
63 compound
64 .iter()
65 .cloned()
66 .map(Into::into)
67 .collect::<Vec<_>>()
68 .into_boxed_slice(),
69 ),
70 BorrowedFormatItem::Optional(item) => Self::Optional(Box::new((*item).into())),
71 BorrowedFormatItem::First(items) => Self::First(
72 items
73 .iter()
74 .cloned()
75 .map(Into::into)
76 .collect::<Vec<_>>()
77 .into_boxed_slice(),
78 ),
79 }
80 }
81}
82
83impl From<Vec<BorrowedFormatItem<'_>>> for OwnedFormatItem {
84 fn from(items: Vec<BorrowedFormatItem<'_>>) -> Self {
85 items.as_slice().into()
86 }
87}
88
89impl<'a, T: AsRef<[BorrowedFormatItem<'a>]> + ?Sized> From<&T> for OwnedFormatItem {
90 fn from(items: &T) -> Self {
91 Self::Compound(
92 items
93 .as_ref()
94 .iter()
95 .cloned()
96 .map(Into::into)
97 .collect::<Vec<_>>()
98 .into_boxed_slice(),
99 )
100 }
101}
102impl From<Component> for OwnedFormatItem {
106 fn from(component: Component) -> Self {
107 Self::Component(component)
108 }
109}
110
111impl TryFrom<OwnedFormatItem> for Component {
112 type Error = error::DifferentVariant;
113
114 fn try_from(value: OwnedFormatItem) -> Result<Self, Self::Error> {
115 match value {
116 OwnedFormatItem::Component(component) => Ok(component),
117 _ => Err(error::DifferentVariant),
118 }
119 }
120}
121
122impl From<Vec<Self>> for OwnedFormatItem {
123 fn from(items: Vec<Self>) -> Self {
124 Self::Compound(items.into_boxed_slice())
125 }
126}
127
128impl TryFrom<OwnedFormatItem> for Vec<OwnedFormatItem> {
129 type Error = error::DifferentVariant;
130
131 fn try_from(value: OwnedFormatItem) -> Result<Self, Self::Error> {
132 match value {
133 OwnedFormatItem::Compound(items) => Ok(items.into_vec()),
134 _ => Err(error::DifferentVariant),
135 }
136 }
137}
138impl PartialEq<Component> for OwnedFormatItem {
142 fn eq(&self, rhs: &Component) -> bool {
143 matches!(self, Self::Component(component) if component == rhs)
144 }
145}
146
147impl PartialEq<OwnedFormatItem> for Component {
148 fn eq(&self, rhs: &OwnedFormatItem) -> bool {
149 rhs == self
150 }
151}
152
153impl PartialEq<&[Self]> for OwnedFormatItem {
154 fn eq(&self, rhs: &&[Self]) -> bool {
155 matches!(self, Self::Compound(compound) if &&**compound == rhs)
156 }
157}
158
159impl PartialEq<OwnedFormatItem> for &[OwnedFormatItem] {
160 fn eq(&self, rhs: &OwnedFormatItem) -> bool {
161 rhs == self
162 }
163}
164