Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding global variable to enable symlinks by default if type not specified #173

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 243 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ pub type Variables = toml::value::Table;
#[cfg(feature = "scripting")]
pub type Helpers = BTreeMap<String, PathBuf>;

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum DefaultTargetType {
Symbolic,
Template,
#[default]
Automatic,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Settings {
#[serde(default)]
default_target_type: DefaultTargetType,
}

#[derive(Debug, Clone)]
pub struct Configuration {
pub files: Files,
Expand All @@ -90,6 +105,8 @@ pub struct Configuration {
/// turned into a list of all the files inside the structure that
/// are readable.
pub recurse: bool,

pub settings: Settings,
}

#[derive(Debug, Deserialize, Serialize, Default)]
Expand All @@ -110,6 +127,8 @@ struct GlobalConfig {
helpers: Helpers,
#[serde(flatten)]
packages: BTreeMap<String, Package>,
#[serde(default)]
settings: Settings,
}

type IncludedConfig = BTreeMap<String, Package>;
Expand Down Expand Up @@ -211,6 +230,7 @@ pub fn save_dummy_config(
#[cfg(feature = "scripting")]
helpers: Helpers::new(),
packages,
settings: Settings::default(),
};
debug!("Saving global config...");
// Assume default args so all parents are the same
Expand Down Expand Up @@ -330,6 +350,7 @@ fn merge_configuration_files(
variables: Variables::default(),
packages: packages_map,
recurse: true,
settings: Settings::default(),
};

// Merge all the packages
Expand Down Expand Up @@ -373,6 +394,12 @@ fn merge_configuration_files(
output.files = first_package.files;
output.variables = first_package.variables;

if let DefaultTargetType::Symbolic = global.settings.default_target_type {
output.files = transform_file_targets(output.files, FileTargetTransform::Symbolic);
} else if let DefaultTargetType::Template = global.settings.default_target_type {
output.files = transform_file_targets(output.files, FileTargetTransform::Template);
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO you overthought this a little with the FileTargetTransform. How about

for value in files.values_mut() {
    *value = match *value {
        FileTarget::Automatic(target) if global.settings.default_target_type == DefaultTargetType::Symbolic => {
            FileTarget::Symbolic(SymbolicTarget::from(target))
        },
        FileTarget::Automatic(target) if global.settings.default_target_type == DefaultTargetType::Template => {
            FileTarget::ComplexTemplate(TemplateTarget::from(target))
        },
        other => other,
    }
}

Or maybe

for value in files.values_mut() {
    if let FileTarget::Automatic(target) = *value {
        match global.settings.default_target_type {
            DefaultTargetType::Symbolic => {
                *value = FileTarget::Symbolic(SymbolicTarget::from(target));
            },
            DefaultTargetType::Template => {
                *value = FileTarget::ComplexTemplate(TemplateTarget::from(target));
            },
            _ => {},
        }
    }
}

Both of those might run into borrowing issues so you might have to use .into_iter().map 🙃
The block could be extracted into a function that takes default_target_type if you think it would be cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I definitely agree re overthinking it. I like either of those options much better and will be working on implementing one. thanks.

// Add local.toml's patches
output.files.extend(local.files);
recursive_extend_map(&mut output.variables, local.variables);
Expand Down Expand Up @@ -562,6 +589,34 @@ impl UnixUser {
}
}

enum FileTargetTransform {
Symbolic,
Template,
}

fn transform_file_targets(
files: Files,
transform_target: FileTargetTransform,
) -> Files {
files
.into_iter()
.map(|(name, target)| -> _ {
let t = match target {
FileTarget::Automatic(target) => match transform_target {
FileTargetTransform::Symbolic => {
FileTarget::Symbolic(SymbolicTarget::from(target))
},
FileTargetTransform::Template => {
FileTarget::ComplexTemplate(TemplateTarget::from(target))
}
},
_ => target,
};
(name, t)
})
.collect::<Files>()
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -632,4 +687,192 @@ mod test {
)
.unwrap_err();
}

#[test]
fn settting_default_target_type_symbolic() {
let global: GlobalConfig = toml::from_str(
r#"
[settings]
default_target_type = "symbolic"

[cat]
depends = []

[cat.files]
cat = '~/.QuarticCat'

[derby]
depends = []

[derby.files]
derby = { target = '~/.DerbyLantern', type = 'template' }
"#,
)
.unwrap();

let local: LocalConfig = toml::from_str(
r#"
packages = ['cat', 'derby']
"#,
)
.unwrap();

let merged_config =
merge_configuration_files(global, local, None);

let config = merged_config.unwrap();

let cat = config
.files
.get(&PathBuf::from("cat"))
.unwrap();

let derby = config
.files
.get(&PathBuf::from("derby"))
.unwrap();

assert_eq!(
cat,
&FileTarget::Symbolic(PathBuf::from("~/.QuarticCat").into())
);

assert_ne!(
derby,
&FileTarget::Symbolic(PathBuf::from("~/.DerbyLantern").into())
);

assert_eq!(
derby,
&FileTarget::ComplexTemplate(PathBuf::from("~/.DerbyLantern").into())
);
}

#[test]
fn setting_default_target_type_template() {
let global: GlobalConfig = toml::from_str(
r#"
[settings]
default_target_type = "template"

[cat]
depends = []

[cat.files]
cat = '~/.QuarticCat'

[derby]
depends = []

[derby.files]
derby = { target = '~/.DerbyLantern', type = 'symbolic' }
"#,
)
.unwrap();

let local: LocalConfig = toml::from_str(
r#"
packages = ['cat', 'derby']
"#,
)
.unwrap();

let merged_config =
merge_configuration_files(global, local, None);

let config = merged_config.unwrap();

let cat = config
.files
.get(&PathBuf::from("cat"))
.unwrap();

let derby = config
.files
.get(&PathBuf::from("derby"))
.unwrap();

assert_eq!(
cat,
&FileTarget::ComplexTemplate(PathBuf::from("~/.QuarticCat").into())
);

assert_ne!(
derby,
&FileTarget::ComplexTemplate(PathBuf::from("~/.DerbyLantern").into())
);

assert_eq!(
derby,
&FileTarget::Symbolic(PathBuf::from("~/.DerbyLantern").into())
);
}

#[test]
fn setting_default_target_type_automatic() {
let global: GlobalConfig = toml::from_str(
r#"
[cat]
depends = []

[cat.files]
cat = '~/.QuarticCat'

[derby]
depends = []

[derby.files]
derby = { target = '~/.DerbyLantern', type = 'template' }

[sliver]
depends = []

[sliver.files]
sliver = { target = '~/.SliverBodacious', type = 'symbolic' }
"#,
)
.unwrap();

let local: LocalConfig = toml::from_str(
r#"
packages = ['cat', 'derby', 'sliver']
"#,
)
.unwrap();

let merged_config =
merge_configuration_files(global, local, None);

let config = merged_config.unwrap();

let cat = config
.files
.get(&PathBuf::from("cat"))
.unwrap();

let derby = config
.files
.get(&PathBuf::from("derby"))
.unwrap();

let sliver = config
.files
.get(&PathBuf::from("sliver"))
.unwrap();

assert_eq!(
cat,
&FileTarget::Automatic(PathBuf::from("~/.QuarticCat").into())
);

assert_eq!(
derby,
&FileTarget::ComplexTemplate(PathBuf::from("~/.DerbyLantern").into())
);

assert_eq!(
sliver,
&FileTarget::Symbolic(PathBuf::from("~/.SliverBodacious").into())
);
}
}
4 changes: 4 additions & 0 deletions src/handlebars_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ fn add_dotter_variable(

#[cfg(test)]
mod test {
use crate::config::Settings;

use super::*;

#[test]
Expand All @@ -362,6 +364,7 @@ mod test {
helpers: Helpers::new(),
packages: maplit::btreemap! { "default".into() => true, "disabled".into() => false },
recurse: true,
settings: Settings::default(),
};
let handlebars = create_new_handlebars(&mut config).unwrap();

Expand All @@ -388,6 +391,7 @@ mod test {
helpers: Helpers::new(),
packages: BTreeMap::new(),
recurse: true,
settings: Settings::default(),
};
let handlebars = create_new_handlebars(&mut config).unwrap();

Expand Down
Loading