Skip to content

Commit

Permalink
text-rendering: use clap for cli interface
Browse files Browse the repository at this point in the history
This is part of the ongoing effort to remove
our own cli parser
  • Loading branch information
simonwuelker committed Aug 10, 2024
1 parent a6d0ebb commit b8a3152
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 51 deletions.
3 changes: 2 additions & 1 deletion tests/runners/text-rendering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
name = "text-rendering"
version = "0.1.0"
edition = "2021"
description = "A testrunner for the text rendering tests"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
font = { workspace = true }
cli = { workspace = true }
clap = { workspace = true }
71 changes: 21 additions & 50 deletions tests/runners/text-rendering/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
//! A Tool used for [Unicode Rendering Tests](https://github.com/unicode-org/text-rendering-tests)
//!
use cli::CommandLineArgumentParser;
use font::ttf::{Font, TTFParseError};
use std::{fs, io};

#[derive(Debug, Default, CommandLineArgumentParser)]
struct ArgumentParser {
#[argument(
may_be_omitted,
short_name = 'f',
long_name = "font",
description = "Specify the font to be used"
)]
use clap::Parser;
use font::ttf::{Font, TTFParseError};

#[derive(Debug, Default, Parser)]
#[command(version, about, long_about = None)]
struct Arguments {
/// Specify the font to be used
#[arg(short = 'f', long = "font")]
font_path: Option<String>,

#[argument(
may_be_omitted,
short_name = 'r',
long_name = "render",
description = "The characters to be rendered"
)]
/// The characters to be rendered
#[arg(short = 'r', long = "render")]
text: Option<String>,

#[argument(
may_be_omitted,
short_name = 't',
long_name = "testcase",
description = "The name of the current test case"
)]
/// The name of the current test case
#[arg(short = 't', long = "testcase")]
testcase: Option<String>,

#[argument(
flag,
short_name = 'v',
long_name = "version",
description = "Show package version"
)]
version: bool,
}

#[derive(Debug)]
Expand All @@ -49,25 +30,15 @@ enum Error {
}

fn main() -> Result<(), Error> {
let arguments = match ArgumentParser::parse() {
Ok(arguments) => arguments,
Err(_) => {
println!("{}", ArgumentParser::help());
return Ok(());
},
};
let args = Arguments::parse();

let font_bytes = fs::read(args.font_path.expect("No font path provided")).map_err(Error::IO)?;
let font = Font::new(&font_bytes).map_err(Error::Font)?;
let svg = font.render_as_svg(
&args.text.expect("No text provided"),
&args.testcase.expect("No testcase name provided"),
);
println!("{svg}");

if arguments.version {
println!("{}", env!("CARGO_PKG_VERSION"));
} else {
let font_bytes =
fs::read(arguments.font_path.expect("No font path provided")).map_err(Error::IO)?;
let font = Font::new(&font_bytes).map_err(Error::Font)?;
let svg = font.render_as_svg(
&arguments.text.expect("No text provided"),
&arguments.testcase.expect("No testcase name provided"),
);
println!("{svg}");
}
Ok(())
}

0 comments on commit b8a3152

Please sign in to comment.