diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/archive.rs | 38 | ||||
| -rw-r--r-- | src/io.rs | 21 | ||||
| -rw-r--r-- | src/lib.rs | 6 | ||||
| -rw-r--r-- | src/result.rs | 32 | ||||
| -rw-r--r-- | src/zip.rs | 28 |
5 files changed, 125 insertions, 0 deletions
diff --git a/src/archive.rs b/src/archive.rs new file mode 100644 index 0000000..a1fe344 --- /dev/null +++ b/src/archive.rs | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | use crate::result::ArchiveResult; | ||
| 2 | use std::{fs::File, path::Path}; | ||
| 3 | |||
| 4 | use crate::io::{ArchiveRead, ArchiveWrite}; | ||
| 5 | |||
| 6 | pub struct Archive<IO> { | ||
| 7 | io: IO, | ||
| 8 | } | ||
| 9 | |||
| 10 | impl<IO: ArchiveRead> Archive<IO> { | ||
| 11 | pub fn open(path: impl AsRef<Path>) -> ArchiveResult<Self> | ||
| 12 | where | ||
| 13 | IO: ArchiveRead<Reader = File>, | ||
| 14 | { | ||
| 15 | Self::reader(File::open(path)?) | ||
| 16 | } | ||
| 17 | |||
| 18 | pub fn reader(reader: IO::Reader) -> ArchiveResult<Self> { | ||
| 19 | Ok(Self { | ||
| 20 | io: IO::new(reader)?, | ||
| 21 | }) | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | impl<IO: ArchiveWrite> Archive<IO> { | ||
| 26 | pub fn create(path: impl AsRef<Path>) -> ArchiveResult<Self> | ||
| 27 | where | ||
| 28 | IO: ArchiveWrite<Writer = File>, | ||
| 29 | { | ||
| 30 | Self::writer(File::create(path)?) | ||
| 31 | } | ||
| 32 | |||
| 33 | pub fn writer(writer: IO::Writer) -> ArchiveResult<Self> { | ||
| 34 | Ok(Self { | ||
| 35 | io: IO::new(writer)?, | ||
| 36 | }) | ||
| 37 | } | ||
| 38 | } | ||
diff --git a/src/io.rs b/src/io.rs new file mode 100644 index 0000000..cea998f --- /dev/null +++ b/src/io.rs | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | use std::io::{Read, Seek, Write}; | ||
| 2 | |||
| 3 | use crate::result::ArchiveResult; | ||
| 4 | |||
| 5 | pub trait ArchiveRead | ||
| 6 | where | ||
| 7 | Self: Sized, | ||
| 8 | { | ||
| 9 | type Reader: Read + Seek; | ||
| 10 | |||
| 11 | fn new(reader: Self::Reader) -> ArchiveResult<Self>; | ||
| 12 | } | ||
| 13 | |||
| 14 | pub trait ArchiveWrite | ||
| 15 | where | ||
| 16 | Self: Sized, | ||
| 17 | { | ||
| 18 | type Writer: Write; | ||
| 19 | |||
| 20 | fn new(write: Self::Writer) -> ArchiveResult<Self>; | ||
| 21 | } | ||
| @@ -1 +1,7 @@ | |||
| 1 | pub mod zip; | ||
| 1 | 2 | ||
| 3 | mod archive; | ||
| 4 | mod io; | ||
| 5 | mod result; | ||
| 6 | |||
| 7 | pub use archive::Archive; | ||
diff --git a/src/result.rs b/src/result.rs new file mode 100644 index 0000000..47302ae --- /dev/null +++ b/src/result.rs | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | use std::error::Error; | ||
| 2 | use std::fmt::{Debug, Display}; | ||
| 3 | use std::io; | ||
| 4 | |||
| 5 | pub type ArchiveResult<R> = Result<R, ArchiveError>; | ||
| 6 | |||
| 7 | #[derive(Debug)] | ||
| 8 | pub enum ArchiveError { | ||
| 9 | IO(io::Error), | ||
| 10 | } | ||
| 11 | |||
| 12 | impl From<io::Error> for ArchiveError { | ||
| 13 | fn from(value: io::Error) -> Self { | ||
| 14 | Self::IO(value) | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | impl Display for ArchiveError { | ||
| 19 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 20 | match self { | ||
| 21 | Self::IO(err) => write!(f, "{err}"), | ||
| 22 | } | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | impl Error for ArchiveError { | ||
| 27 | fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
| 28 | match self { | ||
| 29 | Self::IO(source) => Some(source), | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
diff --git a/src/zip.rs b/src/zip.rs new file mode 100644 index 0000000..a5d0fc5 --- /dev/null +++ b/src/zip.rs | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | use crate::io::{ArchiveRead, ArchiveWrite}; | ||
| 2 | use crate::result::ArchiveResult; | ||
| 3 | use std::io::{Seek, Write}; | ||
| 4 | use std::{fs::File, io::Read}; | ||
| 5 | |||
| 6 | pub struct ZipReader<R: Read + Seek = File> { | ||
| 7 | reader: R, | ||
| 8 | } | ||
| 9 | |||
| 10 | impl<R: Read + Seek> ArchiveRead for ZipReader<R> { | ||
| 11 | type Reader = R; | ||
| 12 | |||
| 13 | fn new(reader: Self::Reader) -> ArchiveResult<Self> { | ||
| 14 | Ok(Self { reader }) | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | pub struct ZipWriter<W: Write = File> { | ||
| 19 | writer: W, | ||
| 20 | } | ||
| 21 | |||
| 22 | impl<W: Write> ArchiveWrite for ZipWriter<W> { | ||
| 23 | type Writer = W; | ||
| 24 | |||
| 25 | fn new(writer: Self::Writer) -> ArchiveResult<Self> { | ||
| 26 | Ok(Self { writer }) | ||
| 27 | } | ||
| 28 | } | ||
