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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// 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 defines [LinkDirective].

extern crate strict_yaml_rust;

use crate::filesystem::FileSystemDirective;
use crate::link::action::LinkAction;
use dotfiles_core::action::ActionParser;
use dotfiles_core::action::SKIP_IN_CI_SETTING;
use dotfiles_core::directive::Directive;
use dotfiles_core::directive::DirectiveData;
use dotfiles_core::directive::HasDirectiveData;
use dotfiles_core::error::DotfilesError;
use dotfiles_core::error::ErrorType;
use dotfiles_core::settings::initialize_settings_object;
use dotfiles_core::settings::Setting;
use dotfiles_core::settings::Settings;
use dotfiles_core::yaml_util::*;
use dotfiles_core_macros::Directive;
use filesystem::FakeFileSystem;
use filesystem::FileSystem;
use filesystem::OsFileSystem;
use filesystem::UnixFileSystem;
use std::marker::PhantomData;
use std::path::Path;
use strict_yaml_rust::StrictYaml;

/// Name of the link directive
pub const DIRECTIVE_NAME: &str = "link";
/// Path setting (path of the symlink)
pub const PATH_SETTING: &str = "path";
/// Target setting (path to the file the symlink points to)
pub const TARGET_SETTING: &str = "target";
/// Force setting, replaces any other file or directory
pub const FORCE_SETTING: &str = "force";
/// Relink setting, if true the action relinks an existing symlink
/// (applies if force is false)
pub const RELINK_SETTING: &str = "relink";
/// Create parent dirs if they don't exist
pub const CREATE_PARENT_DIRS_SETTING: &str = "create_parent_dirs";
/// Create the symlink even if the target file does not exist
pub const IGNORE_MISSING_TARGET_SETTING: &str = "ignore_missing_target";
/// Resolves the target if it is a symlink and uses the final target file as the target.
pub const RESOLVE_SYMLINK_TARGET_SETTING: &str = "resolve_symlink_target";

/// Initialize the defaults for the LinkDirective.
pub fn init_directive_data() -> DirectiveData {
  DirectiveData::from(
    DIRECTIVE_NAME.into(),
    initialize_settings_object(&[
      (FORCE_SETTING.to_owned(), Setting::Boolean(false)),
      (RELINK_SETTING.to_owned(), Setting::Boolean(false)),
      (
        CREATE_PARENT_DIRS_SETTING.to_owned(),
        Setting::Boolean(false),
      ),
      (
        IGNORE_MISSING_TARGET_SETTING.to_owned(),
        Setting::Boolean(false),
      ),
      (
        RESOLVE_SYMLINK_TARGET_SETTING.to_owned(),
        Setting::Boolean(false),
      ),
      (SKIP_IN_CI_SETTING.to_owned(), Setting::Boolean(false)),
    ]),
  )
}

/// A directive that can build [LinkAction]s to create directories
/// in the filesystem.
#[derive(Directive, Clone)]
pub struct LinkDirective<'a, F: FileSystem + UnixFileSystem + Default> {
  fs: F,
  data: DirectiveData,
  phantom: PhantomData<&'a F>,
}

/// [LinkDirective] that uses the native [OsFileSystem].
pub type NativeLinkDirective<'a> = LinkDirective<'a, OsFileSystem>;
/// [LinkDirective] that uses the native [FakeFileSystem] for testing.
pub type FakeLinkDirective<'a> = LinkDirective<'a, FakeFileSystem>;

impl<'a, F: FileSystem + UnixFileSystem + Default> Default for LinkDirective<'a, F> {
  fn default() -> Self {
    Self {
      fs: Default::default(),
      data: init_directive_data(),
      phantom: Default::default(),
    }
  }
}

impl<'a, F: FileSystem + UnixFileSystem + Default> FileSystemDirective<'a, F>
  for LinkDirective<'a, F>
{
  fn fs(&self) -> &F {
    &self.fs
  }

  fn mut_fs(&mut self) -> &mut F {
    &mut self.fs
  }
}

impl<'a, F: FileSystem + UnixFileSystem + Default> LinkDirective<'a, F>
where
  LinkDirective<'a, F>: HasDirectiveData<'a> + Directive<'a>,
{
  /// Returns the [FileSystem] instance being used.
  pub fn fs(&self) -> &F {
    &self.fs
  }

  fn parse_full_action(
    &'a self,
    context_settings: &Settings,
    yaml: &StrictYaml,
    current_dir: &Path,
  ) -> Result<LinkAction<'a, F>, DotfilesError> {
    let path = get_string_setting_from_yaml_or_context(
      PATH_SETTING,
      yaml,
      context_settings,
      self.data.defaults(),
    )?;
    let target = get_string_setting_from_yaml_or_context(
      TARGET_SETTING,
      yaml,
      context_settings,
      self.data.defaults(),
    )?;
    let action_settings: Result<Settings, DotfilesError> = self
      .directive_data()
      .defaults()
      .iter()
      .map(|(name, _)| {
        self
          .get_setting_from_yaml_hash_or_from_context(name, yaml, context_settings)
          .map(|setting| (name.to_owned(), setting))
      })
      .collect();

    LinkAction::<'a, F>::new(
      &self.fs,
      path,
      target,
      &action_settings?,
      self.data.defaults(),
      current_dir.to_owned(),
    )
  }

  /// Parse a shortened action with only link name to target name
  pub fn parse_shortened_action(
    &'a self,
    context_settings: &Settings,
    yaml: &StrictYaml,
    current_dir: &Path,
  ) -> Result<LinkAction<'a, F>, DotfilesError> {
    if let StrictYaml::Hash(hash) = yaml {
      match hash.len() {
        1 => {
          if let (StrictYaml::String(path), StrictYaml::String(target)) = hash.front().unwrap() {
            LinkAction::<'a, F>::new(
              &self.fs,
              path.clone(),
              target.clone(),
              context_settings,
              self.data.defaults(),
              current_dir.to_owned()
            )
          } else {
            Err(DotfilesError::from_wrong_yaml(
                        "StrictYaml passed to configure a short Link action is not a hash of string to string, cant parse".into(),
                        yaml.to_owned(), StrictYaml::Hash(Default::default())))
          }
        }

        x => Err(DotfilesError::from(
          format!(
            "StrictYaml passed to configure a short Link action is a hash with {x} values, must be just 1",),
          ErrorType::InconsistentConfigurationError,
        )),
      }
    } else {
      Err(DotfilesError::from_wrong_yaml(
        "StrictYaml passed to configure a Link action is not a Hash".into(),
        yaml.to_owned(),
        StrictYaml::Hash(Default::default()),
      ))
    }
  }
}

impl<'a, F: FileSystem + UnixFileSystem + Default> ActionParser<'a> for LinkDirective<'a, F> {
  type ActionType = LinkAction<'a, F>;

  fn parse_action(
    &'a self,
    settings: &Settings,
    yaml: &StrictYaml,
    current_directory: &Path,
  ) -> Result<LinkAction<'a, F>, DotfilesError> {
    self
      .parse_shortened_action(settings, yaml, current_directory)
      .or_else(|_| self.parse_full_action(settings, yaml, current_directory))
  }
}