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
72
73
74
75
76
77
78
use crate::errors::TebError;

/// Options for the code generator.
///
/// This struct is a builder for the code generator options.
///
/// # Examples
/// ```no_run
/// # use tighterror_build::{CodegenOptions, errors::TebError};
/// # pub fn foo() -> Result<(), TebError> {
/// CodegenOptions::new()
///     .spec("tighterror.yaml".to_owned())
///     .dst("src/errors.rs".to_owned())
///     .codegen()?;
/// # Ok(())
/// # }
/// # foo().unwrap();
/// ```
#[derive(Debug, Clone, Default)]
pub struct CodegenOptions {
    pub(crate) spec: Option<String>,
    pub(crate) dst: Option<String>,
}

impl CodegenOptions {
    /// Creates a new options object with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the specification file path.
    ///
    /// If not specified the path [DEFAULT_SPEC_PATH] is used.
    ///
    /// # Examples
    /// ```rust
    /// # use tighterror_build::CodegenOptions;
    /// CodegenOptions::new().spec(None);
    /// CodegenOptions::new().spec("tighterror.yaml".to_owned());
    /// CodegenOptions::new().spec(Some("myerrors.toml".into()));
    /// ```
    ///
    /// [DEFAULT_SPEC_PATH]: crate::DEFAULT_SPEC_PATH
    pub fn spec(&mut self, spec: impl Into<Option<String>>) -> &mut Self {
        self.spec = spec.into();
        self
    }

    /// Sets the destination file path.
    ///
    /// If the value is `"-"`, or destination file path is not set at all, the
    /// output is written to `stdout`.
    ///
    /// A value specified here overrides the one present in the specification
    /// file.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use tighterror_build::CodegenOptions;
    /// CodegenOptions::new().dst(None);
    /// CodegenOptions::new().dst("src/errors.rs".to_owned());
    /// CodegenOptions::new().dst(Some("myerrors.rs".into()));
    /// ```
    pub fn dst(&mut self, dst: impl Into<Option<String>>) -> &mut Self {
        self.dst = dst.into();
        self
    }

    /// Invokes the code generator [main function] using these options.
    ///
    /// See the struct documentation for a full example.
    ///
    /// [main function]: crate::codegen
    pub fn codegen(&self) -> Result<(), TebError> {
        super::codegen(self)
    }
}