Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 666a056a authored by Henri Chataing's avatar Henri Chataing
Browse files

pdl: Convert main from clap to argh

clap has incompatible versions on aosp-master (3.2),
tm-mainline-prod (2.2) and emu-master-dev (4.1) making
it impossible to provide a main compatible with all branches.

argh is much more stable.

(cherry picked from commit 567988a3)
Test: cargo run tests/canonical/le_test_file.pdl
Merged-In: I9ffb5119e2e1e90562411c782f5cd4ccd926b93c
Change-Id: I50d39c1d57fb6dcd68d05d6053b20c3a0f34c8a1
parent 2b139615
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ rust_defaults {
    name: "pdl_defaults",
    // LINT.IfChange
    rustlibs: [
        "libclap",
        "libargh",
        "libcodespan_reporting",
        "libheck",
        "libpest",
@@ -34,8 +34,8 @@ rust_binary_host {
    defaults: ["pdl_defaults"],
    srcs: ["src/main.rs"],
    visibility: [
        "//packages/modules/Bluetooth:__subpackages__",
        "//external/uwb/src",
        "//packages/modules/Bluetooth:__subpackages__",
    ],
}

@@ -156,8 +156,8 @@ genrule_defaults {
        ":rustfmt",
    ],
    defaults_visibility: [
        "//packages/modules/Bluetooth:__subpackages__",
        "//external/uwb/src",
        "//packages/modules/Bluetooth:__subpackages__",
    ],
}

+3 −3
Original line number Diff line number Diff line
@@ -12,12 +12,12 @@ default = ["serde"]
[dependencies]
codespan-reporting = "0.11.1"
heck = "0.4.0"
pest = "2.4.0"
pest_derive = "2.4.0"
pest = "2.5.5"
pest_derive = "2.5.5"
proc-macro2 = "1.0.46"
quote = "1.0.21"
serde_json = "1.0.86"
clap = "3.2.23"
argh = "0.1.7"
syn = "1.0.102"

[dependencies.serde]
+10 −34
Original line number Diff line number Diff line
//! PDL parser and analyzer.

use argh::FromArgs;
use codespan_reporting::term::{self, termcolor};

mod analyzer;
@@ -33,50 +34,25 @@ impl std::str::FromStr for OutputFormat {
    }
}

#[derive(Debug)]
#[derive(FromArgs, Debug)]
/// PDL analyzer and generator.
struct Opt {
    /// Print tool version and exit.
    #[argh(switch)]
    /// print tool version and exit.
    version: bool,

    /// Generate output in this format ("json", "rust", "rust_no_alloc", "rust_no_alloc_test"). The output
    #[argh(option, default = "OutputFormat::JSON")]
    /// generate output in this format ("json", "rust", "rust_no_alloc", "rust_no_alloc_test"). The output
    /// will be printed on stdout in both cases.
    output_format: OutputFormat,

    /// Input file.
    #[argh(positional)]
    /// input file.
    input_file: String,
}

impl Opt {
    fn parse() -> Opt {
        let app = clap::App::new("Packet Description Language parser")
            .version("1.0")
            .arg(
                clap::Arg::with_name("input-file")
                    .value_name("FILE")
                    .help("Input PDL file")
                    .required(true),
            )
            .arg(
                clap::Arg::with_name("output-format")
                    .value_name("FORMAT")
                    .long("output-format")
                    .help("Output file format")
                    .takes_value(true)
                    .default_value("json")
                    .possible_values(&["json", "rust", "rust_no_alloc", "rust_no_alloc_test"]),
            );
        let matches = app.get_matches();

        Opt {
            version: matches.is_present("version"),
            input_file: matches.value_of("input-file").unwrap().into(),
            output_format: matches.value_of("output-format").unwrap().parse().unwrap(),
        }
    }
}

fn main() -> Result<(), String> {
    let opt = Opt::parse();
    let opt: Opt = argh::from_env();

    if opt.version {
        println!("Packet Description Language parser version 1.0");