File: parser.rs

package info (click to toggle)
rust-nextest-filtering 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: makefile: 2
file content (71 lines) | stat: -rw-r--r-- 1,908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Standalone expression parser
//!
//! Useful for manually testing the parsing result

use camino::Utf8PathBuf;
use clap::Parser;
use guppy::graph::PackageGraph;
use nextest_filtering::errors::FiltersetParseErrors;

#[derive(Debug, Parser)]
struct Args {
    /// Path to a cargo metadata json file
    #[clap(short = 'g')]
    cargo_metadata: Option<Utf8PathBuf>,

    /// The expression to parse
    expr: String,
}

const EMPTY_GRAPH: &str = r#"{
        "packages": [],
        "workspace_members": [],
        "workspace_root": "",
        "target_directory": "",
        "version": 1
    }"#;

fn load_graph(path: Option<Utf8PathBuf>) -> PackageGraph {
    let json = match path {
        Some(path) => match std::fs::read_to_string(path) {
            Ok(json) => json,
            Err(err) => {
                eprintln!("Failed to read cargo-metadata file: {err}");
                std::process::exit(1);
            }
        },
        None => EMPTY_GRAPH.to_string(),
    };

    match PackageGraph::from_json(json) {
        Ok(graph) => graph,
        Err(err) => {
            eprintln!("Failed to parse cargo-metadata: {err}");
            std::process::exit(1);
        }
    }
}

fn main() {
    let args = Args::parse();

    let graph = load_graph(args.cargo_metadata);
    let cx = nextest_filtering::ParseContext::new(&graph);
    match nextest_filtering::Filterset::parse(
        args.expr,
        &cx,
        nextest_filtering::FiltersetKind::Test,
    ) {
        Ok(expr) => println!("{expr:?}"),
        Err(FiltersetParseErrors { input, errors, .. }) => {
            for error in errors {
                let report = miette::Report::new(error).with_source_code(input.clone());
                eprintln!("{report:?}");
            }
            std::process::exit(1);
        }
    }
}