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 #[inline]
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 match self {
40 Self::Literal(literal) => f.write_str(&String::from_utf8_lossy(literal)),
41 Self::Component(component) => component.fmt(f),
42 Self::Compound(compound) => compound.fmt(f),
43 Self::Optional(item) => f.debug_tuple("Optional").field(item).finish(),
44 Self::First(items) => f.debug_tuple("First").field(items).finish(),
45 }
46 }
47}
48
49impl From<BorrowedFormatItem<'_>> for OwnedFormatItem {
50 #[inline]
51 fn from(item: BorrowedFormatItem<'_>) -> Self {
52 (&item).into()
53 }
54}
55
56impl From<&BorrowedFormatItem<'_>> for OwnedFormatItem {
57 #[inline]
58 fn from(item: &BorrowedFormatItem<'_>) -> Self {
59 match item {
60 BorrowedFormatItem::Literal(literal) => {
61 Self::Literal(literal.to_vec().into_boxed_slice())
62 }
63 BorrowedFormatItem::Component(component) => Self::Component(*component),
64 BorrowedFormatItem::Compound(compound) => Self::Compound(
65 compound
66 .iter()
67 .cloned()
68 .map(Into::into)
69 .collect::<Vec<_>>()
70 .into_boxed_slice(),
71 ),
72 BorrowedFormatItem::Optional(item) => Self::Optional(Box::new((*item).into())),
73 BorrowedFormatItem::First(items) => Self::First(
74 items
75 .iter()
76 .cloned()
77 .map(Into::into)
78 .collect::<Vec<_>>()
79 .into_boxed_slice(),
80 ),
81 }
82 }
83}
84
85impl From<Vec<BorrowedFormatItem<'_>>> for OwnedFormatItem {
86 #[inline]
87 fn from(items: Vec<BorrowedFormatItem<'_>>) -> Self {
88 items.as_slice().into()
89 }
90}
91
92impl<'a, T: AsRef<[BorrowedFormatItem<'a>]> + ?Sized> From<&T> for OwnedFormatItem {
93 #[inline]
94 fn from(items: &T) -> Self {
95 Self::Compound(
96 items
97 .as_ref()
98 .iter()
99 .cloned()
100 .map(Into::into)
101 .collect::<Vec<_>>()
102 .into_boxed_slice(),
103 )
104 }
105}
106
107impl From<Component> for OwnedFormatItem {
108 #[inline]
109 fn from(component: Component) -> Self {
110 Self::Component(component)
111 }
112}
113
114impl TryFrom<OwnedFormatItem> for Component {
115 type Error = error::DifferentVariant;
116
117 #[inline]
118 fn try_from(value: OwnedFormatItem) -> Result<Self, Self::Error> {
119 match value {
120 OwnedFormatItem::Component(component) => Ok(component),
121 _ => Err(error::DifferentVariant),
122 }
123 }
124}
125
126impl From<Vec<Self>> for OwnedFormatItem {
127 #[inline]
128 fn from(items: Vec<Self>) -> Self {
129 Self::Compound(items.into_boxed_slice())
130 }
131}
132
133impl TryFrom<OwnedFormatItem> for Vec<OwnedFormatItem> {
134 type Error = error::DifferentVariant;
135
136 #[inline]
137 fn try_from(value: OwnedFormatItem) -> Result<Self, Self::Error> {
138 match value {
139 OwnedFormatItem::Compound(items) => Ok(items.into_vec()),
140 _ => Err(error::DifferentVariant),
141 }
142 }
143}
144
145impl PartialEq<Component> for OwnedFormatItem {
146 #[inline]
147 fn eq(&self, rhs: &Component) -> bool {
148 matches!(self, Self::Component(component) if component == rhs)
149 }
150}
151
152impl PartialEq<OwnedFormatItem> for Component {
153 #[inline]
154 fn eq(&self, rhs: &OwnedFormatItem) -> bool {
155 rhs == self
156 }
157}
158
159impl PartialEq<&[Self]> for OwnedFormatItem {
160 #[inline]
161 fn eq(&self, rhs: &&[Self]) -> bool {
162 matches!(self, Self::Compound(compound) if &&**compound == rhs)
163 }
164}
165
166impl PartialEq<OwnedFormatItem> for &[OwnedFormatItem] {
167 #[inline]
168 fn eq(&self, rhs: &OwnedFormatItem) -> bool {
169 rhs == self
170 }
171}