#cfg #rustc #cargo #expression-evaluator

cfg-expr

解析和评估 Rust cfg() 表达式的工具

36 个版本

0.16.0 2024 年 7 月 29 日
0.15.7 2024 年 2 月 9 日
0.15.5 2023 年 9 月 8 日
0.15.4 2023 年 7 月 28 日
0.2.1 2020 年 3 月 30 日

#26解析器实现

Download history 248388/week @ 2024-05-03 256025/week @ 2024-05-10 241346/week @ 2024-05-17 234136/week @ 2024-05-24 234882/week @ 2024-05-31 221953/week @ 2024-06-07 236666/week @ 2024-06-14 239342/week @ 2024-06-21 225440/week @ 2024-06-28 229170/week @ 2024-07-05 246329/week @ 2024-07-12 248399/week @ 2024-07-19 246869/week @ 2024-07-26 242423/week @ 2024-08-02 268983/week @ 2024-08-09 239682/week @ 2024-08-16

1,042,398 每月下载量
用于 1,740 个 crate (直接使用 11 个)

MIT/Apache

190KB
4.5K SLoC

⚙️ cfg-expr

解析和评估 Rust cfg() 表达式的工具。从 1.80.0 开始支持内置目标。

Build Status Crates.io Docs Minimum Stable Rust Version Rust Targets Contributor Covenant Embark

替代方案

用法

cfg-expr 是一个 crate,可以用来解析和评估 Rust cfg() 表达式,包括作为 Rust 代码本身中的声明,以及在 cargo 清单的 [target.'cfg()'.dependencies] 部分。

它包含了一个已知于 rustc 的所有内置目标列表(截至 1.80.0),可用于确定特定的 cfg 表达式是否可满足。

use cfg_expr::{targets::get_builtin_target_by_triple, Expression, Predicate};

let specific = Expression::parse(
    r#"all(
        target_os = "windows",
        target_arch = "x86",
        windows,
        target_env = "msvc",
        target_feature = "fxsr",
        target_feature = "sse",
        target_feature = "sse2",
        target_pointer_width = "32",
        target_endian = "little",
        not(target_vendor = "uwp"),
        feature = "cool_thing",
    )"#,
).unwrap();

// cfg_expr includes a list of every builtin target in rustc
let x86_win = get_builtin_target_by_triple("i686-pc-windows-msvc").unwrap();
let x86_pentium_win = get_builtin_target_by_triple("i586-pc-windows-msvc").unwrap();
let uwp_win = get_builtin_target_by_triple("i686-uwp-windows-msvc").unwrap();
let mac = get_builtin_target_by_triple("x86_64-apple-darwin").unwrap();

let avail_target_feats = ["fxsr", "sse", "sse2"];

// This will satisfy all requirements
assert!(specific.eval(|pred| {
    match pred {
        Predicate::Target(tp) => tp.matches(x86_win),
        Predicate::TargetFeature(feat) => avail_target_feats.contains(feat),
        Predicate::Feature(feat) => *feat == "cool_thing",
        _ => false,
    }
}));

// This won't, it doesn't have the cool_thing feature!
assert!(!specific.eval(|pred| {
    match pred {
        Predicate::Target(tp) => tp.matches(x86_pentium_win),
        Predicate::TargetFeature(feat) => avail_target_feats.contains(feat),
        _ => false,
    }
}));

// This will *not* satisfy the vendor predicate
assert!(!specific.eval(|pred| {
    match pred {
        Predicate::Target(tp) => tp.matches(uwp_win),
        Predicate::TargetFeature(feat) => avail_target_feats.contains(feat),
        _ => false,
    }
}));

// This will *not* satisfy the vendor, os, or env predicates
assert!(!specific.eval(|pred| {
    match pred {
        Predicate::Target(tp) => tp.matches(mac),
        Predicate::TargetFeature(feat) => avail_target_feats.contains(feat),
        _ => false,
    }
}));

贡献

我们欢迎社区为此项目做出贡献。

请阅读我们的 贡献指南 以了解如何开始。

许可证

根据以下之一获得许可

由您选择。

贡献

除非您明确声明,否则根据 Apache-2.0 许可证定义的,任何有意提交以包含在作品中的贡献,均应如上所述双许可,不附加任何额外条款或条件。

依赖项

~98KB