Debian Codegraph @

src/entry/builder.rs

raw symbols SCIP
1 // Copyright (c) 2022 Harry [Majored] [[email protected]]
2 // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4 use crate::entry::ZipEntry;
5 use crate::spec::{attribute::AttributeCompatibility, header::ExtraField, Compression};
6 use crate::{date::ZipDateTime, string::ZipString};
7
8 /// A builder for [`ZipEntry`].
9 pub struct ZipEntryBuilder(pub(crate) ZipEntry);
10
11 impl From<ZipEntry> for ZipEntryBuilder {
12 fn from(entry: ZipEntry) -> Self {
13 Self(entry)
14 }
15 }
16
17 impl ZipEntryBuilder {
18 /// Constructs a new builder which defines the raw underlying data of a ZIP entry.
19 ///
20 /// A filename and compression method are needed to construct the builder as minimal parameters.
21 pub fn new(filename: ZipString, compression: Compression) -> Self {
22 Self(ZipEntry::new(filename, compression))
23 }
24
25 /// Sets the entry's filename.
26 pub fn filename(mut self, filename: ZipString) -> Self {
27 self.0.filename = filename;
28 self
29 }
30
31 /// Sets the entry's compression method.
32 pub fn compression(mut self, compression: Compression) -> Self {
33 self.0.compression = compression;
34 self
35 }
36
37 // Sets the entry's CRC32 checksum.
38 pub fn crc32<N: Into<u32>>(mut self, crc: N) -> Self {
39 self.0.crc32 = crc.into();
40 self
41 }
42
43 // Sets the entry's compressed size.
44 pub fn compressed_size<N: Into<u64>>(mut self, size: N) -> Self {
45 self.0.compressed_size = size.into();
46 self
47 }
48
49 // Sets the entry's uncompressed size.
50 pub fn uncompressed_size<N: Into<u64>>(mut self, size: N) -> Self {
51 self.0.uncompressed_size = size.into();
52 self
53 }
54
55 /// Set the deflate compression option.
56 ///
57 /// If the compression type isn't deflate, this option has no effect.
58 #[cfg(any(feature = "deflate", feature = "bzip2", feature = "zstd", feature = "lzma", feature = "xz"))]
59 pub fn deflate_option(mut self, option: crate::DeflateOption) -> Self {
60 self.0.compression_level = option.into_level();
61 self
62 }
63
64 /// Sets the entry's attribute host compatibility.
65 pub fn attribute_compatibility(mut self, compatibility: AttributeCompatibility) -> Self {
66 self.0.attribute_compatibility = compatibility;
67 self
68 }
69
70 /// Sets the entry's last modification date.
71 pub fn last_modification_date(mut self, date: ZipDateTime) -> Self {
72 self.0.last_modification_date = date;
73 self
74 }
75
76 /// Sets the entry's internal file attribute.
77 pub fn internal_file_attribute(mut self, attribute: u16) -> Self {
78 self.0.internal_file_attribute = attribute;
79 self
80 }
81
82 /// Sets the entry's external file attribute.
83 pub fn external_file_attribute(mut self, attribute: u32) -> Self {
84 self.0.external_file_attribute = attribute;
85 self
86 }
87
88 /// Sets the entry's extra field data.
89 pub fn extra_fields(mut self, field: Vec<ExtraField>) -> Self {
90 self.0.extra_fields = field;
91 self
92 }
93
94 /// Sets the entry's file comment.
95 pub fn comment(mut self, comment: ZipString) -> Self {
96 self.0.comment = comment;
97 self
98 }
99
100 /// Sets the entry's Unix permissions mode.
101 ///
102 /// If the attribute host compatibility isn't set to Unix, this will have no effect.
103 pub fn unix_permissions(mut self, mode: u16) -> Self {
104 if matches!(self.0.attribute_compatibility, AttributeCompatibility::Unix) {
105 self.0.external_file_attribute = (self.0.external_file_attribute & 0xFFFF) | (mode as u32) << 16;
106 }
107 self
108 }
109
110 /// Returns a reference to the currently built entry.
111 pub fn current(&self) -> &ZipEntry {
112 &self.0
113 }
114
115 /// Consumes this builder and returns a final [`ZipEntry`].
116 ///
117 /// This is equivalent to:
118 /// ```
119 /// # use async_zip::{ZipEntry, ZipEntryBuilder, Compression};
120 /// #
121 /// # let builder = ZipEntryBuilder::new(String::from("foo.bar").into(), Compression::Stored);
122 /// let entry: ZipEntry = builder.into();
123 /// ```
124 pub fn build(self) -> ZipEntry {
125 self.into()
126 }
127 }