dotfiles_actions/apt/
directive.rs

1// Copyright (c) 2021-2022 Miguel Barreto and others
2//
3// Permission is hereby granted, free of charge, to any person obtaining
4// a copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to
8// permit persons to whom the Software is furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be
12// included in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21//! This module defines [AptDirective].
22
23extern crate strict_yaml_rust;
24
25use crate::apt::action::AptAction;
26use dotfiles_core::action::ActionParser;
27use dotfiles_core::action::SKIP_IN_CI_SETTING;
28use dotfiles_core::directive::DirectiveData;
29use dotfiles_core::error::add_directive_error_prefix;
30use dotfiles_core::error::DotfilesError;
31use dotfiles_core::settings::initialize_settings_object;
32use dotfiles_core::settings::Setting;
33use dotfiles_core::settings::Settings;
34use dotfiles_core::yaml_util::*;
35use dotfiles_core_macros::Directive;
36
37use std::marker::PhantomData;
38use std::path::Path;
39use strict_yaml_rust::StrictYaml;
40/// Name of the APT directive
41pub const DIRECTIVE_NAME: &str = "apt";
42
43/// The string that identifies the list of packages to install
44pub const PACKAGE_SETTING: &str = "package";
45
46/// Initialize the defaults for the AptDirective.
47pub fn init_directive_data() -> DirectiveData {
48  DirectiveData::from(
49    DIRECTIVE_NAME.into(),
50    initialize_settings_object(&[(SKIP_IN_CI_SETTING.to_owned(), Setting::Boolean(false))]),
51  )
52}
53
54/// A directive that can build [AptAction]s to install packages
55#[derive(Directive, Clone)]
56pub struct AptDirective<'a> {
57  data: DirectiveData,
58  phantom_data: PhantomData<&'a DirectiveData>,
59}
60
61impl<'a> Default for AptDirective<'a> {
62  fn default() -> AptDirective<'a> {
63    AptDirective::<'a> {
64      data: init_directive_data(),
65      phantom_data: PhantomData,
66    }
67  }
68}
69
70impl<'a> ActionParser<'a> for AptDirective<'a> {
71  type ActionType = AptAction<'a>;
72
73  fn parse_action(
74    &'a self,
75    context_settings: &Settings,
76    yaml: &StrictYaml,
77    _: &Path,
78  ) -> Result<AptAction<'a>, DotfilesError> {
79    let skip_in_ci = get_boolean_setting_from_yaml_or_context(
80      SKIP_IN_CI_SETTING,
81      yaml,
82      context_settings,
83      self.data.defaults(),
84    )?;
85    let packages = get_optional_string_array_from_yaml_hash(PACKAGE_SETTING, yaml)?;
86    Ok(AptAction::new(skip_in_ci, packages))
87  }
88
89  /// Parse the list of actions from yaml, in this case it's only one action so
90  /// this function only wraps [AptDirective::parse_action]
91  fn parse_action_list(
92    &'a self,
93    context_settings: &Settings,
94    yaml: &StrictYaml,
95    current_dir: &Path,
96  ) -> Result<Vec<AptAction<'a>>, DotfilesError> {
97    Ok(vec![add_directive_error_prefix(
98      self,
99      self.parse_action(context_settings, yaml, current_dir),
100    )?])
101  }
102}