cfg_expr::expr

Struct Expression

Source
pub struct Expression { /* private fields */ }
Expand description

A parsed cfg() expression that can evaluated

Implementations§

Source§

impl Expression

Source

pub fn parse(original: &str) -> Result<Self, ParseError>

Given a cfg() expression (the cfg( and ) are optional), attempts to parse it into a form where it can be evaluated

assert!(cfg_expr::Expression::parse(r#"cfg(all(unix, target_arch = "x86_64"))"#).is_ok());
Source§

impl Expression

Source

pub fn predicates(&self) -> impl Iterator<Item = Predicate<'_>>

An iterator over each predicate in the expression

Source

pub fn eval<EP, T>(&self, eval_predicate: EP) -> T
where EP: FnMut(&Predicate<'_>) -> T, T: Logic + Debug,

Evaluates the expression, using the provided closure to determine the value of each predicate, which are then combined into a final result depending on the functions not(), all(), or any() in the expression.

eval_predicate typically returns bool, but may return any type that implements the Logic trait.

§Examples
use cfg_expr::{targets::*, Expression, Predicate};

let linux_musl = get_builtin_target_by_triple("x86_64-unknown-linux-musl").unwrap();

let expr = Expression::parse(r#"all(not(windows), target_env = "musl", any(target_arch = "x86", target_arch = "x86_64"))"#).unwrap();

assert!(expr.eval(|pred| {
    match pred {
        Predicate::Target(tp) => tp.matches(linux_musl),
        _ => false,
    }
}));

Returning Option<bool>, where None indicates the result is unknown:

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

let expr = Expression::parse(r#"any(target_feature = "sse2", target_env = "musl")"#).unwrap();

let linux_gnu = get_builtin_target_by_triple("x86_64-unknown-linux-gnu").unwrap();
let linux_musl = get_builtin_target_by_triple("x86_64-unknown-linux-musl").unwrap();

fn eval(expr: &Expression, target: &TargetInfo) -> Option<bool> {
    expr.eval(|pred| {
        match pred {
            Predicate::Target(tp) => Some(tp.matches(target)),
            Predicate::TargetFeature(_) => None,
            _ => panic!("unexpected predicate"),
        }
    })
}

// Whether the target feature is present is unknown, so the whole expression evaluates to
// None (unknown).
assert_eq!(eval(&expr, linux_gnu), None);

// Whether the target feature is present is irrelevant for musl, since the any() always
// evaluates to true.
assert_eq!(eval(&expr, linux_musl), Some(true));
Source

pub fn original(&self) -> &str

The original string which has been parsed to produce this Expression.

use cfg_expr::Expression;

assert_eq!(
    Expression::parse("any()").unwrap().original(),
    "any()"
);

Trait Implementations§

Source§

impl Clone for Expression

Source§

fn clone(&self) -> Expression

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expression

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Expression

PartialEq will do a syntactical comparison, so will just check if both expressions have been parsed from the same string, not if they are semantically equivalent.

use cfg_expr::Expression;

assert_eq!(
    Expression::parse("any()").unwrap(),
    Expression::parse("any()").unwrap()
);
assert_ne!(
    Expression::parse("any()").unwrap(),
    Expression::parse("unix").unwrap()
);
Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.