Skip to content

Commit

Permalink
refactor(config): fix warnings by 'cargo clippy' for config
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Kumar <[email protected]>
  • Loading branch information
octonawish-akcodes committed Jan 24, 2024
1 parent 0f99a7c commit 96ed54f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 64 deletions.
2 changes: 1 addition & 1 deletion kclvm/config/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ where
create_dir_all(&cache_dir).unwrap();
let tmp_filename = temp_file(&cache_dir, pkgpath);
save_data_to_file(&dst_filename, &tmp_filename, data);
return Ok(());
Ok(())
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions kclvm/config/src/modfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub const DEFAULT_KPM_SUBDIR: &str = "kpm";
pub fn get_vendor_home() -> String {
match env::var(KCL_PKG_PATH) {
Ok(path) => path,
Err(_) => create_default_vendor_home().unwrap_or(String::default()),
Err(_) => create_default_vendor_home().unwrap_or_default(),
}
}

Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn get_pkg_root_from_paths(file_paths: &[String], workdir: String) -> Result
return Ok("".to_string());
}
if m.len() == 1 {
return Ok(last_root);
Ok(last_root)
} else if !workdir.is_empty() {
return Ok(workdir);
} else {
Expand Down
24 changes: 12 additions & 12 deletions kclvm/config/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ModRelativePath {
/// ```
pub fn is_relative_path(&self) -> Result<bool> {
Ok(Regex::new(RELATIVE_PATH_PREFFIX)?
.find(&self.path.as_bytes())?
.find(self.path.as_bytes())?
.map_or(false, |mat| mat.start() == 0))
}

Expand All @@ -97,7 +97,7 @@ impl ModRelativePath {
}

Ok(Regex::new(RELATIVE_PATH_PREFFIX)?
.captures(&self.path.as_bytes())?
.captures(self.path.as_bytes())?
.and_then(|caps| caps.name(ROOT_PKG_NAME_FLAG))
.map(|mat| std::str::from_utf8(mat.as_bytes()).map(|s| s.to_string()))
.transpose()?)
Expand All @@ -124,7 +124,7 @@ impl ModRelativePath {
}

Ok(Regex::new(RELATIVE_PATH_PREFFIX)?
.captures(&self.path.as_bytes())?
.captures(self.path.as_bytes())?
.map_or_else(
|| self.get_path(),
|caps| {
Expand All @@ -136,11 +136,11 @@ impl ModRelativePath {
"",
);
let res = PathBuf::from(root_path)
.join(sub_path.to_string())
.join(sub_path)
.display()
.to_string();

return res;
res
},
))
}
Expand All @@ -153,19 +153,19 @@ mod test_relative_path {
#[test]
fn test_is_relative_path() {
let path = ModRelativePath::new("${name:KCL_MOD}/src/path.rs".to_string());
assert_eq!(path.is_relative_path().unwrap(), true);
assert!(path.is_relative_path().unwrap());
let path = ModRelativePath::new("${KCL_MOD}/src/path.rs".to_string());
assert_eq!(path.is_relative_path().unwrap(), true);
assert!(path.is_relative_path().unwrap());
let path = ModRelativePath::new("/usr/${name:KCL_MOD}/src/path.rs".to_string());
assert_eq!(path.is_relative_path().unwrap(), false);
assert!(!path.is_relative_path().unwrap());
let path = ModRelativePath::new("/src/path.rs".to_string());
assert_eq!(path.is_relative_path().unwrap(), false);
assert!(!path.is_relative_path().unwrap());
let path = ModRelativePath::new("./src/path.rs".to_string());
assert_eq!(path.is_relative_path().unwrap(), false);
assert!(!path.is_relative_path().unwrap());
let path = ModRelativePath::new("${K_MOD}/src/path.rs".to_string());
assert_eq!(path.is_relative_path().unwrap(), false);
assert!(!path.is_relative_path().unwrap());
let path = ModRelativePath::new("${:KCL_MOD}/src/path.rs".to_string());
assert_eq!(path.is_relative_path().unwrap(), false);
assert!(!path.is_relative_path().unwrap());
}

#[test]
Expand Down
98 changes: 49 additions & 49 deletions kclvm/config/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,55 @@ pub fn merge_settings(settings: &[SettingsFile]) -> SettingsFile {
result
}

/// Build SettingsPathBuf from args.
pub fn build_settings_pathbuf(
files: &[&str],
setting_files: Option<Vec<&str>>,
setting_config: Option<SettingsFile>,
) -> Result<SettingsPathBuf> {
let mut path = None;
let settings = if let Some(files) = setting_files {
let mut settings = vec![];
for file in &files {
let s = load_file(file)?;
if !s.input().is_empty() {
path = Some(
PathBuf::from(file)
.parent()
.map(|p| p.to_path_buf())
.ok_or(anyhow::anyhow!("The parent path of {file} is not found"))?,
)
}
settings.push(s);
}
merge_settings(&settings)
// If exists default kcl.yaml, load it.
} else if std::fs::metadata(DEFAULT_SETTING_FILE).is_ok() {
path = Some(
PathBuf::from(DEFAULT_SETTING_FILE)
.parent()
.map(|p| p.to_path_buf())
.ok_or(anyhow::anyhow!(
"The parent path of {DEFAULT_SETTING_FILE} is not found"
))?,
);
load_file(DEFAULT_SETTING_FILE)?
} else {
SettingsFile::default()
};
let mut settings = if let Some(setting_config) = setting_config {
merge_settings(&[settings, setting_config])
} else {
settings
};
if let Some(config) = &mut settings.kcl_cli_configs {
if !files.is_empty() {
config.files = Some(files.iter().map(|f| f.to_string()).collect());
}
}
Ok(SettingsPathBuf::new(path, settings))
}

#[cfg(test)]
mod settings_test {
use crate::settings::*;
Expand Down Expand Up @@ -466,52 +515,3 @@ mod settings_test {
Ok(())
}
}

/// Build SettingsPathBuf from args.
pub fn build_settings_pathbuf(
files: &[&str],
setting_files: Option<Vec<&str>>,
setting_config: Option<SettingsFile>,
) -> Result<SettingsPathBuf> {
let mut path = None;
let settings = if let Some(files) = setting_files {
let mut settings = vec![];
for file in &files {
let s = load_file(file)?;
if !s.input().is_empty() {
path = Some(
PathBuf::from(file)
.parent()
.map(|p| p.to_path_buf())
.ok_or(anyhow::anyhow!("The parent path of {file} is not found"))?,
)
}
settings.push(s);
}
merge_settings(&settings)
// If exists default kcl.yaml, load it.
} else if std::fs::metadata(DEFAULT_SETTING_FILE).is_ok() {
path = Some(
PathBuf::from(DEFAULT_SETTING_FILE)
.parent()
.map(|p| p.to_path_buf())
.ok_or(anyhow::anyhow!(
"The parent path of {DEFAULT_SETTING_FILE} is not found"
))?,
);
load_file(DEFAULT_SETTING_FILE)?
} else {
SettingsFile::default()
};
let mut settings = if let Some(setting_config) = setting_config {
merge_settings(&[settings, setting_config])
} else {
settings
};
if let Some(config) = &mut settings.kcl_cli_configs {
if !files.is_empty() {
config.files = Some(files.iter().map(|f| f.to_string()).collect());
}
}
Ok(SettingsPathBuf::new(path, settings))
}

0 comments on commit 96ed54f

Please sign in to comment.