use crate::error::{Result, ShamirError};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SplitMode {
Sequential,
Parallel,
Streaming,
}
impl Default for SplitMode {
fn default() -> Self {
Self::Sequential
}
}
#[derive(Debug, Clone)]
pub struct Config {
pub chunk_size: usize,
pub mode: SplitMode,
pub compression: bool,
pub integrity_check: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
chunk_size: 1024 * 1024, mode: SplitMode::default(),
compression: false,
integrity_check: true,
}
}
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn with_chunk_size(mut self, size: usize) -> Result<Self> {
if size == 0 {
return Err(ShamirError::InvalidConfig(
"Chunk size cannot be zero".into(),
));
}
self.chunk_size = size;
Ok(self)
}
pub fn with_mode(mut self, mode: SplitMode) -> Self {
self.mode = mode;
self
}
#[cfg(feature = "compress")]
pub fn with_compression(mut self, enabled: bool) -> Self {
self.compression = enabled;
self
}
pub fn with_integrity_check(mut self, enabled: bool) -> Self {
self.integrity_check = enabled;
self
}
pub fn validate(&self) -> Result<()> {
if self.chunk_size == 0 {
return Err(ShamirError::InvalidConfig(
"Chunk size cannot be zero".into(),
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = Config::default();
assert_eq!(config.mode, SplitMode::Sequential);
assert_eq!(config.chunk_size, 1024 * 1024);
assert!(!config.compression);
assert!(config.integrity_check);
}
#[test]
fn test_config_builder() {
let mut config = Config::new()
.with_chunk_size(4096)
.unwrap()
.with_mode(SplitMode::Parallel)
.with_integrity_check(false);
#[cfg(feature = "compress")]
{
config = config.with_compression(true);
assert!(config.compression);
}
assert_eq!(config.chunk_size, 4096);
assert_eq!(config.mode, SplitMode::Parallel);
assert!(!config.integrity_check);
}
#[test]
fn test_invalid_config() {
assert!(Config::new().with_chunk_size(0).is_err());
}
}