Skip to content

Commit

Permalink
fmt and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Aug 17, 2023
1 parent cd0d86c commit 5551d6b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
27 changes: 2 additions & 25 deletions provider/datagen/src/baked_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
//! # }
//! ```

use crate::helpers::ZeroOneOrTwo;
use databake::*;
use icu_provider::datagen::*;
use icu_provider::prelude::*;
Expand Down Expand Up @@ -468,43 +469,19 @@ impl BakedExporter {
let mut map = BTreeMap::new();
let mut statics = Vec::new();

#[derive(Copy, Clone)]
enum ZeroOneOrTwo<T> {
Zero,
One(T),
Two(T, T)
}
impl<T: Copy> Iterator for ZeroOneOrTwo<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match *self {
ZeroOneOrTwo::Zero => None,
ZeroOneOrTwo::One(a) => {
*self = ZeroOneOrTwo::Zero;
Some(a)
}
ZeroOneOrTwo::Two(a, b) => {
*self = ZeroOneOrTwo::One(b);
Some(a)
}
}
}
}

for (bake, locales) in values {
let first_locale = locales.iter().next().unwrap();
let anchor = syn::parse_str::<syn::Ident>(
&first_locale
.chars()
.map(|b| b.to_ascii_uppercase())
.flat_map(|ch| {
if ch == AuxiliaryKey::separator() as char {
// Replace the aux key separator with double-underscore
ZeroOneOrTwo::Two('_', '_')
} else if ch == '-' {
ZeroOneOrTwo::One('_')
} else {
ZeroOneOrTwo::One(ch)
ZeroOneOrTwo::One(ch.to_ascii_uppercase())
}
})
.collect::<String>(),
Expand Down
28 changes: 28 additions & 0 deletions provider/datagen/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

/// A container for 0, 1, or 2 elements that owns its data and supports iteration
#[derive(Copy, Clone)]
pub(crate) enum ZeroOneOrTwo<T> {
Zero,
One(T),
Two(T, T),
}

impl<T: Copy> Iterator for ZeroOneOrTwo<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match *self {
ZeroOneOrTwo::Zero => None,
ZeroOneOrTwo::One(a) => {
*self = ZeroOneOrTwo::Zero;
Some(a)
}
ZeroOneOrTwo::Two(a, b) => {
*self = ZeroOneOrTwo::One(b);
Some(a)
}
}
}
}
1 change: 1 addition & 0 deletions provider/datagen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#![warn(missing_docs)]

mod error;
mod helpers;
mod registry;
mod source;
mod transform;
Expand Down

0 comments on commit 5551d6b

Please sign in to comment.