Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from deku to cookie-factory and nom #10

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Add std feature, add error handling
  • Loading branch information
aumetra committed Jun 6, 2022
commit 9c4e910fa4757b20b3d0a4721bb48008f62b2cb3
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ flate2 = { version = "1.0.24", optional = true }
nom = { version = "7.1.1", default-features = false }

[features]
compression = ["flate2", "std"]
std = ["cookie-factory/std", "nom/std"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Deserialisation is built on top of [nom](https://docs.rs/nom) and serialisation

[Format specification](https://geti2p.net/spec/updates#su3-file-specification)

## Features

- `compress`: Helper function to decompress the content when appropriate

## License

This crate is licensed under the [MIT license](https://opensource.org/licenses/MIT)
Expand Down
6 changes: 3 additions & 3 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ pub fn deserialise(data: &[u8]) -> IResult<&[u8], Su3<'_>> {
))(rest)?;

let su3 = Su3 {
signature_type: signature_type.try_into().unwrap(),
file_type: file_type.try_into().unwrap(),
content_type: content_type.try_into().unwrap(),
signature_type: signature_type.try_into()?,
file_type: file_type.try_into()?,
content_type: content_type.try_into()?,
raw_version,
raw_signer_id,
raw_content,
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! ```
//!

#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(missing_docs, rust_2018_idioms, unsafe_code)]
#![warn(clippy::all, clippy::pedantic)]

Expand Down Expand Up @@ -183,8 +183,8 @@ impl<'a> Su3<'a> {
/// # Errors
///
/// Returns an IO error in case the decompression of the GZ compressed content fails
#[cfg_attr(docsrs, doc(cfg(feature = "flate2")))]
#[cfg(feature = "flate2")]
#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
#[cfg(feature = "compression")]
pub fn content(&self) -> std::io::Result<std::borrow::Cow<'a, [u8]>> {
use flate2::read::GzDecoder;

Expand Down
6 changes: 4 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ macro_rules! try_from_number {
}

impl ::core::convert::TryFrom<$num_type> for $enum_name {
type Error = ();
type Error = ::nom::Err<::nom::error::Error<&'static [u8]>>;

fn try_from(num: $num_type) -> Result<Self, Self::Error> {
use ::nom::{Err, error::{Error, ErrorKind}};

match num {
$(
$value => Ok(Self::$variant),
)*
_ => Err(())
_ => Err(Err::Failure(Error::new(&[], ErrorKind::Digit)))
}
}
}
Expand Down