]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/pxar/match_pattern.rs
pxar: Implement to_bytes() for MatchPattern in order to write them to file.
[proxmox-backup.git] / src / pxar / match_pattern.rs
index 088efba088b28edd349afd1148850649c4ea2235..7a7ef75d27401f8886b7062c38b73ffbf41fa0f8 100644 (file)
@@ -217,6 +217,33 @@ impl MatchPattern {
         }
     }
 
+    /// Convert a list of MatchPattern to bytes in order to write them to e.g.
+    /// a file.
+    pub fn to_bytes(patterns: &[MatchPattern]) -> Vec<u8> {
+        let mut buffer = Vec::new();
+        for pattern in patterns {
+            let byte_pattern = pattern.pattern.as_bytes();
+            match (pattern.match_positive, pattern.match_dir_only) {
+                (true, true) => {
+                    buffer.extend_from_slice(byte_pattern);
+                    buffer.push(b'/');
+                }
+                (true, false) => buffer.extend_from_slice(byte_pattern),
+                (false, true) => {
+                    buffer.push(b'!');
+                    buffer.extend_from_slice(byte_pattern);
+                    buffer.push(b'/');
+                }
+                (false, false) => {
+                    buffer.push(b'!');
+                    buffer.extend_from_slice(byte_pattern);
+                }
+            }
+            buffer.push(b'\n');
+        }
+        buffer
+    }
+
     /// Match the given filename against this `MatchPattern`.
     /// If the filename matches the pattern completely, `MatchType::Positive` or
     /// `MatchType::Negative` is returned, depending if the match pattern is was