easy_archive/traits.rs
1/// Traits for archive encoding and decoding operations
2use crate::{File, error::Result};
3
4/// Trait for decoding archives from bytes
5///
6/// Implementors of this trait can decode archive data into a list of files.
7///
8/// This trait is only available when the `decode` feature is enabled.
9#[cfg(feature = "decode")]
10pub trait Decode {
11 /// Decode an archive from a byte buffer
12 ///
13 /// # Arguments
14 /// * `buffer` - The archive data (can be any type that converts to &[u8])
15 ///
16 /// # Returns
17 /// * `Ok(Vec<File>)` - The extracted files on success
18 /// * `Err(ArchiveError)` - If decoding fails
19 ///
20 /// # Example
21 /// ```no_run
22 /// use easy_archive::{Decode, archive::tar::Tar};
23 ///
24 /// let data = std::fs::read("archive.tar")?;
25 /// let files = Tar::decode(data)?;
26 /// # Ok::<(), Box<dyn std::error::Error>>(())
27 /// ```
28 fn decode<T: AsRef<[u8]>>(buffer: T) -> Result<Vec<File>>;
29}
30
31/// Trait for encoding files into archives
32///
33/// Implementors of this trait can encode a list of files into archive format.
34///
35/// This trait is only available when the `encode` feature is enabled.
36#[cfg(feature = "encode")]
37pub trait Encode {
38 /// Encode files into an archive
39 ///
40 /// # Arguments
41 /// * `files` - The list of files to include in the archive
42 ///
43 /// # Returns
44 /// * `Ok(Vec<u8>)` - The encoded archive data on success
45 /// * `Err(ArchiveError)` - If encoding fails or duplicate files are detected
46 ///
47 /// # Example
48 /// ```no_run
49 /// use easy_archive::{Encode, File, archive::tar::Tar};
50 ///
51 /// let files = vec![
52 /// File {
53 /// path: "hello.txt".to_string(),
54 /// buffer: b"Hello, world!".to_vec(),
55 /// ..Default::default()
56 /// }
57 /// ];
58 /// let archive = Tar::encode(files)?;
59 /// # Ok::<(), Box<dyn std::error::Error>>(())
60 /// ```
61 fn encode(files: Vec<File>) -> Result<Vec<u8>>;
62}
63
64/// Combined trait for types that support both encoding and decoding
65///
66/// This is a marker trait that indicates a type can both encode and decode archives.
67/// Only available when both `encode` and `decode` features are enabled.
68#[cfg(all(feature = "encode", feature = "decode"))]
69pub trait Archive: Encode + Decode {}