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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2021-2022 Miguel Barreto and others
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//! This module contains the base trait for all [Directive] and all
//! necessary conveniences to allow for user-configuration of directive
//! defaults.

extern crate strict_yaml_rust;

use crate::{
  error::{DotfilesError, ErrorType},
  settings::{parse_setting, Settings},
  yaml_util::{fold_hash_until_first_err, get_setting_from_context},
  Setting,
};
use getset::Getters;
use strict_yaml_rust::StrictYaml;

/// A struct that contains the default settings for a Directive and the
/// name it takes in configuration sources. The name must be unique.
///
/// These default settings can be configured by the user as well.
#[derive(Getters, Debug, Clone)]
pub struct DirectiveData {
  /// Unique name of this directive.
  ///
  /// This name will be used in configuration sources to instantiate actions
  /// of this directive
  #[getset(get = "pub")]
  name: String,
  /// Default settings for this directive.
  ///
  /// Any setting that is not in the defaults for a directive but is part of
  /// the corresponding Action struct is considered to be mandatory.
  ///
  /// Since all configurable settings have a default, this can also be used to infer the data
  /// types.
  #[getset(get = "pub")]
  defaults: Settings,
}
impl DirectiveData {
  /// Constructs a new directive from a name and a set of default settings.
  pub fn from(name: String, defaults: Settings) -> DirectiveData {
    DirectiveData { name, defaults }
  }

  // Parses an individual setting named `name`'s value from a yaml containing the value, according
  // to the type set in
  /// `DirectiveData.setting_types`.
  pub fn parse_setting_value(
    &self,
    name: &str,
    yaml: &StrictYaml,
  ) -> Result<Setting, DotfilesError> {
    if let Some(setting_type) = self.defaults().get(name) {
      parse_setting(setting_type, yaml)
    } else {
      Err(DotfilesError::from(
        format!(
          "Directive `{}` could not parse settings, unknown setting: {}",
          self.name(),
          name,
        ),
        ErrorType::InconsistentConfigurationError,
      ))
    }
  }

  /// Parses all settings for this directive from StrictYaml, checking the types correspond to
  /// what's stored in `DirectiveData.setting_types`
  pub fn parse_context_defaults(
    &self,
    yaml_settings: &StrictYaml,
  ) -> Result<Settings, DotfilesError> {
    fold_hash_until_first_err(
      yaml_settings,
      Ok(Settings::new()),
      |name, value_yaml| {
        self
          .parse_setting_value(&name, value_yaml)
          .map(|value| (name, value))
      },
      |mut settings, (name, val)| {
        settings.try_insert(name.clone(), val).map_err(|_| {
          DotfilesError::from(
            format!(
              "Directive {} configuration contains duplicated setting {}",
              self.name(),
              name
            ),
            ErrorType::InconsistentConfigurationError,
          )
        })?;
        Ok(settings)
      },
    )
  }
}

/// A trait for all directives, it is shared between [crate::action::ActionParser] and [Directive]
pub trait HasDirectiveData<'a> {
  /// Returns the directive data for this object
  fn directive_data(&'a self) -> &'a DirectiveData;

  /// Returns the name of the directive.
  fn name(&'a self) -> &'a str {
    self.directive_data().name()
  }

  /// Returns the default settings as configured.
  fn defaults(&'a self) -> &'a Settings {
    self.directive_data().defaults()
  }
}

/// A parser for action steps, each directive represents a type of Action.
pub trait Directive<'a>: HasDirectiveData<'a> {
  /// Parse a particular setting with its correct type from yaml, fall back to context settings or
  /// directive defaults if not found in yaml. Returns error if there is any kind of parsing or
  /// typing error
  fn get_setting_from_yaml_hash_or_from_context(
    &'a self,
    name: &str,
    yaml: &StrictYaml,
    context_settings: &Settings,
  ) -> Result<Setting, DotfilesError> {
    self
      .get_setting_from_yaml_hash(name, yaml)
      .or_else(|_| get_setting_from_context(name, context_settings, self.defaults()))
  }

  /// Parses an individual setting named `name` from a yaml hash using the type stored in
  /// `DirectiveData.setting_types`.
  fn get_setting_from_yaml_hash(
    &'a self,
    name: &str,
    yaml: &StrictYaml,
  ) -> Result<Setting, DotfilesError> {
    if let Some(setting_type) = self.directive_data().defaults().get(name) {
      crate::yaml_util::get_setting_from_yaml_hash(name, setting_type, yaml)
    } else {
      Err(DotfilesError::from(
        format!(
          "Directive `{}` could not parse settings, unknown setting: {}",
          self.directive_data().name(),
          name,
        ),
        ErrorType::InconsistentConfigurationError,
      ))
    }
  }
}