Skip to main content

asimov_patterns/options/
filtering.rs

1// This is free and unencumbered software released into the public domain.
2
3//! Shared output filtering options.
4
5use alloc::string::String;
6use bon::Builder;
7
8#[cfg(feature = "clap")]
9const HELP_JEV: &str = r#"Filter output using a Jev noul: an assertion evaluated as true or false.
10
11Use an assertion such as "The name is Ukrainian", rather than a question.
12Jev filtering is applied before --jq."#;
13
14#[cfg(feature = "clap")]
15const HELP_JQ: &str = r#"Filter and/or transform JSON-LD output using a jq expression.
16
17For example, "select(.name)". When --jev is also specified, jq is applied after
18Jev filtering."#;
19
20/// Output filtering requests for host commands.
21///
22/// Flatten this alongside [`crate::FetcherOptions`] or [`crate::ListerOptions`]
23/// in a host's Clap arguments. Both fields default to `None`, leaving output
24/// unfiltered. The host applies Jev before jq and supplies the evaluators and any
25/// required credentials. This type stores expressions without evaluating them;
26/// `asimov-runner` does not perform this filtering.
27#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Builder)]
28#[builder(derive(Debug), on(String, into))]
29#[cfg_attr(feature = "clap", derive(clap::Args))]
30#[cfg_attr(feature = "clap", command(about = None, long_about = None))]
31pub struct FilteringOptions {
32    /// Filter output using a Jev noul (an assertion evaluated as true or false).
33    ///
34    /// The `NOUL` argument should be an assertion, such as
35    /// `The name is Ukrainian`, rather than a question. Applied by the host
36    /// before [`Self::jq`].
37    #[cfg_attr(
38        feature = "clap",
39        clap(
40            long,
41            value_name = "NOUL",
42            help = "Filter output using a Jev noul (an assertion evaluated as true or false)",
43            long_help = HELP_JEV
44        )
45    )]
46    pub jev: Option<String>,
47
48    /// Filter and/or transform JSON-LD output using a jq expression.
49    ///
50    /// For example, `select(.name)`. Applied by the host after [`Self::jev`].
51    #[cfg_attr(
52        feature = "clap",
53        clap(
54            long,
55            value_name = "EXPR",
56            help = "Filter and/or transform JSON-LD output using a jq expression",
57            long_help = HELP_JQ
58        )
59    )]
60    pub jq: Option<String>,
61}