Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CI and apply clippy suggestions #14

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,20 @@ on:

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: "-D warnings"

jobs:
cargo-test-and-lint:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
lfs: true

- uses: Swatinem/rust-cache@v1

- name: cargo test
uses: actions-rs/cargo@v1
with:
command: test

- name: rustfmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: -p generate-api -- --check

- name: clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: -- -D warnings
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo test
# Don't check the generated file, it doesn't quite match the style of rustfmt yet.
# - run: cargo fmt --check
- run: cargo fmt -p generate-api --check
- run: cargo clippy
- run: cargo clippy -p generate-api
30 changes: 12 additions & 18 deletions generate-api/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,9 @@ impl CodeGenerator {
let mut normalized_langs = BTreeMap::<Lang, String>::new();

for (lang, objects) in objects.iter() {
normalized_langs.insert(lang.to_string(), lang.replace("@", "_"));
normalized_langs.insert(lang.to_string(), lang.replace('@', "_"));

let lang_categories = by_language
.entry(lang.to_string())
.or_insert(BTreeMap::new());
let lang_categories = by_language.entry(lang.to_string()).or_default();

for object in objects.iter() {
if object.name == "LC_COLLATE"
Expand All @@ -278,7 +276,7 @@ impl CodeGenerator {
parser::Value::String(x) => {
lang_categories.insert(
object.name.clone(),
Category::Link(x.replace("@", "_"), object.name.clone()),
Category::Link(x.replace('@', "_"), object.name.clone()),
);
}
x => panic!("unexpected value for key {}: {:?}", key, x),
Expand All @@ -291,9 +289,7 @@ impl CodeGenerator {

let mut fields = BTreeMap::<Field, Value>::new();

let cat_field_meta = field_metadata
.entry(object.name.clone())
.or_insert(BTreeMap::new());
let cat_field_meta = field_metadata.entry(object.name.clone()).or_default();

for (key, group) in &object
.values
Expand All @@ -303,13 +299,12 @@ impl CodeGenerator {
.group_by(|x| x.0.clone())
{
let key = key
.replace("'", "")
.replace("\"", "")
.replace("-", "_")
.replace("=", "eq")
.replace("<", "lt")
.replace(['\'', '\"'], "")
.replace('-', "_")
.replace('=', "eq")
.replace('<', "lt")
.replace("..", "dotdot")
.replace("2", "two")
.replace('2', "two")
.to_uppercase();
let group: Vec<_> = group.map(|x| &x.1).collect();

Expand Down Expand Up @@ -341,8 +336,7 @@ impl CodeGenerator {
fields.insert(key, Value::Array(vec));
} else if group
.iter()
.map(|x| x.iter().map(u8::from))
.flatten()
.flat_map(|x| x.iter().map(u8::from))
.all_equal()
{
meta.mark_array_2d();
Expand Down Expand Up @@ -384,7 +378,7 @@ impl CodeGenerator {
Category::Link(_, _) => {}
Category::Fields(fields) => {
for (field, meta) in all_fields {
if let None = fields.get(field) {
if fields.get(field).is_none() {
fields.insert(field.clone(), Value::Empty);
meta.make_optional();
}
Expand Down Expand Up @@ -529,7 +523,7 @@ impl CodeGenerator {
_ => "".to_string(),
};
write!(f, "\n/// `{}`: {}\n", lang, desc)?;
if lang == &"POSIX" {
if lang == "POSIX" {
writeln!(f, "\n#[default]\n")?;
}
writeln!(f, "\n{},\n", norm)?;
Expand Down
27 changes: 12 additions & 15 deletions generate-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> Result<()> {
let file_name = entry.file_name();
let lang = file_name.to_str().unwrap();

if parser::parse_lang(&lang).is_err() {
if parser::parse_lang(lang).is_err() {
// parse only files for which the name matches a language
// example: wa_BE@euro
continue;
Expand All @@ -41,7 +41,7 @@ fn main() -> Result<()> {

let lib_file = metadata.workspace_root.join("src").join("lib.rs");

if matches!(env::var("CHECK"), Ok(_)) {
if env::var("CHECK").is_ok() {
eprintln!("Calculating checksum...");
let mut f = Sha256::default();

Expand Down Expand Up @@ -69,7 +69,7 @@ fn main() -> Result<()> {
Ok(())
}

fn validate_and_fix(objects: &mut Vec<Object>) {
fn validate_and_fix(objects: &mut [Object]) {
validate_and_fix_t_fmt_ampm(objects);
validate_and_fix_d_t_fmt(objects);
}
Expand All @@ -80,7 +80,7 @@ fn validate_and_fix(objects: &mut Vec<Object>) {
/// for POSIX: `%l:%M:%S %p`.
/// If the locale has empty values for `AM_PM` we set `T_FMT_AMPM` to an empty value, similar to
/// other locales that don't have a 12-hour clock format.
fn validate_and_fix_t_fmt_ampm(objects: &mut Vec<Object>) {
fn validate_and_fix_t_fmt_ampm(objects: &mut [Object]) {
for object in objects.iter_mut() {
if object.name != "LC_TIME" {
continue;
Expand Down Expand Up @@ -109,7 +109,7 @@ fn validate_and_fix_t_fmt_ampm(objects: &mut Vec<Object>) {
/// In some locales `D_T_FMT` refers to other items:
/// to `D_FMT` with `%x`, `T_FMT` with `%X`, and/or `T_FMT_AMPM` with `%r`.
/// Inlining these strings simplifies the implementation of the strftime parser in chrono.
fn validate_and_fix_d_t_fmt(objects: &mut Vec<Object>) {
fn validate_and_fix_d_t_fmt(objects: &mut [Object]) {
for object in objects.iter_mut() {
if object.name != "LC_TIME" {
continue;
Expand All @@ -126,17 +126,14 @@ fn validate_and_fix_d_t_fmt(objects: &mut Vec<Object>) {
}
}
for (key, ref mut value) in object.values.iter_mut() {
match (key.as_str(), value) {
("d_t_fmt", vec) => {
if let Value::String(ref val) = vec[0] {
let d_t_fmt = val
.replace("%x", &d_fmt)
.replace("%X", &t_fmt)
.replace("%r", &t_fmt_ampm);
vec[0] = Value::String(d_t_fmt);
}
if let ("d_t_fmt", vec) = (key.as_str(), value) {
if let Value::String(ref val) = vec[0] {
let d_t_fmt = val
.replace("%x", &d_fmt)
.replace("%X", &t_fmt)
.replace("%r", &t_fmt_ampm);
vec[0] = Value::String(d_t_fmt);
}
_ => {}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions generate-api/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn value<'a, E: ParseError<&'a str>>(
preceded(
|x| sp(x, escape_char, comment_char),
alt((
map_res(integer, |s| i64::from_str_radix(s, 10).map(Value::Integer)),
map_res(integer, |s| s.parse().map(Value::Integer)),
map(|x| string(x, escape_char), Value::String),
map(|x| parse_raw(x, escape_char, comment_char), Value::Raw),
)),
Expand Down Expand Up @@ -248,7 +248,7 @@ fn object<'a, E: ParseError<&'a str>>(
name: name.to_string(),
values: values
.into_iter()
.map(|(k, v)| (k, v.into_iter().filter_map(|x| x).collect()))
.map(|(k, v)| (k, v.into_iter().flatten().collect()))
.collect(),
},
))
Expand Down