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#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29#[builder(derive(Debug), on(String, into))]
30#[cfg_attr(feature = "clap", derive(clap::Args))]
31#[cfg_attr(feature = "clap", command(about = None, long_about = None))]
32pub struct FilteringOptions {
33    /// Filter output using a Jev noul (an assertion evaluated as true or false).
34    ///
35    /// The `NOUL` argument should be an assertion, such as
36    /// `The name is Ukrainian`, rather than a question. Applied by the host
37    /// before [`Self::jq`].
38    #[cfg_attr(
39        feature = "clap",
40        clap(
41            long,
42            value_name = "NOUL",
43            help = "Filter output using a Jev noul (an assertion evaluated as true or false)",
44            long_help = HELP_JEV
45        )
46    )]
47    #[cfg_attr(
48        feature = "serde",
49        serde(default, skip_serializing_if = "Option::is_none")
50    )]
51    pub jev: Option<String>,
52
53    /// Filter and/or transform JSON-LD output using a jq expression.
54    ///
55    /// For example, `select(.name)`. Applied by the host after [`Self::jev`].
56    #[cfg_attr(
57        feature = "clap",
58        clap(
59            long,
60            value_name = "EXPR",
61            help = "Filter and/or transform JSON-LD output using a jq expression",
62            long_help = HELP_JQ
63        )
64    )]
65    #[cfg_attr(
66        feature = "serde",
67        serde(default, skip_serializing_if = "Option::is_none")
68    )]
69    pub jq: Option<String>,
70}