use crate::ArchiveError;
use crate::Limit;
use crate::Result;
use crate::creation::filters::ExcludePattern;
use std::num::NonZeroU64;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CompressionLevel(u8);
impl CompressionLevel {
#[must_use]
pub const fn get(self) -> u8 {
self.0
}
}
impl Default for CompressionLevel {
fn default() -> Self {
Self(6)
}
}
impl TryFrom<u8> for CompressionLevel {
type Error = ArchiveError;
fn try_from(level: u8) -> Result<Self> {
if (1..=9).contains(&level) {
Ok(Self(level))
} else {
Err(ArchiveError::InvalidCompressionLevel {
level: u32::from(level),
})
}
}
}
impl TryFrom<u32> for CompressionLevel {
type Error = ArchiveError;
fn try_from(level: u32) -> Result<Self> {
u8::try_from(level)
.ok()
.and_then(|level| Self::try_from(level).ok())
.ok_or(ArchiveError::InvalidCompressionLevel { level })
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CreationConfig {
pub follow_symlinks: bool,
pub include_hidden: bool,
pub max_file_size: Limit<NonZeroU64>,
pub exclude_patterns: Vec<ExcludePattern>,
pub strip_prefix: Option<PathBuf>,
pub compression_level: CompressionLevel,
pub preserve_permissions: bool,
}
impl Default for CreationConfig {
fn default() -> Self {
Self {
follow_symlinks: false,
include_hidden: false,
max_file_size: Limit::Unbounded,
exclude_patterns: vec![
ExcludePattern::exact(".git"),
ExcludePattern::exact(".DS_Store"),
ExcludePattern::suffix(".tmp"),
],
strip_prefix: None,
compression_level: CompressionLevel::default(),
preserve_permissions: true,
}
}
}
impl CreationConfig {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_follow_symlinks(mut self, follow: bool) -> Self {
self.follow_symlinks = follow;
self
}
#[must_use]
pub fn with_include_hidden(mut self, include: bool) -> Self {
self.include_hidden = include;
self
}
#[must_use]
pub fn with_max_file_size(mut self, max_size: impl Into<Limit<NonZeroU64>>) -> Self {
self.max_file_size = max_size.into();
self
}
#[must_use]
pub fn with_exclude_patterns(mut self, patterns: Vec<ExcludePattern>) -> Self {
self.exclude_patterns = patterns;
self
}
#[must_use]
pub fn with_strip_prefix(mut self, prefix: Option<PathBuf>) -> Self {
self.strip_prefix = prefix;
self
}
#[must_use]
pub fn with_compression_level(mut self, level: CompressionLevel) -> Self {
self.compression_level = level;
self
}
#[must_use]
pub fn with_preserve_permissions(mut self, preserve: bool) -> Self {
self.preserve_permissions = preserve;
self
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use std::assert_matches;
#[test]
fn test_creation_config_default() {
let config = CreationConfig::default();
assert!(!config.follow_symlinks);
assert!(!config.include_hidden);
assert_eq!(config.max_file_size, Limit::Unbounded);
assert_eq!(config.exclude_patterns.len(), 3);
let shown: Vec<String> = config
.exclude_patterns
.iter()
.map(ToString::to_string)
.collect();
assert_eq!(shown, [".git", ".DS_Store", "*.tmp"]);
assert_eq!(config.strip_prefix, None);
assert_eq!(config.compression_level, CompressionLevel::default());
assert!(config.preserve_permissions);
}
#[test]
fn test_creation_config_builder() {
let level = CompressionLevel::try_from(9u8).unwrap();
let config = CreationConfig::default()
.with_follow_symlinks(true)
.with_include_hidden(true)
.with_max_file_size(crate::non_zero!(1024 * 1024))
.with_exclude_patterns(vec![ExcludePattern::try_from("*.log").unwrap()])
.with_strip_prefix(Some(PathBuf::from("/base")))
.with_compression_level(level)
.with_preserve_permissions(false);
assert!(config.follow_symlinks);
assert!(config.include_hidden);
assert_eq!(
config.max_file_size,
Limit::Bounded(crate::non_zero!(1024 * 1024))
);
assert_eq!(
config.exclude_patterns,
vec![ExcludePattern::try_from("*.log").unwrap()]
);
assert_eq!(config.strip_prefix, Some(PathBuf::from("/base")));
assert_eq!(config.compression_level, level);
assert!(!config.preserve_permissions);
}
#[test]
fn test_compression_level_refuses_out_of_range() {
for level in [0u8, 10, 200] {
assert_matches!(
CompressionLevel::try_from(level),
Err(ArchiveError::InvalidCompressionLevel { level: l }) if l == u32::from(level)
);
}
for level in [0u32, 10, 256, 4_294_967_295] {
assert_matches!(
CompressionLevel::try_from(level),
Err(ArchiveError::InvalidCompressionLevel { level: l }) if l == level
);
}
}
#[test]
fn test_compression_level_accepts_the_whole_scale() {
for level in 1u8..=9 {
assert_eq!(CompressionLevel::try_from(level).unwrap().get(), level);
assert_eq!(
CompressionLevel::try_from(u32::from(level)).unwrap().get(),
level
);
}
assert_eq!(CompressionLevel::default().get(), 6);
}
#[test]
fn test_creation_config_new() {
let config = CreationConfig::new();
assert_eq!(config.compression_level, CompressionLevel::default());
assert!(config.preserve_permissions);
}
#[test]
fn test_creation_config_secure_defaults() {
let config = CreationConfig::default();
assert!(
!config.follow_symlinks,
"should not follow symlinks by default (security)"
);
assert!(
!config.include_hidden,
"should not include hidden files by default"
);
assert!(
config
.exclude_patterns
.contains(&ExcludePattern::exact(".git")),
"should exclude .git by default"
);
}
}