1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/// Represents a pair of attribute name and value.
///
/// This struct is used to hold a pair of attribute name and value. The
/// attribute name is a reference to a string slice, and the attribute value is
/// a `Cow` (Copy-on-Write) reference to a string. This allows for efficient
/// handling of both borrowed and owned strings.
///
/// # Fields
///
/// * `0`: The attribute name as a reference to a string slice.
/// * `1`: The attribute value as a `Cow` reference to a string.
pub struct AttrPair<'a>(pub(crate) &'a str, pub(crate) std::borrow::Cow<'a, str>);
/// A collection of attribute pairs.
///
/// This type alias represents a vector of `AttrPair` instances, which can be
/// used to collect and manage multiple attribute pairs.
pub type AttrCollection<'a> = Vec<AttrPair<'a>>;
impl<'a> From<(&'a str, &'a str)> for AttrPair<'a> {
/// Converts a tuple of two string slices into an `AttrPair`.
///
/// This method takes a tuple of two string slices and returns an `AttrPair`
/// instance. The first element of the tuple becomes the attribute name, and
/// the second element becomes the attribute value as a borrowed string.
///
/// # Example
///
/// ```rust
/// # use umya_spreadsheet::AttrPair;
/// let attr_pair = AttrPair::from(("name", "value"));
/// ```
#[inline]
fn from(tuple: (&'a str, &'a str)) -> Self {
AttrPair(tuple.0, std::borrow::Cow::Borrowed(tuple.1))
}
}
impl<'a> From<(&'a str, String)> for AttrPair<'a> {
/// Converts a tuple of a string slice and a `String` into an `AttrPair`.
///
/// This method takes a tuple of a string slice and a `String` and returns
/// an `AttrPair` instance. The string slice becomes the attribute name, and
/// the `String` becomes the attribute value as an owned string.
///
/// # Example
///
/// ```rust
/// # use umya_spreadsheet::AttrPair;
/// let attr_pair = AttrPair::from(("name", String::from("value")));
/// ```
#[inline]
fn from(tuple: (&'a str, String)) -> Self {
AttrPair(tuple.0, std::borrow::Cow::Owned(tuple.1))
}
}
impl<'a> From<(&'a str, &String)> for AttrPair<'a> {
/// Converts a tuple of a string slice and a reference to a `String` into an
/// `AttrPair`.
///
/// This method takes a tuple of a string slice and a reference to a
/// `String` and returns an `AttrPair` instance. The string slice becomes
/// the attribute name, and the `String` becomes the attribute value as an
/// owned string.
///
/// # Example
///
/// ```rust
/// # use umya_spreadsheet::AttrPair;
/// let string = String::from("value");
/// let attr_pair = AttrPair::from(("name", &string));
/// ```
#[inline]
fn from(tuple: (&'a str, &String)) -> Self {
AttrPair(tuple.0, std::borrow::Cow::Owned(tuple.1.to_owned()))
}
}
impl<'a> From<(&'a str, Box<str>)> for AttrPair<'a> {
/// Converts a tuple of a string slice and a `Box` of a string slice into an
/// `AttrPair`.
///
/// This method takes a tuple of a string slice and a `Box` of a string
/// slice and returns an `AttrPair` instance. The string slice becomes the
/// attribute name, and the `Box` of a string slice becomes the attribute
/// value as an owned string.
///
/// # Example
///
/// ```rust
/// # use umya_spreadsheet::AttrPair;
/// let box_str: Box<str> = "value".into();
/// let attr_pair = AttrPair::from(("name", box_str));
/// ```
#[inline]
fn from(tuple: (&'a str, Box<str>)) -> Self {
AttrPair(tuple.0, std::borrow::Cow::Owned(tuple.1.into_string()))
}
}
impl<'a> From<(&'a str, &Box<str>)> for AttrPair<'a> {
/// Converts a tuple of a string slice and a reference to a `Box` of a
/// string slice into an `AttrPair`.
///
/// This method takes a tuple of a string slice and a reference to a `Box`
/// of a string slice and returns an `AttrPair` instance. The string slice
/// becomes the attribute name, and the `Box` of a string slice becomes the
/// attribute value as an owned string.
///
/// # Example
///
/// ```rust
/// use umya_spreadsheet::AttrPair;
/// let box_str: &'static str = Box::leak(Box::new("value"));
/// let attr_pair = AttrPair::from(("name", box_str));
/// ```
#[inline]
fn from(tuple: (&'a str, &Box<str>)) -> Self {
AttrPair(
tuple.0,
std::borrow::Cow::Owned(tuple.1.clone().into_string()),
)
}
}
impl<'a> From<AttrPair<'a>> for (&'a str, std::borrow::Cow<'a, str>) {
/// Converts an `AttrPair` into a tuple of a string slice and a `Cow` of a
/// string slice.
///
/// This method takes an `AttrPair` and returns a tuple of a string slice
/// and a `Cow` of a string slice. The attribute name becomes the string
/// slice, and the attribute value becomes the `Cow` of a string slice.
///
/// # Example
///
/// ```rust
/// use std::borrow::Cow;
///
/// use umya_spreadsheet::AttrPair;
///
/// let attr_pair = AttrPair::from(("name", "value"));
/// let tuple = <(&str, Cow<str>)>::from(attr_pair);
/// ```
#[inline]
fn from(attr_pair: AttrPair<'a>) -> Self {
(attr_pair.0, attr_pair.1)
}
}