From e50a90e01036f5ddd74a3224452bba0d3feb6abf Mon Sep 17 00:00:00 2001 From: Christian Ebner Date: Tue, 22 Oct 2019 15:07:49 +0200 Subject: [PATCH] pxar: Implement to_bytes() for MatchPattern in order to write them to file. Signed-off-by: Christian Ebner --- src/pxar/match_pattern.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/pxar/match_pattern.rs b/src/pxar/match_pattern.rs index 088efba0..7a7ef75d 100644 --- a/src/pxar/match_pattern.rs +++ b/src/pxar/match_pattern.rs @@ -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 { + 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