Skip to content
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
2 changes: 2 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
allow-unwrap-in-consts = true
allow-unwrap-in-tests = true
40 changes: 40 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,43 @@ tls_codec_derive = { path = "./tls_codec/derive" }
x509-tsp = { path = "./x509-tsp" }
x509-cert = { path = "./x509-cert" }
x509-ocsp = { path = "./x509-ocsp" }

[workspace.lints.clippy]
borrow_as_ptr = "warn"
cast_lossless = "warn"
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_precision_loss = "warn"
cast_sign_loss = "warn"
checked_conversions = "warn"
doc_markdown = "warn"
from_iter_instead_of_collect = "warn"
implicit_saturating_sub = "warn"
manual_assert = "warn"
map_unwrap_or = "warn"
missing_errors_doc = "warn"
missing_panics_doc = "warn"
mod_module_files = "warn"
must_use_candidate = "warn"
needless_range_loop = "allow"
ptr_as_ptr = "warn"
redundant_closure_for_method_calls = "warn"
ref_as_ptr = "warn"
return_self_not_must_use = "warn"
semicolon_if_nothing_returned = "warn"
trivially_copy_pass_by_ref = "warn"
std_instead_of_alloc = "warn"
std_instead_of_core = "warn"
undocumented_unsafe_blocks = "warn"
unnecessary_safety_comment = "warn"
unwrap_in_result = "warn"
unwrap_used = "warn"

[workspace.lints.rust]
missing_copy_implementations = "warn"
missing_debug_implementations = "warn"
missing_docs = "warn"
trivial_casts = "warn"
trivial_numeric_casts = "warn"
unused_lifetimes = "warn"
unused_qualifications = "warn"
3 changes: 3 additions & 0 deletions der/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ oid = ["dep:const-oid"]
pem = ["dep:pem-rfc7468", "alloc", "zeroize"]
real = []

[lints]
workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
35 changes: 35 additions & 0 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ impl<'a> AnyRef<'a> {
};

/// Create a new [`AnyRef`] from the provided [`Tag`] and DER bytes.
///
/// # Errors
/// Returns [`Error`] with [`ErrorKind::Length`] if `bytes` is too long.
pub const fn new(tag: Tag, bytes: &'a [u8]) -> Result<Self, Error> {
match BytesRef::new(bytes) {
Ok(value) => Ok(Self { tag, value }),
Expand All @@ -50,16 +53,21 @@ impl<'a> AnyRef<'a> {
}

/// Get the raw value for this [`AnyRef`] type as a byte slice.
#[must_use]
pub fn value(self) -> &'a [u8] {
self.value.as_slice()
}

/// Returns [`Tag`] and [`Length`] of self.
#[must_use]
pub fn header(&self) -> Header {
Header::new(self.tag, self.value.len())
}

/// Attempt to decode this [`AnyRef`] type into the inner value.
///
/// # Errors
/// Returns `T::Error` if a decoding error occurred.
pub fn decode_as<T>(self) -> Result<T, <T as DecodeValue<'a>>::Error>
where
T: Choice<'a> + DecodeValue<'a>,
Expand All @@ -68,6 +76,9 @@ impl<'a> AnyRef<'a> {
}

/// Attempt to decode this [`AnyRef`] type into the inner value.
///
/// # Errors
/// Returns `T::Error` if a decoding error occurred.
pub fn decode_as_encoding<T>(
self,
encoding: EncodingRules,
Expand All @@ -86,12 +97,16 @@ impl<'a> AnyRef<'a> {
}

/// Is this value an ASN.1 `NULL` value?
#[must_use]
pub fn is_null(self) -> bool {
self == Self::NULL
}

/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
/// nested reader and calling the provided argument with it.
///
/// # Errors
/// Returns `E` in the event an error is returned from `F` or if a decoding error occurs.
pub fn sequence<F, T, E>(self, f: F) -> Result<T, E>
where
F: FnOnce(&mut SliceReader<'a>) -> Result<T, E>,
Expand Down Expand Up @@ -192,22 +207,30 @@ mod allocating {

impl Any {
/// Create a new [`Any`] from the provided [`Tag`] and DER bytes.
///
/// # Errors
/// If `bytes` is too long.
pub fn new(tag: Tag, bytes: impl Into<Box<[u8]>>) -> Result<Self, Error> {
let value = BytesOwned::new(bytes)?;
Ok(Self { tag, value })
}

/// Allow access to value
#[must_use]
pub fn value(&self) -> &[u8] {
self.value.as_slice()
}

/// Returns [`Tag`] and [`Length`] of self.
#[must_use]
pub fn header(&self) -> Header {
Header::new(self.tag, self.value.len())
}

/// Attempt to decode this [`Any`] type into the inner value.
///
/// # Errors
/// Returns `T::Error` if a decoding error occurred.
pub fn decode_as<'a, T>(&'a self) -> Result<T, <T as DecodeValue<'a>>::Error>
where
T: Choice<'a> + DecodeValue<'a>,
Expand All @@ -216,6 +239,9 @@ mod allocating {
}

/// Attempt to decode this [`Any`] type into the inner value with the given encoding rules.
///
/// # Errors
/// Returns `T::Error` if a decoding error occurred.
pub fn decode_as_encoding<'a, T>(
&'a self,
encoding: EncodingRules,
Expand All @@ -227,6 +253,9 @@ mod allocating {
}

/// Encode the provided type as an [`Any`] value.
///
/// # Errors
/// If an encoding error occurred.
pub fn encode_from<T>(msg: &T) -> Result<Self, Error>
where
T: Tagged + EncodeValue,
Expand All @@ -239,6 +268,9 @@ mod allocating {

/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
/// nested reader and calling the provided argument with it.
///
/// # Errors
/// If a decoding error occurred.
pub fn sequence<'a, F, T, E>(&'a self, f: F) -> Result<T, E>
where
F: FnOnce(&mut SliceReader<'a>) -> Result<T, E>,
Expand All @@ -248,6 +280,7 @@ mod allocating {
}

/// [`Any`] representation of the ASN.1 `NULL` type.
#[must_use]
pub fn null() -> Self {
Self {
tag: Tag::Null,
Expand All @@ -256,6 +289,7 @@ mod allocating {
}

/// Create a new [`AnyRef`] from the provided [`Any`] owned tag and bytes.
#[must_use]
pub fn to_ref(&self) -> AnyRef<'_> {
AnyRef {
tag: self.tag,
Expand Down Expand Up @@ -350,6 +384,7 @@ mod allocating {

impl Any {
/// Is this value an ASN.1 `NULL` value?
#[must_use]
pub fn is_null(&self) -> bool {
self.owned_to_ref() == AnyRef::NULL
}
Expand Down
Loading