Skip to content

Commit

Permalink
feat(aria-data): add DPub and Graphics modules (#4497)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Nov 10, 2024
1 parent fbf4b3d commit 95faf70
Show file tree
Hide file tree
Showing 14 changed files with 4,177 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions crates/biome_aria_metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ version = "0.5.7"


[build-dependencies]
biome_string_case = { workspace = true }
prettyplease = "0.2.25"
proc-macro2 = { workspace = true, features = ["span-locations"] }
quote = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
syn = "2.0.76"
biome_deserialize = { workspace = true }
biome_deserialize_macros = { workspace = true }
biome_string_case = { workspace = true }
prettyplease = "0.2.25"
proc-macro2 = { workspace = true, features = ["span-locations"] }
quote = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
syn = "2.0.76"

[lints]
workspace = true
1 change: 0 additions & 1 deletion crates/biome_aria_metadata/aria-data.json

This file was deleted.

44 changes: 31 additions & 13 deletions crates/biome_aria_metadata/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//! Generate ARAI metadata from `./aria-data.json`

use biome_deserialize::Merge;
use biome_string_case::Case;
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::{format_ident, quote};
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use std::{env, fs, io};

const WAI_ARIA: &str = "../../packages/aria-data/wai-aria-1-3.json";
const GRAPHICS_ARIA: &str = "../../packages/aria-data/graphics-aria-1-0.json";
const DPUB_ARIA: &str = "../../packages/aria-data/dpub-aria-1-1.json";

const ISO_COUNTRIES: &[&str] = &[
"AF", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS",
"BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BA", "BW", "BR", "IO", "VG", "BN",
Expand Down Expand Up @@ -38,12 +43,11 @@ const ISO_LANGUAGES: &[&str] = &[
"wa", "cy", "wo", "xh", "yi", "ji", "yo", "zu",
];

#[derive(Debug, Default, serde::Deserialize)]
#[derive(Debug, Default, biome_deserialize_macros::Merge, serde::Deserialize)]
struct Aria {
roles: BTreeMap<String, AriaRole>,
attributes: BTreeMap<String, AriaAttribute>,
}

impl Aria {
/// Retuurns direct and indirect superclass roles.
fn superclass_roles(&self, role_name: &str) -> Result<BTreeSet<String>, String> {
Expand All @@ -60,7 +64,7 @@ impl Aria {
}
}

#[derive(Debug, Default, serde::Deserialize)]
#[derive(Debug, Default, biome_deserialize_macros::Merge, serde::Deserialize)]
#[serde(default, rename_all = "camelCase")]
struct AriaRole {
description: String,
Expand Down Expand Up @@ -182,7 +186,7 @@ enum AriaAttributeReferenceShortcut {
},
}

#[derive(Debug, Default, serde::Deserialize)]
#[derive(Debug, Default, biome_deserialize_macros::Merge, serde::Deserialize)]
#[serde(default, rename_all = "camelCase")]
struct AriaAttribute {
r#type: AriaAttributeType,
Expand All @@ -195,7 +199,9 @@ struct AriaAttribute {
values: BTreeMap<String, ValueDefinition>,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Deserialize)]
#[derive(
Clone, Copy, Debug, Default, Eq, PartialEq, biome_deserialize_macros::Merge, serde::Deserialize,
)]
enum AriaValueType {
#[default]
#[serde(rename = "true/false")]
Expand All @@ -220,15 +226,15 @@ enum AriaValueType {
Tristate,
}

#[derive(Debug, Default, serde::Deserialize)]
#[derive(Debug, Default, biome_deserialize_macros::Merge, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
enum AriaAttributeType {
#[default]
Property,
State,
}

#[derive(Debug, Default, serde::Deserialize)]
#[derive(Debug, Default, biome_deserialize_macros::Merge, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
struct ValueDefinition {
Expand All @@ -239,13 +245,25 @@ struct ValueDefinition {
fn main() -> io::Result<()> {
// CARGO instructions: rern if one of these files change
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=aria-data.json");
println!("cargo::rerun-if-changed={WAI_ARIA}");
println!("cargo::rerun-if-changed={GRAPHICS_ARIA}");
println!("cargo::rerun-if-changed={DPUB_ARIA}");

let text = std::fs::read_to_string(WAI_ARIA)?;
let wair_aria: Aria = serde_json::from_str(&text)?;

let text = std::fs::read_to_string(DPUB_ARIA)?;
let dpub_aria: Aria = serde_json::from_str(&text)?;

let text = std::fs::read_to_string(GRAPHICS_ARIA)?;
let graphics_aria: Aria = serde_json::from_str(&text)?;

let text = std::fs::read_to_string("aria-data.json")?;
let data: Aria = serde_json::from_str(&text)?;
let mut aria = graphics_aria;
aria.merge_with(dpub_aria);
aria.merge_with(wair_aria);

let aria_attributes = generate_aria_attributes(&data.attributes);
let aria_roles = generate_aria_roles(&data);
let aria_attributes = generate_aria_attributes(&aria.attributes);
let aria_roles = generate_aria_roles(&aria);

let iso_countries = generate_enums(ISO_COUNTRIES, "IsoCountries");
let iso_languages = generate_enums(ISO_LANGUAGES, "IsoLanguages");
Expand All @@ -268,7 +286,7 @@ fn main() -> io::Result<()> {
// We print the code even if it cannot be parsed,
// this allows to debug the code by directly looking at it.
let out_dir = env::var("OUT_DIR").unwrap();
fs::write(PathBuf::from(out_dir).join("roles_and_properties.rs"), ast)?;
fs::write(PathBuf::from(out_dir).join("roles_and_attributes.rs"), ast)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_aria_metadata/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include!(concat!(env!("OUT_DIR"), "/roles_and_properties.rs"));
include!(concat!(env!("OUT_DIR"), "/roles_and_attributes.rs"));

pub const ISO_COUNTRIES: [&str; 233] = [
"AF", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS",
Expand Down
6 changes: 6 additions & 0 deletions crates/biome_deserialize/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ impl<K: Hash + Eq, V: Merge, S: Default + BuildHasher> Merge
}
}

impl<V: Ord> Merge for std::collections::BTreeSet<V> {
fn merge_with(&mut self, other: Self) {
self.extend(other);
}
}

impl<K: Ord, V: Merge> Merge for std::collections::BTreeMap<K, V> {
fn merge_with(&mut self, other: Self) {
for (k, v) in other {
Expand Down
8 changes: 4 additions & 4 deletions packages/aria-data/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# ARIA data

This package provides a script that extracts data from the ARIA specification.
This is a best effort approach because the ARIA specification is in a semi-structured representation.
This package provides a script that extracts data from an ARIA specification.
This is a best effort approach because the ARIA specifications are in a semi-structured representation.

Just call the script with the version of the ARIA specification and write t oa given file:
Just call the script with the name an dversion of the ARIA specification and write to a given file:

```shell
node generate-aria-data.js 1.2 >| aria-data-1-2.json
node generate-aria-data.js wai-aria-1.2 >| wai-aria-1-2.json
```
Loading

0 comments on commit 95faf70

Please sign in to comment.