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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
use crate::{Chinese, ChineseFormat, Variant};
/// Creates [ChineseVec] instances with elegant simplicity.
///
/// It works almost like [ChineseVec::from], but you just need to pass *instances* of [ChineseFormat] instead of *references* - the `&` is added automatically.
///
/// ```
/// use chinese_format::*;
///
/// let empty_vec = chinese_vec!(Variant::Simplified, []);
///
/// assert_eq!(empty_vec.collect(), Chinese {
/// logograms: "".to_string(),
/// omissible: true
/// });
///
///
/// let one_item_vec = chinese_vec!(Variant::Simplified, [90]);
///
/// assert_eq!(one_item_vec.collect(), Chinese {
/// logograms: "九十".to_string(),
/// omissible: false
/// });
///
///
/// let two_item_vec = chinese_vec!(Variant::Simplified, [
/// "你好",
/// 38,
/// ]);
///
/// assert_eq!(two_item_vec.collect(), Chinese {
/// logograms: "你好三十八".to_string(),
/// omissible: false
/// });
/// ```
///
#[macro_export]
macro_rules! chinese_vec {
($variant: expr, [$($item: expr),* $(,)?]) => {{
let chinese_vector: $crate::ChineseVec =
vec![ $($item.to_chinese($variant)),* ].into();
chinese_vector
}};
}
/// A vector containing [Chinese] expressions.
///
/// It can be manipulated with functional methods
/// such as [trim_end](Self::trim_end) - and the overall sequence can be
/// reduced to a single [Chinese] instance via [collect](Self::collect).
///
/// It can be instantiated using a `.into()` conversion from a `Vec<Chinese>`,
/// but also - and especially :
///
/// * using the [chinese_vec] macro, passing instances implementing [ChineseFormat].
///
/// * via its [from](Self::from) method for [ChineseFormat] instances,
/// for more fine-grained control.
///
/// ```
/// use chinese_format::*;
///
/// let first_vec: ChineseVec = vec![
/// Chinese {
/// logograms: "很".to_string(),
/// omissible: false
/// },
/// Chinese {
/// logograms: "好".to_string(),
/// omissible: false
/// }
/// ].into();
///
/// assert_eq!(first_vec.collect(), Chinese {
/// logograms: "很好".to_string(),
/// omissible: false
/// });
///
///
/// let second_vec = chinese_vec!(Variant::Traditional, [
/// Count(2),
/// ("厘米", "釐米")
/// ]);
///
/// assert_eq!(second_vec.collect(), Chinese {
/// logograms: "兩釐米".to_string(),
/// omissible: false
/// });
///
///
/// let third_vec = ChineseVec::from(Variant::Traditional, vec![
/// &Count(7),
/// &("厘米", "釐米")
/// ]);
///
/// assert_eq!(third_vec.collect(), Chinese {
/// logograms: "七釐米".to_string(),
/// omissible: false
/// });
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChineseVec(Vec<Chinese>);
impl ChineseVec {
/// Creates a new [ChineseVec] by converting a sequence of [ChineseFormat],
/// according to the given [Variant].
///
/// Except specific needs, you'll most often prefer [chinese_vec].
///
/// ```
/// use chinese_format::*;
///
/// //Toy example based on the real implementation of Fraction.
/// let chinese_vec = ChineseVec::from(
/// Variant::Simplified,
/// vec![
/// &Sign(-5),
/// &7,
/// &"分之",
/// &5
/// ]
/// );
///
/// assert_eq!(chinese_vec.collect(), Chinese {
/// logograms: "负七分之五".to_string(),
/// omissible: false
/// });
/// ```
pub fn from(variant: Variant, source: Vec<&dyn ChineseFormat>) -> ChineseVec {
Self(
source
.into_iter()
.map(|item| item.to_chinese(variant))
.collect(),
)
}
/// Removes the left-most sequence of [Chinese] characters that are [omissible](Chinese::omissible).
///
/// ```
/// use chinese_format::*;
///
/// let chinese_vec = chinese_vec!(Variant::Simplified, [
/// 0,
/// "",
/// Count(0),
/// 8,
/// "",
/// "好",
/// "",
/// 0,
/// Count(0)
/// ]).trim_start();
///
/// assert_eq!(chinese_vec.collect(), Chinese {
/// logograms: "八好零零".to_string(),
/// omissible: false
/// });
/// ```
pub fn trim_start(&self) -> Self {
ChineseVec(
self.0
.iter()
.skip_while(|item| item.omissible)
.cloned()
.collect(),
)
}
/// Removes the right-most sequence of [Chinese] characters that are [omissible](Chinese::omissible).
///
/// ```
/// use chinese_format::*;
///
/// let chinese_vec = chinese_vec!(Variant::Simplified, [
/// 0,
/// "",
/// Count(0),
/// 8,
/// "",
/// "好",
/// "",
/// 0,
/// Count(0)
/// ]).trim_end();
///
/// assert_eq!(chinese_vec.collect(), Chinese {
/// logograms: "零零八好".to_string(),
/// omissible: false
/// });
/// ```
pub fn trim_end(&self) -> Self {
let mut rev_result: Vec<Chinese> = vec![];
for item in self.0.iter().rev().skip_while(|item| item.omissible) {
rev_result.push(item.clone());
}
let result = rev_result.into_iter().rev().collect();
ChineseVec(result)
}
/// Concatenates all the [Chinese] expressions into a single one.
///
/// The resulting [Chinese] is defined as follows:
///
/// * the [logograms](Chinese::logograms) are obtained by concatenating all the logograms.
///
/// * it is [omissible](Chinese::omissible) in one of two cases:
///
/// * the vector is empty.
///
/// * all of its items are [omissible](Chinese::omissible).
///
/// ```
/// use chinese_format::{*, length::*};
///
/// let basic: ChineseVec = chinese_vec!(Variant::Simplified, [
/// 9,
/// "点",
/// 4,
/// "分"
/// ]);
/// assert_eq!(basic.collect(), Chinese {
/// logograms: "九点四分".to_string(),
/// omissible: false
/// });
///
/// let empty: ChineseVec = vec![].into();
/// assert_eq!(empty.collect(), Chinese {
/// logograms: "".to_string(),
/// omissible: true
/// });
///
/// let only_omissible = chinese_vec!(Variant::Simplified, [
/// 0,
/// Count(0),
/// "",
/// chinese_vec!(Variant::Simplified, [
/// Sign(9),
/// EmptyPlaceholder::new(&Meter::new(0))
/// ]),
/// ("", "Test")
/// ]);
/// assert_eq!(only_omissible.collect(), Chinese {
/// logograms: "零零".to_string(),
/// omissible: true
/// })
/// ```
pub fn collect(&self) -> Chinese {
Chinese {
logograms: self
.0
.iter()
.map(|item| item.logograms.as_str())
.collect::<Vec<_>>()
.join(""),
omissible: self.0.is_empty() || self.0.iter().all(|item| item.omissible),
}
}
}
/// A [ChineseVec] can be infallibly built from a [Vec] of [Chinese]`.
///
/// ```
/// use chinese_format::*;
///
/// let chinese_vec: ChineseVec = vec![
/// Chinese {
/// logograms: "没".to_string(),
/// omissible: false
/// },
/// Chinese {
/// logograms: "关".to_string(),
/// omissible: false
/// },
/// Chinese {
/// logograms: "系".to_string(),
/// omissible: false
/// }
/// ].into();
///
/// assert_eq!(chinese_vec.collect(), Chinese {
/// logograms: "没关系".to_string(),
/// omissible: false
/// });
/// ```
impl From<Vec<Chinese>> for ChineseVec {
fn from(value: Vec<Chinese>) -> Self {
Self(value)
}
}
/// [ChineseVec] supports [ChineseFormat] via its [collect](Self::collect) method.
///
/// Of course, the [Variant] parameter is ignored - because the
/// [Chinese] instances are already available in the vector.
///
/// ```
/// use chinese_format::*;
///
/// let chinese_vec = chinese_vec!(Variant::Simplified, [
/// "飞",
/// "机"
/// ]);
///
/// //In traditional script, 飞 is written 飛! No conversion can be performed.
/// assert_eq!(chinese_vec.to_chinese(Variant::Traditional), "飞机");
/// ```
impl ChineseFormat for ChineseVec {
fn to_chinese(&self, _variant: Variant) -> Chinese {
self.collect()
}
}
///Any &[ChineseVec] can be infallibly converted to a [Vec] of [Chinese].
///
/// ```
/// use chinese_format::*;
///
/// let chinese_vec = chinese_vec!(Variant::Simplified, [
/// "你好",
/// "生日快乐"
/// ]);
///
/// let vec_of_chinese: Vec<Chinese> = (&chinese_vec).into();
///
/// assert_eq!(vec_of_chinese, vec![
/// Chinese {
/// logograms: "你好".to_string(),
/// omissible: false
/// },
///
/// Chinese {
/// logograms: "生日快乐".to_string(),
/// omissible: false
/// }
/// ]);
/// ```
impl From<&ChineseVec> for Vec<Chinese> {
fn from(value: &ChineseVec) -> Self {
value.0.to_vec()
}
}