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

Make backtraces as feature, enabled by default #214

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ chrono = { version = "0.4.27", default-features = false, features = ["alloc"] }
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }

[features]
default = ["macros"]
default = ["macros", "backtraces"]
macros = ["rasn-derive"]
backtraces = []

[[bench]]
name = "criterion"
Expand Down
11 changes: 10 additions & 1 deletion src/error/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use super::strings::PermittedAlphabetError;
use alloc::{boxed::Box, string::ToString};

use jzon::JsonValue;
use snafu::{Backtrace, GenerateImplicitData, Snafu};
use snafu::Snafu;
#[cfg(feature = "backtraces")]
use snafu::{Backtrace, GenerateImplicitData};

use crate::de::Error;
use crate::types::variants::Variants;
Expand Down Expand Up @@ -68,6 +70,7 @@ impl From<CodecDecodeError> for DecodeError {
///
/// fn main() {
/// // Hello, World! in decimal bytes with trailing zeros
/// // Below sample requires that `backtraces` feature is enabled
/// let hello_data = vec![
/// 13, 145, 151, 102, 205, 235, 16, 119, 223, 203, 102, 68, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/// 0,
Expand Down Expand Up @@ -100,12 +103,14 @@ impl From<CodecDecodeError> for DecodeError {
///
/// }
/// _ => {
/// #[cfg(feature = "backtraces")]
/// println!("Backtrace:\n{:?}", e.backtrace);
/// panic!("Unexpected error! {e:?}");
/// }
/// }
/// }
/// k => {
/// #[cfg(feature = "backtraces")]
/// println!("Backtrace:\n{:?}", e.backtrace);
/// panic!("Unexpected error! {k:?}");
/// }
Expand All @@ -127,12 +132,14 @@ impl From<CodecDecodeError> for DecodeError {
pub struct DecodeError {
pub kind: Box<DecodeErrorKind>,
pub codec: Codec,
#[cfg(feature = "backtraces")]
pub backtrace: Backtrace,
}
impl core::fmt::Display for DecodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
writeln!(f, "Error Kind: {}", self.kind)?;
writeln!(f, "Codec: {}", self.kind)?;
#[cfg(feature = "backtraces")]
write!(f, "\nBacktrace:\n{}", self.backtrace)?;
Ok(())
}
Expand Down Expand Up @@ -284,6 +291,7 @@ impl DecodeError {
Self {
kind: Box::new(kind),
codec,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::generate(),
}
}
Expand All @@ -300,6 +308,7 @@ impl DecodeError {
Self {
kind: Box::new(DecodeErrorKind::CodecSpecific { inner }),
codec,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::generate(),
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/error/encode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::types::constraints::{Bounded, Size};
use snafu::{Backtrace, GenerateImplicitData, Snafu};
use snafu::Snafu;
#[cfg(feature = "backtraces")]
use snafu::{Backtrace, GenerateImplicitData};

use alloc::{boxed::Box, string::ToString};

Expand Down Expand Up @@ -62,6 +64,7 @@ impl From<CodecEncodeError> for EncodeError {
/// );
///
/// fn main() {
/// // Below sample requires that `backtraces` feature is enabled
///
/// let constrained_str = MyConstrainedString(VisibleString::try_from("abcD").unwrap());
/// let encoded = Codec::Uper.encode_to_binary(&constrained_str);
Expand All @@ -79,6 +82,7 @@ impl From<CodecEncodeError> for EncodeError {
/// println!("Codec: {}", e.codec);
/// println!("Character {} not found from the permitted list.",
/// char::from_u32(character).unwrap());
/// #[cfg(feature = "backtraces")]
/// println!("Backtrace:\n{}", e.backtrace);
/// }
/// _ => {
Expand All @@ -101,12 +105,14 @@ impl From<CodecEncodeError> for EncodeError {
pub struct EncodeError {
pub kind: Box<EncodeErrorKind>,
pub codec: crate::Codec,
#[cfg(feature = "backtraces")]
pub backtrace: Backtrace,
}
impl core::fmt::Display for EncodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
writeln!(f, "Error Kind: {}", self.kind)?;
writeln!(f, "Codec: {}", self.kind)?;
#[cfg(feature = "backtraces")]
write!(f, "\nBacktrace:\n{}", self.backtrace)?;

Ok(())
Expand All @@ -121,6 +127,7 @@ impl EncodeError {
Self {
kind: Box::new(EncodeErrorKind::AlphabetConstraintNotSatisfied { reason }),
codec,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::generate(),
}
}
Expand All @@ -131,6 +138,7 @@ impl EncodeError {
expected: (**expected),
}),
codec,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::generate(),
})
}
Expand All @@ -152,6 +160,7 @@ impl EncodeError {
Self {
kind: Box::new(kind),
codec,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::generate(),
}
}
Expand All @@ -168,6 +177,7 @@ impl EncodeError {
Self {
kind: Box::new(EncodeErrorKind::CodecSpecific { inner }),
codec,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::generate(),
}
}
Expand Down Expand Up @@ -283,6 +293,7 @@ impl crate::enc::Error for EncodeError {
msg: msg.to_string(),
}),
codec,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::generate(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Error module includes all encode and decode errors among all codecs.
//! Encoding can result to `EncodeError` and decoding can result to `DecodeError`.
//! Backtraces are enabled by default with `backtraces` feature.
//! See submodules for other error types.
#![allow(clippy::module_name_repetitions)]
mod decode;
Expand Down
Loading