Skip to content

Commit

Permalink
♻️ Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Nov 16, 2024
1 parent 246a801 commit ac26e94
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 53 deletions.
32 changes: 13 additions & 19 deletions lib/src/archive/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<W: Write> Archive<W> {
#[inline]
fn write_header_with(mut write: W, header: ArchiveHeader) -> io::Result<Self> {
write.write_all(PNA_HEADER)?;
(ChunkType::AHED, header.to_bytes().as_slice()).write_chunk_in(&mut write)?;
(ChunkType::AHED, header.to_bytes()).write_chunk_in(&mut write)?;
Ok(Self::new(write, header))
}

Expand Down Expand Up @@ -134,16 +134,13 @@ impl<W: Write> Archive<W> {
);
(ChunkType::FHED, header.to_bytes()).write_chunk_in(&mut self.inner)?;
if let Some(c) = metadata.created {
(ChunkType::cTIM, c.as_secs().to_be_bytes().as_slice())
.write_chunk_in(&mut self.inner)?;
(ChunkType::cTIM, c.as_secs().to_be_bytes()).write_chunk_in(&mut self.inner)?;
}
if let Some(m) = metadata.modified {
(ChunkType::mTIM, m.as_secs().to_be_bytes().as_slice())
.write_chunk_in(&mut self.inner)?;
(ChunkType::mTIM, m.as_secs().to_be_bytes()).write_chunk_in(&mut self.inner)?;
}
if let Some(a) = metadata.accessed {
(ChunkType::aTIM, a.as_secs().to_be_bytes().as_slice())
.write_chunk_in(&mut self.inner)?;
(ChunkType::aTIM, a.as_secs().to_be_bytes()).write_chunk_in(&mut self.inner)?;
}
if let Some(p) = metadata.permission {
(ChunkType::fPRM, p.to_bytes()).write_chunk_in(&mut self.inner)?;
Expand Down Expand Up @@ -233,7 +230,7 @@ impl<W: Write> Archive<W> {

fn add_next_archive_marker(&mut self) -> io::Result<usize> {
let mut chunk_writer = ChunkWriter::from(&mut self.inner);
chunk_writer.write_chunk((ChunkType::ANXT, [].as_slice()))
chunk_writer.write_chunk((ChunkType::ANXT, []))
}

/// Split to the next archive.
Expand Down Expand Up @@ -289,7 +286,7 @@ impl<W: Write> Archive<W> {
#[inline]
pub fn finalize(mut self) -> io::Result<W> {
let mut chunk_writer = ChunkWriter::from(&mut self.inner);
chunk_writer.write_chunk((ChunkType::AEND, [].as_slice()))?;
chunk_writer.write_chunk((ChunkType::AEND, []))?;
Ok(self.inner)
}
}
Expand All @@ -309,7 +306,7 @@ impl<W: AsyncWrite + Unpin> Archive<W> {
write.write_all(PNA_HEADER).await?;
let mut chunk_writer = ChunkWriter::from(&mut write);
chunk_writer
.write_chunk_async((ChunkType::AHED, header.to_bytes().as_slice()))
.write_chunk_async((ChunkType::AHED, header.to_bytes()))
.await?;
Ok(Self::new(write, header))
}
Expand All @@ -330,7 +327,7 @@ impl<W: AsyncWrite + Unpin> Archive<W> {
pub async fn finalize_async(mut self) -> io::Result<W> {
let mut chunk_writer = ChunkWriter::from(&mut self.inner);
chunk_writer
.write_chunk_async((ChunkType::AEND, [].as_slice()))
.write_chunk_async((ChunkType::AEND, []))
.await?;
Ok(self.inner)
}
Expand Down Expand Up @@ -382,7 +379,7 @@ impl<W: Write> Archive<W> {
);
let context = get_writer_context(option)?;

(ChunkType::SHED, header.to_bytes().as_slice()).write_chunk_in(&mut self.inner)?;
(ChunkType::SHED, header.to_bytes()).write_chunk_in(&mut self.inner)?;
if let Some(WriteCipher { context: c, .. }) = &context.cipher {
(ChunkType::PHSF, c.phsf.as_bytes()).write_chunk_in(&mut self.inner)?;
(ChunkType::SDAT, c.iv.as_slice()).write_chunk_in(&mut self.inner)?;
Expand Down Expand Up @@ -466,16 +463,13 @@ impl<W: Write> SolidArchive<W> {
);
(ChunkType::FHED, header.to_bytes()).write_chunk_in(&mut self.inner)?;
if let Some(c) = metadata.created {
(ChunkType::cTIM, c.as_secs().to_be_bytes().as_slice())
.write_chunk_in(&mut self.inner)?;
(ChunkType::cTIM, c.as_secs().to_be_bytes()).write_chunk_in(&mut self.inner)?;
}
if let Some(m) = metadata.modified {
(ChunkType::mTIM, m.as_secs().to_be_bytes().as_slice())
.write_chunk_in(&mut self.inner)?;
(ChunkType::mTIM, m.as_secs().to_be_bytes()).write_chunk_in(&mut self.inner)?;
}
if let Some(a) = metadata.accessed {
(ChunkType::aTIM, a.as_secs().to_be_bytes().as_slice())
.write_chunk_in(&mut self.inner)?;
(ChunkType::aTIM, a.as_secs().to_be_bytes()).write_chunk_in(&mut self.inner)?;
}
if let Some(p) = metadata.permission {
(ChunkType::fPRM, p.to_bytes()).write_chunk_in(&mut self.inner)?;
Expand Down Expand Up @@ -527,7 +521,7 @@ impl<W: Write> SolidArchive<W> {
fn finalize_solid_entry(mut self) -> io::Result<Archive<W>> {
self.inner.flush()?;
let mut inner = self.inner.try_into_inner()?.try_into_inner()?.into_inner();
(ChunkType::SEND, [].as_slice()).write_chunk_in(&mut inner)?;
(ChunkType::SEND, []).write_chunk_in(&mut inner)?;
Ok(Archive::new(inner, self.archive_header))
}
}
Expand Down
7 changes: 1 addition & 6 deletions lib/src/chunk/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ mod tests {
#[test]
fn write_aend_chunk() {
let mut chunk_writer = ChunkWriter::from(Vec::new());
assert_eq!(
chunk_writer
.write_chunk((ChunkType::AEND, [].as_slice()))
.unwrap(),
12
);
assert_eq!(chunk_writer.write_chunk((ChunkType::AEND, [])).unwrap(), 12);
assert_eq!(
chunk_writer.w,
[0, 0, 0, 0, 65, 69, 78, 68, 107, 246, 72, 109]
Expand Down
47 changes: 19 additions & 28 deletions lib/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,17 @@ impl SealedEntryExt for SolidEntry<Vec<u8>> {

fn write_in<W: Write>(&self, writer: &mut W) -> io::Result<usize> {
let mut total = 0;
total += (ChunkType::SHED, self.header.to_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::SHED, self.header.to_bytes()).write_chunk_in(writer)?;
for extra_chunk in &self.extra {
total += extra_chunk.write_chunk_in(writer)?;
}
if let Some(phsf) = &self.phsf {
total += (ChunkType::PHSF, phsf.as_bytes()).write_chunk_in(writer)?;
}
for data in &self.data {
total += (ChunkType::SDAT, data.as_slice()).write_chunk_in(writer)?;
total += (ChunkType::SDAT, data).write_chunk_in(writer)?;
}
total += (ChunkType::SEND, [].as_slice()).write_chunk_in(writer)?;
total += (ChunkType::SEND, []).write_chunk_in(writer)?;
Ok(total)
}
}
Expand All @@ -333,7 +333,7 @@ impl SealedEntryExt for SolidEntry<&[u8]> {

fn write_in<W: Write>(&self, writer: &mut W) -> io::Result<usize> {
let mut total = 0;
total += (ChunkType::SHED, self.header.to_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::SHED, self.header.to_bytes()).write_chunk_in(writer)?;
for extra_chunk in &self.extra {
total += extra_chunk.write_chunk_in(writer)?;
}
Expand All @@ -343,7 +343,7 @@ impl SealedEntryExt for SolidEntry<&[u8]> {
for data in &self.data {
total += (ChunkType::SDAT, *data).write_chunk_in(writer)?;
}
total += (ChunkType::SEND, [].as_slice()).write_chunk_in(writer)?;
total += (ChunkType::SEND, []).write_chunk_in(writer)?;
Ok(total)
}
}
Expand All @@ -366,7 +366,7 @@ impl SealedEntryExt for SolidEntry<Cow<'_, [u8]>> {

fn write_in<W: Write>(&self, writer: &mut W) -> io::Result<usize> {
let mut total = 0;
total += (ChunkType::SHED, self.header.to_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::SHED, self.header.to_bytes()).write_chunk_in(writer)?;
for extra_chunk in &self.extra {
total += extra_chunk.write_chunk_in(writer)?;
}
Expand All @@ -376,7 +376,7 @@ impl SealedEntryExt for SolidEntry<Cow<'_, [u8]>> {
for data in &self.data {
total += (ChunkType::SDAT, data.as_ref()).write_chunk_in(writer)?;
}
total += (ChunkType::SEND, [].as_slice()).write_chunk_in(writer)?;
total += (ChunkType::SEND, []).write_chunk_in(writer)?;
Ok(total)
}
}
Expand Down Expand Up @@ -752,24 +752,21 @@ impl SealedEntryExt for NormalEntry<Vec<u8>> {
}
}
if let Some(c) = created {
total +=
(ChunkType::cTIM, c.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::cTIM, c.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(d) = modified {
total +=
(ChunkType::mTIM, d.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::mTIM, d.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(a) = accessed {
total +=
(ChunkType::aTIM, a.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::aTIM, a.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(p) = permission {
total += (ChunkType::fPRM, p.to_bytes()).write_chunk_in(writer)?;
}
for xattr in &self.xattrs {
total += (ChunkType::xATR, xattr.to_bytes()).write_chunk_in(writer)?;
}
total += (ChunkType::FEND, [].as_slice()).write_chunk_in(writer)?;
total += (ChunkType::FEND, []).write_chunk_in(writer)?;
Ok(total)
}
}
Expand Down Expand Up @@ -863,24 +860,21 @@ impl SealedEntryExt for NormalEntry<&[u8]> {
}
}
if let Some(c) = created {
total +=
(ChunkType::cTIM, c.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::cTIM, c.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(d) = modified {
total +=
(ChunkType::mTIM, d.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::mTIM, d.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(a) = accessed {
total +=
(ChunkType::aTIM, a.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::aTIM, a.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(p) = permission {
total += (ChunkType::fPRM, p.to_bytes()).write_chunk_in(writer)?;
}
for xattr in &self.xattrs {
total += (ChunkType::xATR, xattr.to_bytes()).write_chunk_in(writer)?;
}
total += (ChunkType::FEND, [].as_slice()).write_chunk_in(writer)?;
total += (ChunkType::FEND, []).write_chunk_in(writer)?;
Ok(total)
}
}
Expand Down Expand Up @@ -974,24 +968,21 @@ impl SealedEntryExt for NormalEntry<Cow<'_, [u8]>> {
}
}
if let Some(c) = created {
total +=
(ChunkType::cTIM, c.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::cTIM, c.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(d) = modified {
total +=
(ChunkType::mTIM, d.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::mTIM, d.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(a) = accessed {
total +=
(ChunkType::aTIM, a.as_secs().to_be_bytes().as_slice()).write_chunk_in(writer)?;
total += (ChunkType::aTIM, a.as_secs().to_be_bytes()).write_chunk_in(writer)?;
}
if let Some(p) = permission {
total += (ChunkType::fPRM, p.to_bytes()).write_chunk_in(writer)?;
}
for xattr in &self.xattrs {
total += (ChunkType::xATR, xattr.to_bytes()).write_chunk_in(writer)?;
}
total += (ChunkType::FEND, [].as_slice()).write_chunk_in(writer)?;
total += (ChunkType::FEND, []).write_chunk_in(writer)?;
Ok(total)
}
}
Expand Down

0 comments on commit ac26e94

Please sign in to comment.