parse_cfg

Crates.ioparse_cfg
lib.rsparse_cfg
version4.1.1
sourcesrc
created_at2018-05-20 12:32:12.045389
updated_at2023-08-18 15:06:27.393325
descriptionParse and evaluate Rust's `cfg(any(condition))` attribute syntax and target triples
homepagehttps://lib.rs/crates/parse_cfg
repositoryhttps://gitlab.com/kornelski/parse_cfg
max_upload_size
id66314
size32,277
Kornel (kornelski)

documentation

https://docs.rs/parse_cfg

README

cfg() expression parser

Cfg is an AST for just cfg() expressions. Target allows target triples or cfg(), so it's suitable for parsing targets Cargo allows in target.🈁️.dependencies.

use parse_cfg::*;
fn main() -> Result<(), ParseError> {

let cfg: Cfg = r#"cfg(any(unix, feature = "extra"))"#.parse()?;
assert_eq!(Cfg::Any(vec![
    Cfg::Is("unix".into()),
    Cfg::Equal("feature".into(), "extra".into()),
]), cfg);

let is_set = cfg.eval(|key, comparison| if key == "feature" && comparison == "extra" { Some(comparison) } else { None });
assert!(is_set);

let target = "powerpc64le-unknown-linux-gnu".parse()?;
assert_eq!(Target::Triple {
    arch: "powerpc64le".into(),
    vendor: "unknown".into(),
    os: "linux".into(),
    env: Some("gnu".into()),
}, target);

/// `Cfg` and `Target` types take an optional generic argument for the string type,
/// so you can parse slices without allocating `String`s, or parse into `Cow<str>`.
let target = Target::<&str>::parse_generic("powerpc64le-unknown-linux-gnu")?;
assert_eq!(Target::Triple {
    arch: "powerpc64le",
    vendor: "unknown",
    os: "linux",
    env: Some("gnu"),
}, target);

Ok(()) }

It's safe to parse untrusted input. The depth of expressions is limited to 255 levels.

Target triples used by Rust don't follow its documented syntax, so sometimes os/vendor/env will be shifted.

Commit count: 14

cargo fmt