Add basic error handling for invalid paths in proc_macro attribute

finchie
Jul 3, 2024, 7:01 AM
V5S5K33ALIEG5ZABUSAPO4ULHEBFDB2PLTW27A4BFS342SJG7URQC

Dependencies

  • [2] NO3PDO7P Refactor `fluent_embed` to support structs
  • [3] 56F2YE6H Use `prettyplease` to format macro output
  • [4] VZYZRAO4 Move `output-macros` crate into workspace
  • [5] 5FIVUZYF Unify `fluent_embed` macro API as `localize()`
  • [6] VNSHGQYN Support using glob paths in `localize` macro
  • [7] UKFEFT6L Create basic `Output` proc-macro
  • [8] O77KA6C4 Create `fluent_embed` crate
  • [9] WBI5HFOB Add simple wrapper for `libc::settext()` to query system locale
  • [10] SHNZZSZG Create `cli_macros` shim crate
  • [11] 5TEX4MNU Split `fluent_embed` into `group` and `parse` modules
  • [12] D652S2N3 Rename `parse` module to `parse_fluent`
  • [13] XGNME3WR Move `Group::derive_enum` to new `crate::parse_macro` module
  • [14] UOMQT7LT Add support for cardinal CLDR plural selectors

Change contents

  • replacement in fluent_embed/src/lib.rs at line 13
    [3.1387][2.4390:4453]()
    pub fn attribute_groups(path_literal: &syn::LitStr) -> Group {
    [3.1387]
    [2.4453]
    pub fn attribute_groups(path_literal: &syn::LitStr) -> Result<Group, wax::GlobError> {
  • replacement in fluent_embed/src/lib.rs at line 20
    [2.4658][2.4658:4715]()
    let glob = wax::Glob::new(&attribute_glob).unwrap();
    [2.4658]
    [2.4715]
    let glob = wax::Glob::new(&attribute_glob)?;
  • replacement in fluent_embed/src/lib.rs at line 23
    [2.4837][2.4837:4883]()
    let entry = potential_entry.unwrap();
    [2.4837]
    [2.4883]
    let entry = potential_entry?;
  • replacement in fluent_embed/src/lib.rs at line 36
    [2.5428][2.5428:5472]()
    Group::new(locale!("en-US"), resources)
    [2.5428]
    [2.5472]
    Ok(Group::new(locale!("en-US"), resources))
  • replacement in fluent_embed/src/lib.rs at line 39
    [2.5475][2.5475:5596]()
    pub fn localize(path: &syn::LitStr, derive_input: &DeriveInput) -> TokenStream {
    let group = attribute_groups(path);
    [2.5475]
    [2.5596]
    pub fn localize(
    path: &syn::LitStr,
    derive_input: &DeriveInput,
    ) -> Result<TokenStream, wax::GlobError> {
    let group = attribute_groups(path)?;
  • replacement in fluent_embed/src/lib.rs at line 52
    [2.5915][2.5915:5928]()
    quote! {
    [2.5915]
    [2.5928]
    Ok(quote! {
  • replacement in fluent_embed/src/lib.rs at line 59
    [2.6110][2.6110:6116]()
    }
    [2.6110]
    [2.6116]
    })
  • edit in cli_macros/src/lib.rs at line 4
    [3.160]
    [3.160]
    use proc_macro_error::{emit_error, proc_macro_error};
  • replacement in cli_macros/src/lib.rs at line 6
    [3.178][2.7171:7214]()
    use syn::{parse_macro_input, DeriveInput};
    [3.178]
    [3.206]
    use syn::parse_macro_input;
    use wax::GlobError;
  • edit in cli_macros/src/lib.rs at line 10
    [3.231]
    [3.231]
    #[proc_macro_error]
  • replacement in cli_macros/src/lib.rs at line 13
    [2.7285][3.307:378](),[3.307][3.307:378](),[3.378][2.7286:7350]()
    let parsed_attribute: syn::LitStr = parse_macro_input!(attribute);
    let parsed_input = parse_macro_input!(item as DeriveInput);
    [2.7285]
    [3.441]
    let derive_attribute: syn::LitStr = parse_macro_input!(attribute);
    let derive_input = parse_macro_input!(item);
  • replacement in cli_macros/src/lib.rs at line 16
    [3.442][2.7351:7434]()
    let implementation = fluent_embed::localize(&parsed_attribute, &parsed_input);
    [3.442]
    [3.600]
    let implementation = match fluent_embed::localize(&derive_attribute, &derive_input) {
    Ok(implementation) => implementation,
    Err(glob_error) => {
    // Emit the relevant error messages
    match glob_error {
    GlobError::Build(_build_error) => todo!(),
    GlobError::Walk(walk_error) => {
    // Generate help text
    let help = if let Some(path) = walk_error.path() {
    let path_name = path.to_str().unwrap();
    // Might hit an error if file exists but insufficient permissions
    match path.try_exists() {
    Ok(true) => {
    format!("the path `{path_name}` exists, but unable to access it")
    }
    _ => format!("the path `{path_name}` doesn't seem to exist"),
    }
    } else {
    String::from("no associated path")
    };
    emit_error! { derive_attribute, "error at depth {} while walking path", walk_error.depth();
    help = help;
    }
    }
    };
    // Generate a minimal `localize()` implementation so the error is self-contained
    let ident = &derive_input.ident;
    quote! {
    impl #ident {
    fn localize(&self) -> String {
    unimplemented!("Encountered error in proc-macro")
    }
    }
    }
    }
    };
  • edit in cli_macros/Cargo.toml at line 14
    [3.888]
    [2.7484]
    proc-macro-error = "1.0.4"
  • edit in cli_macros/Cargo.toml at line 18
    [3.954]
    [3.971]
    wax = "0.6.0"
  • edit in Cargo.lock at line 34
    [3.18]
    [2.7525]
    "proc-macro-error",
  • replacement in Cargo.lock at line 37
    [3.1218][3.1218:1226]()
    "syn",
    [3.1218]
    [3.1244]
    "syn 2.0.48",
    "wax",
  • replacement in Cargo.lock at line 69
    [3.1465][3.1465:1473]()
    "syn",
    [3.1465]
    [3.1473]
    "syn 2.0.48",
  • replacement in Cargo.lock at line 118
    [3.1978][3.1978:1986]()
    "syn",
    [3.1978]
    [3.1952]
    "syn 2.0.48",
  • replacement in Cargo.lock at line 230
    [3.3660][3.3660:3668]()
    "syn",
    [3.3660]
    [3.1961]
    "syn 2.0.48",
  • replacement in Cargo.lock at line 349
    [3.4478][3.4478:4486]()
    "syn",
    [3.4478]
    [3.4486]
    "syn 2.0.48",
  • replacement in Cargo.lock at line 368
    [3.518][3.518:526]()
    "syn",
    [3.518]
    [3.526]
    "syn 2.0.48",
    ]
    [[package]]
    name = "proc-macro-error"
    version = "1.0.4"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
    dependencies = [
    "proc-macro-error-attr",
    "proc-macro2",
    "quote",
    "syn 1.0.109",
    "version_check",
    ]
    [[package]]
    name = "proc-macro-error-attr"
    version = "1.0.4"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
    dependencies = [
    "proc-macro2",
    "quote",
    "version_check",
  • replacement in Cargo.lock at line 468
    [3.4330][3.4330:4338]()
    "syn",
    [3.4330]
    [3.4338]
    "syn 2.0.48",
  • edit in Cargo.lock at line 485
    [3.4972]
    [3.4972]
    version = "1.0.109"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
    dependencies = [
    "proc-macro2",
    "unicode-ident",
    ]
    [[package]]
    name = "syn"
  • replacement in Cargo.lock at line 512
    [3.4991][3.4991:4999]()
    "syn",
    [3.4991]
    [3.2222]
    "syn 2.0.48",
  • replacement in Cargo.lock at line 523
    [3.2256][3.2256:2275]()
    version = "1.0.56"
    [3.2256]
    [3.2275]
    version = "1.0.61"
  • replacement in Cargo.lock at line 525
    [3.2340][3.2340:2418]()
    checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad"
    [3.2340]
    [3.2418]
    checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
  • replacement in Cargo.lock at line 532
    [3.2479][3.2479:2498]()
    version = "1.0.56"
    [3.2479]
    [3.2498]
    version = "1.0.61"
  • replacement in Cargo.lock at line 534
    [3.2563][3.2563:2641]()
    checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471"
    [3.2563]
    [3.2641]
    checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
  • replacement in Cargo.lock at line 538
    [3.2684][3.2684:2692]()
    "syn",
    [3.2684]
    [3.2692]
    "syn 2.0.48",
  • edit in Cargo.lock at line 568
    [3.4184]
    [3.4184]
    [[package]]
    name = "version_check"
    version = "0.9.4"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
  • replacement in Cargo.lock at line 657
    [3.5938][3.5938:5946]()
    "syn",
    [3.5938]
    [3.5946]
    "syn 2.0.48",
  • replacement in Cargo.lock at line 678
    [3.6438][3.6438:6446]()
    "syn",
    [3.6438]
    [3.6446]
    "syn 2.0.48",
  • replacement in Cargo.lock at line 701
    [3.6959][3.6959:6967]()
    "syn",
    [3.6959]
    [3.6967]
    "syn 2.0.48",