use crate::entry::ZipEntry;
use crate::spec::{attribute::AttributeCompatibility, header::ExtraField, Compression};
use crate::{date::ZipDateTime, string::ZipString};
pub struct ZipEntryBuilder(pub(crate) ZipEntry);
impl From<ZipEntry> for ZipEntryBuilder {
fn from(entry: ZipEntry) -> Self {
Self(entry)
}
}
impl ZipEntryBuilder {
pub fn new(filename: ZipString, compression: Compression) -> Self {
Self(ZipEntry::new(filename, compression))
}
pub fn filename(mut self, filename: ZipString) -> Self {
self.0.filename = filename;
self
}
pub fn compression(mut self, compression: Compression) -> Self {
self.0.compression = compression;
self
}
pub fn crc32<N: Into<u32>>(mut self, crc: N) -> Self {
self.0.crc32 = crc.into();
self
}
pub fn compressed_size<N: Into<u64>>(mut self, size: N) -> Self {
self.0.compressed_size = size.into();
self
}
pub fn uncompressed_size<N: Into<u64>>(mut self, size: N) -> Self {
self.0.uncompressed_size = size.into();
self
}
#[cfg(any(feature = "deflate", feature = "bzip2", feature = "zstd", feature = "lzma", feature = "xz"))]
pub fn deflate_option(mut self, option: crate::DeflateOption) -> Self {
self.0.compression_level = option.into_level();
self
}
pub fn attribute_compatibility(mut self, compatibility: AttributeCompatibility) -> Self {
self.0.attribute_compatibility = compatibility;
self
}
pub fn last_modification_date(mut self, date: ZipDateTime) -> Self {
self.0.last_modification_date = date;
self
}
pub fn internal_file_attribute(mut self, attribute: u16) -> Self {
self.0.internal_file_attribute = attribute;
self
}
pub fn external_file_attribute(mut self, attribute: u32) -> Self {
self.0.external_file_attribute = attribute;
self
}
pub fn extra_fields(mut self, field: Vec<ExtraField>) -> Self {
self.0.extra_fields = field;
self
}
pub fn comment(mut self, comment: ZipString) -> Self {
self.0.comment = comment;
self
}
pub fn unix_permissions(mut self, mode: u16) -> Self {
if matches!(self.0.attribute_compatibility, AttributeCompatibility::Unix) {
self.0.external_file_attribute = (self.0.external_file_attribute & 0xFFFF) | (mode as u32) << 16;
}
self
}
pub fn current(&self) -> &ZipEntry {
&self.0
}
pub fn build(self) -> ZipEntry {
self.into()
}
}