pxar: Implement to_bytes() for MatchPattern in order to write them to file.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
This commit is contained in:
Christian Ebner 2019-10-22 15:07:49 +02:00 committed by Dietmar Maurer
parent a8f10f849e
commit e50a90e010

View 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`. /// Match the given filename against this `MatchPattern`.
/// If the filename matches the pattern completely, `MatchType::Positive` or /// If the filename matches the pattern completely, `MatchType::Positive` or
/// `MatchType::Negative` is returned, depending if the match pattern is was /// `MatchType::Negative` is returned, depending if the match pattern is was