conf/
introspection.rs

1//! Facilities for introspecting on Conf structures and the Conf process
2//!
3//! The main ways to introspect are:
4//! * [`Conf::program_options`](crate::Conf::program_options), which uses the [`ProgramOptionMeta`] trait
5//! * [`ConfBuilder::config_logger`](crate::ConfBuilder::config_logger), which uses the [`ConfigEvent`] trait
6use core::fmt::Display;
7
8/// Public interface to metadata about a program option
9#[allow(private_bounds)]
10pub trait ProgramOptionMeta: Sealed {
11    /// The id of the program option. This is generally its "rust path" in the config object.
12    fn id(&self) -> &dyn Display;
13    /// The description of the program option. This is generally its doc string, plus any prefixing.
14    /// This is also displayed in the help text for the program option.
15    fn description(&self) -> Option<&dyn Display>;
16    /// The short-form switch (-f, -h) of the option, if any.
17    fn short_form(&self) -> Option<char>;
18    /// The long-form switch (--file, --help) of the option, if any.
19    fn long_form(&self) -> Option<&dyn Display>;
20    /// True the option can be specified as a positional command line argument.
21    fn is_positional(&self) -> bool;
22    /// The environment variable associated to this option, if any.
23    fn env_form(&self) -> Option<&dyn Display>;
24    /// True if this option could be read from the serde document.
25    fn has_serde_source(&self) -> bool;
26    /// True if this option is required to appear in one of the input sources (i.e. it's an error if doesn't)
27    fn is_required(&self) -> bool;
28    /// The help string describing the default value, if any.
29    fn default_help_str(&self) -> Option<&dyn Display>;
30}
31
32/// Describes the source of the value when a program option is assigned a value
33#[non_exhaustive]
34pub enum ValueSource<'a> {
35    /// The value came from command-line arguments
36    #[non_exhaustive]
37    Args {},
38    /// The value came from an environment variable
39    #[non_exhaustive]
40    Env {
41        /// The env var that was read
42        var: &'a dyn Display,
43    },
44    /// The value came from a document
45    #[non_exhaustive]
46    Document {
47        /// The name of the document that was read
48        name: &'a dyn Display,
49    },
50    /// The value came from a default setting
51    #[non_exhaustive]
52    Default {},
53}
54
55/// A record of how a program option was assigned a value
56#[allow(private_bounds)]
57pub trait ConfigEvent: Sealed {
58    /// Get the program option
59    fn program_option(&self) -> &dyn ProgramOptionMeta;
60    /// Get the value source
61    fn value_source(&self) -> ValueSource<'_>;
62}
63
64/// A callback that receives ConfigEvents
65#[doc(hidden)]
66pub type ConfigLogger<'a> = &'a mut dyn FnMut(&dyn ConfigEvent);
67
68#[doc(hidden)]
69pub(crate) trait Sealed {}