implement filenames on commandline

This commit is contained in:
Ward Wouts 2025-05-13 13:58:04 +02:00
parent 12da2e2970
commit 708c57e726
3 changed files with 57 additions and 15 deletions

View file

@ -12,11 +12,26 @@ enum DirListEntry {
Text(String)
}
impl ToString for DirListEntry {
impl DirListEntry {
fn to_string(&self) -> String {
match self {
DirListEntry::Text(line) => line.clone(),
DirListEntry::Path(path) => path.display().to_string()[2..].to_string(),
// TODO only strip first to chars if they're `./`
DirListEntry::Path(path) => {
let checkstring = path.display().to_string();
if &checkstring[0..2] == "./" {
path.display().to_string()[2..].to_string()
} else {
path.display().to_string()
}
},
}
}
fn exists(&self) -> bool {
match self {
DirListEntry::Text(filename) => Path::new(filename).exists(),
DirListEntry::Path(path) => path.exists(),
}
}
}
@ -72,12 +87,12 @@ impl DirList {
}
}
pub fn from_list(list: &[String]) -> Self {
pub fn from_list(list: &[PathBuf]) -> Self {
Self {
safe_source: false,
noop: false,
verbose: false,
entries: list.iter().map(|l| DirListEntry::Text(l.to_string())).collect(),
entries: list.iter().map(|l| DirListEntry::Path(l.clone())).collect(),
}
}
@ -102,8 +117,20 @@ impl DirList {
let mut intermediate_files: HashMap<String, String> = HashMap::new();
self.run_basic_checks(target_list)?;
if ! self.safe_source {
println!("Implement check if target files already exist");
// if target == source => next
// if target exists and is not in source list => Err
// if target exists and is in source list => use intermediate files, which is normal functionality below
let tgt_len = target_list.entries.len();
for i in 0..tgt_len {
if self.entries[i].to_string() != target_list.entries[i].to_string() {
if target_list.entries[i].exists() {
return Err(format!("ERROR: Target '{}' already exists", target_list.entries[i].to_string()).to_string());
}
}
}
}
for i in 0..src_len {
@ -151,8 +178,7 @@ impl DirList {
fn run_basic_checks(&self, target_list: &DirList) -> Result<(), String> {
// Make sure there are an equal number of sources and destinations
if self.entries.len() != target_list.entries.len() {
return Err("ERROR: Source and target list don't have the same number of
lines.".to_string());
return Err("ERROR: Source and target list don't have the same number of lines.".to_string());
}
// Make sure destination names are not empty