#features #programs #program #supported

bin+lib feature-check

Query a program for supported features

4 stable releases

2.1.0 Feb 10, 2024
2.0.0 Oct 11, 2022
1.0.1 Dec 17, 2021
1.0.0 Jun 24, 2021

#407 in Parser implementations

25 downloads per month

BSD-2-Clause

54KB
1K SLoC

feature-check - query a program for supported features

The feature-check library helps programs obtain the list of supported features from a program via various methods (e.g. running it with the --features command-line option) and check for the presence and, possibly, versions of specific features.

List a program's features

Use the default List mode of operation in the configuration object, specify the program name and, possibly, the command-line option to use and the string that the feature list line should start with:

use std::error::Error;

use feature_check::defs::{Config, Obtained};
use feature_check::obtain as fobtain;

fn main() -> Result<(), Box<dyn Error>> {
    let config = Config {
        program: "curl".to_string(),
        option_name: "--version".to_string(),
        ..Config::default()
    };
    match fobtain::obtain_features(&config)? {
        Obtained::Failed(err) => eprintln!("Could not obtain the features: {}", err),
        Obtained::NotSupported => eprintln!("Querying features not supported"),
        Obtained::Features(res) => println!("{} features", res.len()),
    };
    Ok(())
}

Compare a single feature against a version number

Once the list of features has been queried, use an expression previously obtained from the expr::parse_simple() function:

use std::error;

use feature_check::defs::{Config, Obtained};
use feature_check::expr::{self as fexpr, CalcResult};
use feature_check::obtain as fobtain;

fn main() -> Result<(), Box<dyn error::Error>> {
    let query = "feature-check >= 0.2";
    let expr = fexpr::parse_simple(&query)?;
    let config = Config {
        program: "feature-check".to_string(),
        ..Config::default()
    };
    match fobtain::obtain_features(&config)? {
        Obtained::Failed(err) => eprintln!(
            "Could not obtain the features for {}: {}",
            config.program, err
        ),
        Obtained::NotSupported => eprintln!("Querying features not supported"),
        Obtained::Features(res) => {
            println!("{} features", res.len());
            match res.contains_key("feature-check") {
                false => eprintln!("No 'feature-check' in the features list"),
                true => match expr.get_value(&res)? {
                    CalcResult::Bool(value) => println!("{}: {}", query, value),
                    other => eprintln!("Unexpected result: {:?}", other),
                },
            };
        }
    };
    Ok(())
}

For the crate's change history, see the changelog file in the source distribution.

Dependencies

~2.5–3.5MB
~69K SLoC