summaryrefslogtreecommitdiff
path: root/src/codec.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2024-06-02 18:38:25 -0400
committerLibravatar Silas Bartha <[email protected]>2024-06-02 18:38:25 -0400
commited31c4201834719b2b75dca7cad1637abf1a80bb (patch)
tree3650833ed6d8c458bef9efff18f3585f4ccf17d9 /src/codec.rs
parentccb19be9d0e918070029f31a86c7eb546121bd87 (diff)
Made codecs object-safe and deal with bytes only
Diffstat (limited to 'src/codec.rs')
-rw-r--r--src/codec.rs40
1 files changed, 21 insertions, 19 deletions
diff --git a/src/codec.rs b/src/codec.rs
index 8b38a9b..11fa76e 100644
--- a/src/codec.rs
+++ b/src/codec.rs
@@ -1,26 +1,28 @@
+use thiserror::Error;
+
/// Codecs enable the concealment of payload data inside the data of a carrier.
pub trait Codec {
- /// Data type representing the carrier.
- type Carrier;
-
- /// Data type representing the payload.
- type Payload;
-
- /// Data type representing encoder output/decoder input (usually the same as the carrier).
- type Output;
-
- /// Type of errors produced by this codec.
- type Error;
-
/// Embeds payload data inside carrier, returning the result.
- fn encode<C, P>(&self, carrier: C, payload: P) -> Result<Self::Output, Self::Error>
- where
- C: Into<Self::Carrier>,
- P: Into<Self::Payload>;
+ fn encode(&self, carrier: &[u8], payload: &[u8]) -> Result<Vec<u8>, CodecError>;
/// Extracts payload data from an encoded carrier, returning the carrier with data removed and the
/// payload data.
- fn decode<E>(&self, encoded: E) -> Result<(Self::Carrier, Self::Payload), Self::Error>
- where
- E: Into<Self::Output>;
+ fn decode(&self, encoded: &[u8]) -> Result<(Vec<u8>, Vec<u8>), CodecError>;
+}
+
+/// Errors produced by a codec
+#[derive(Debug, Error)]
+pub enum CodecError {
+ /// Variant used when data is determined not to be encoded. Note that a codec may have no way
+ /// of knowing this, so this may not be returned even if the data was not encoded
+ #[error("Data was not encoded with this codec")]
+ DataNotEncoded,
+
+ /// Variant used when data is invalid in some way. Allows a message string for further context
+ #[error("Provided data invalid: {0}")]
+ DataInvalid(String),
+
+ /// Variant used when some dependency, such as a file load, fails
+ #[error("Error occured in dependency: {0}")]
+ DependencyError(String),
}