summary history branches tags files
commit:96be147a1f7a67f47ad290fabfdc3fb492b09d77
author:Trevor Bentley
committer:Trevor Bentley
date:Mon Jan 13 23:51:24 2025 +0100
parents:f1facf30a992ef2c657a59b99515fa37a7eb878f
allow setting policies from cli
diff --git a/src/main.rs b/src/main.rs
line changes: +31/-9
index 9d137fa..bb6026e
--- a/src/main.rs
+++ b/src/main.rs
@@ -62,6 +62,14 @@ struct PwwArgs {
     #[arg(short = 's', long)]
     skip_processed: bool,
 
+    /// Policy for which metadata should be updated
+    #[arg(long)]
+    file_update_policy: FileUpdatePolicy,
+
+    /// Policy for how changes should be made to the specified tags.
+    #[arg(long)]
+    tag_update_policy: TagUpdatePolicy,
+
     /// Print information about which tags are written
     #[arg(short = 'v', long)]
     verbose: bool,
@@ -88,18 +96,18 @@ struct PwwArgs {
     image_paths: Vec<PathBuf>,
 }
 
-#[derive(Default, Clone, Serialize, Deserialize, ValueEnum)]
+#[derive(Default, Debug, Clone, Serialize, Deserialize, ValueEnum)]
 enum TagUpdatePolicy {
-    // Replace all existing contents (if any) with new items
+    /// Replace all existing contents (if any) with new items
     Replace,
-    // Append new items to existing tag contents
+    /// Append new items to existing tag contents
     Append,
-    // Remove existing items with the same prefix and then append new ones.
+    /// Remove existing items with the same prefix and then append new ones.
     #[default]
     ReplacePrefixed,
 }
 
-#[derive(Default, Clone, Serialize, Deserialize, ValueEnum, PartialEq)]
+#[derive(Default, Debug, Clone, Serialize, Deserialize, ValueEnum, PartialEq)]
 enum FileUpdatePolicy {
     /// Just print the tags, do not update any files
     DisplayOnly,
@@ -315,6 +323,20 @@ impl PwwConfig {
         self.cli.as_ref().map(|c| c.skip_processed).unwrap_or(self.skip_processed)
     }
 
+    fn tag_update_policy(&self) -> TagUpdatePolicy {
+        match &self.cli {
+            Some(c) => c.tag_update_policy.clone(),
+            _ => self.tag_update_policy.clone(),
+        }
+    }
+
+    fn file_update_policy(&self) -> FileUpdatePolicy {
+        match &self.cli {
+            Some(c) => c.file_update_policy.clone(),
+            _ => self.file_update_policy.clone(),
+        }
+    }
+
     fn verbose(&self) -> bool {
         self.cli.as_ref().map(|c| c.verbose).unwrap_or_default()
     }
@@ -525,7 +547,7 @@ fn should_update_sidecar<P: AsRef<Path>>(config: &PwwConfig, filepath: P) -> Res
     // Return path to XMP file if it exists and we're in a mode that
     // updates it, None if we shouldn't update it, or error if it's
     // required but missing.
-    match config.file_update_policy {
+    match config.file_update_policy() {
         FileUpdatePolicy::SidecarOnly | FileUpdatePolicy::SidecarAndImage => {
             match xmp_path {
                 Some(_) => Ok(xmp_path),
@@ -560,7 +582,7 @@ fn should_update_sidecar<P: AsRef<Path>>(config: &PwwConfig, filepath: P) -> Res
 }
 
 fn should_update_image<P: AsRef<Path>>(config: &PwwConfig, filepath: P) -> Result<bool, PwwError> {
-    match config.file_update_policy {
+    match config.file_update_policy() {
         FileUpdatePolicy::ImageOnly | FileUpdatePolicy::SidecarAndImage => Ok(true),
         FileUpdatePolicy::SidecarIfPresent => {
             match xmp_file_path(&filepath) {
@@ -592,7 +614,7 @@ fn write_tags_to_file<P: AsRef<std::ffi::OsStr>>(config: &PwwConfig, metatags: &
         println!(" - updating file: {}", filepath.as_ref().to_string_lossy());
     }
     for metatag in metatags {
-        let new_values: Vec<String> = match config.tag_update_policy {
+        let new_values: Vec<String> = match config.tag_update_policy() {
             TagUpdatePolicy::Append => {
                 meta.get_tag_multiple_strings(&metatag).unwrap_or_default().into_iter().chain(tags.iter().cloned()).collect()
             },
@@ -774,7 +796,7 @@ fn process_image(config: &PwwConfig, input_path: &Path) -> Result<(), PwwError> 
         }
     }
 
-    if config.file_update_policy == FileUpdatePolicy::DisplayOnly {
+    if config.file_update_policy() == FileUpdatePolicy::DisplayOnly {
         println!("{}", tags.join(", "));
     }