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

Move recurse flag to Settings struct #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 25 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,25 @@ pub enum DefaultTargetType {
Automatic,
}

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

impl Default for Settings {
fn default() -> Self {
Settings {
default_target_type: DefaultTargetType::default(),
recurse: true,
}
}
}

fn default_true() -> bool {
true
}

#[derive(Debug, Clone)]
Expand All @@ -100,13 +115,6 @@ pub struct Configuration {
#[cfg(feature = "scripting")]
pub helpers: Helpers,

/// If the source is a directory, or a symlink to a directory,
/// and this option is true, the source will be recursed and
/// turned into a list of all the files inside the structure that
/// are readable.
pub recurse: bool,

#[allow(dead_code)]
pub settings: Settings,
}

Expand Down Expand Up @@ -348,8 +356,7 @@ fn merge_configuration_files(
files: Files::default(),
variables: Variables::default(),
packages: packages_map,
recurse: true,
settings: Settings::default(),
settings: global.settings,
};

// Merge all the packages
Expand Down Expand Up @@ -395,7 +402,7 @@ fn merge_configuration_files(

for value in output.files.values_mut() {
if let FileTarget::Automatic(target) = value {
*value = match global.settings.default_target_type {
*value = match output.settings.default_target_type {
DefaultTargetType::Symbolic => {
FileTarget::Symbolic(SymbolicTarget::from(target.clone()))
}
Expand Down Expand Up @@ -554,16 +561,12 @@ fn expand_directory(source: &Path, target: &FileTarget, config: &Configuration)
condition: _,
recurse: Some(rec),
}) => *rec,
_ => config.recurse,
_ => config.settings.recurse,
};

trace!("expanding '{source:?}', recurse: {recurse}");

if !recurse || !metadata.is_dir() {
let mut map = Files::new();
map.insert(source.into(), target.clone());
Ok(map)
} else {
if recurse && metadata.is_dir() {
let expanded = fs::read_dir(source)
.context("read contents of directory")?
.map(|child| -> Result<Files> {
Expand All @@ -576,6 +579,10 @@ fn expand_directory(source: &Path, target: &FileTarget, config: &Configuration)
})
.collect::<Result<Vec<Files>>>()?; // Use transposition of Iterator<Result<T,E>> -> Result<Sequence<T>, E>
Ok(expanded.into_iter().flatten().collect())
} else {
let mut map = Files::new();
map.insert(source.into(), target.clone());
Ok(map)
}
}

Expand Down Expand Up @@ -817,7 +824,7 @@ mod test {

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

assert_eq!(
Expand Down
2 changes: 0 additions & 2 deletions src/handlebars_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ mod test {
#[cfg(feature = "scripting")]
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 @@ -390,7 +389,6 @@ mod test {
#[cfg(feature = "scripting")]
helpers: Helpers::new(),
packages: BTreeMap::new(),
recurse: true,
settings: Settings::default(),
};
let handlebars = create_new_handlebars(&mut config).unwrap();
Expand Down
Loading