Skip to content

Commit

Permalink
"describe" CLI, TOML-based testing, Turkish i
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Nov 27, 2024
1 parent 457350c commit d82f8dd
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ read-fonts = "0"
skrifa = "0"
itertools = "0.13.0"
google-fonts-languages = "*"
toml = "0.8.19"
3 changes: 2 additions & 1 deletion shaperglot-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ skrifa = { workspace = true }
read-fonts = { workspace = true }
itertools = { workspace = true }
clap = { version = "4.5.21", features = ["derive"] }
serde_json = "1.0.70"
serde_json = "1.0.70"
toml = {workspace = true }
26 changes: 26 additions & 0 deletions shaperglot-cli/src/describe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use clap::Args;

#[derive(Args)]
pub struct DescribeArgs {
/// Output check definition as TOML
#[arg(long)]
json: bool,
/// Language name or ID to describe
language: String,
}

pub fn describe_command(args: &DescribeArgs, language_database: shaperglot::Languages) {
if let Some(language) = language_database.get_language(&args.language) {
if args.json {
let json = serde_json::to_string_pretty(&language.checks).unwrap();
println!("{}", json);
// }
} else {
for check in language.checks.iter() {
println!("{}", check.description);
}
}
} else {
println!("Language not found");
}
}
7 changes: 7 additions & 0 deletions shaperglot-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use check::{check_command, CheckArgs};
use clap::{Parser, Subcommand};
use describe::{describe_command, DescribeArgs};
use report::{report_command, ReportArgs};

mod check;
mod describe;
mod report;

#[derive(Parser)]
Expand All @@ -19,6 +21,8 @@ enum Commands {
Check(CheckArgs),
/// Report language support
Report(ReportArgs),
/// Describe what is needed to support a language
Describe(DescribeArgs),
}

fn main() {
Expand All @@ -32,5 +36,8 @@ fn main() {
Commands::Report(args) => {
report_command(args, language_database);
}
Commands::Describe(args) => {
describe_command(args, language_database);
}
}
}
7 changes: 6 additions & 1 deletion shaperglot-cli/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ pub struct ReportArgs {
}

pub fn report_command(args: &ReportArgs, language_database: shaperglot::Languages) {
let font_binary = std::fs::read(args.font.as_path()).expect("Failed to read font file");
let font_binary = std::fs::read(args.font.as_path())
.map_err(|e| {
eprintln!("Failed to read font file {}: {}", args.font.display(), e);
std::process::exit(1);
})
.unwrap();
let checker = Checker::new(&font_binary).expect("Failed to load font");
for language in language_database.iter() {
if let Some(filter) = &args.filter {
Expand Down
2 changes: 1 addition & 1 deletion shaperglot-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ unicode-properties = "0.1.3"
unicode-joining-type = "1.0.0"
indexmap = "2"
log = "0.4"
toml = "0.8.19"
toml = { workspace = true }
serde = "1"
ambassador = "0.4.1"
14 changes: 14 additions & 0 deletions shaperglot-lib/manual_checks.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[tr_Latn]]
name = "Small caps i should be dotted"
severity = "Warn"
description = "When the letter 'i' is in small caps, it should be dotted"
scoring_strategy = "Continuous"
weight = 10

[[tr_Latn.implementations]]
type = "ShapingDiffers"
features_optional = true
pairs = [[
{ text = "i", features = ["smcp"] },
{ text = "i", features = ["smcp"], language = "tr" },
]]
2 changes: 1 addition & 1 deletion shaperglot-lib/src/checks/codepoint_coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashSet;

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CodepointCoverage {
strings: HashSet<String>,
code: String,
Expand Down
6 changes: 3 additions & 3 deletions shaperglot-lib/src/checks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ pub trait CheckImplementation {
fn execute(&self, checker: &Checker) -> (Vec<Problem>, usize);
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum ScoringStrategy {
Continuous,
AllOrNothing,
}

#[derive(Delegate, Serialize, Deserialize, Debug)]
#[derive(Delegate, Serialize, Deserialize, Debug, Clone)]
#[delegate(CheckImplementation)]
#[serde(tag = "type")]
pub enum CheckType {
Expand All @@ -36,7 +36,7 @@ pub enum CheckType {
ShapingDiffers(ShapingDiffers),
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Check {
pub name: String,
pub severity: ResultCode,
Expand Down
2 changes: 1 addition & 1 deletion shaperglot-lib/src/checks/no_orphaned_marks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use itertools::Itertools;
use serde::{Deserialize, Serialize};
use unicode_properties::{GeneralCategory, UnicodeGeneralCategory};

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NoOrphanedMarks {
test_strings: Vec<ShapingInput>,
has_orthography: bool,
Expand Down
2 changes: 1 addition & 1 deletion shaperglot-lib/src/checks/shaping_differs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use itertools::Itertools;
use rustybuzz::SerializeFlags;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ShapingDiffers {
pairs: Vec<(ShapingInput, ShapingInput)>,
features_optional: bool,
Expand Down
2 changes: 2 additions & 0 deletions shaperglot-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// #![deny(missing_docs)]
// #![deny(clippy::missing_docs_in_private_items)]
mod checker;
mod checks;
mod font;
Expand Down
7 changes: 4 additions & 3 deletions shaperglot-lib/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use crate::{checks::Check, language::Language};
mod orthographies;
mod positional;
mod small_caps;
mod toml;

use orthographies::OrthographiesProvider;
use positional::PositionalProvider;
use small_caps::SmallCapsProvider;
use toml::TomlProvider;

pub trait Provider {
fn checks_for(&self, language: &Language) -> Vec<Check>;
Expand All @@ -20,9 +23,7 @@ impl Provider for BaseCheckProvider {
checks.extend(OrthographiesProvider.checks_for(language));
checks.extend(SmallCapsProvider.checks_for(language));
checks.extend(PositionalProvider.checks_for(language));

// And any manually coded checks

checks.extend(TomlProvider.checks_for(language));
checks
}
}
21 changes: 21 additions & 0 deletions shaperglot-lib/src/providers/toml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::collections::HashMap;

use crate::{checks::Check, language::Language, Provider};

const TOML_PROFILE: &str = include_str!("../../manual_checks.toml");

use std::sync::LazyLock;

static MANUAL_CHECKS: LazyLock<HashMap<String, Vec<Check>>> =
LazyLock::new(|| toml::from_str(TOML_PROFILE).expect("Could not parse manual checks file: "));

pub struct TomlProvider;

impl Provider for TomlProvider {
fn checks_for(&self, language: &Language) -> Vec<Check> {
MANUAL_CHECKS
.get(language.id())
.cloned()
.unwrap_or_default()
}
}
4 changes: 3 additions & 1 deletion shaperglot-lib/src/shaping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use serde::{Deserialize, Serialize};

use crate::Checker;

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ShapingInput {
pub text: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub features: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub language: Option<String>,
}

Expand Down

0 comments on commit d82f8dd

Please sign in to comment.