Skip to content

Commit

Permalink
Remove transitional traits to derive BerParser and DerParser from Try…
Browse files Browse the repository at this point in the history
…From
  • Loading branch information
chifflier committed Feb 28, 2025
1 parent 487898a commit 2eb3200
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 79 deletions.
50 changes: 1 addition & 49 deletions src/from_ber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nom::Input as _;
use nom::{Err, IResult};

use crate::ber::{GetObjectContent, MAX_RECURSION};
use crate::{Any, BerError, BerMode, Error, Header, Input, ParseResult, Tag, Tagged};
use crate::{Any, BerError, BerMode, Error, Header, Input, ParseResult, Tag};

/// Base trait for BER object parsers
///
Expand Down Expand Up @@ -121,54 +121,6 @@ where
}
}

// NOTE: remove this when MSRV >= 1.84?
/// Trait to automatically derive BerParser
///
/// This trait is only a marker to control if a BER parser should be automatically derived. It is empty.
///
/// # Note
/// We cannot derive BerParser automatically from `T: TryFrom<Any>`, because we end up in a
/// conflicting implementation of trait:
/// <pre>
/// error[E0119]: conflicting implementations of trait `from_ber::BerParser<'_>` for type `asn1_types::any::Any<'_>`
/// --> src/from_ber.rs:125:1
/// |
/// 125 | impl<'i, E, T> BerParser<'i> for T
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `asn1_types::any::Any<'_>`
/// |
/// ::: src/asn1_types/any.rs:440:1
/// |
/// 440 | impl<'i> BerParser<'i> for Any<'i> {
/// | ---------------------------------- first implementation here
/// </pre>
/// This seems to be solved from rustc >= 1.84, thanks to the new trait solver
///
/// since our MSRV is < 1.84, we add a workaround.
pub trait DeriveBerParserFromTryFrom {}

impl<'i, E, T> BerParser<'i> for T
where
T: Tagged,
T: DeriveBerParserFromTryFrom,
T: TryFrom<Any<'i>, Error = E>,
E: ParseError<Input<'i>> + From<BerError<Input<'i>>>,
{
type Error = E;

fn check_tag(tag: Tag) -> bool {
tag == Self::TAG
}

fn from_any_ber(input: Input<'i>, header: Header<'i>) -> IResult<Input<'i>, Self, Self::Error> {
let (rem, data) =
BerMode::get_object_content(input, &header, MAX_RECURSION).map_err(Err::convert)?;

let any = Any::new(header, data);
let obj = T::try_from(any).map_err(|e| Err::Error(e))?;
Ok((rem, obj))
}
}

// NOTE: function useful during transition to Input. Remove this after
pub(crate) fn wrap_ber_parser<'i, F, T>(mut f: F) -> impl FnMut(&'i [u8]) -> ParseResult<'i, T>
where
Expand Down
31 changes: 1 addition & 30 deletions src/from_der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use nom::bytes::streaming::take;
use nom::error::ParseError;
use nom::{Err, IResult, Input as _};

use crate::ber::{GetObjectContent, MAX_RECURSION};
use crate::debug::{trace, trace_generic};
use crate::{
parse_der_any, wrap_ber_parser, Any, BerError, DerMode, Error, Header, Input, ParseResult,
Result, Tag, Tagged,
parse_der_any, wrap_ber_parser, Any, BerError, Error, Header, Input, ParseResult, Result, Tag,
};

/// Base trait for DER object parsers
Expand Down Expand Up @@ -193,30 +191,3 @@ where
Ok((rem, Some(obj)))
}
}

/// See [`crate::DeriveBerParserFromTryFrom`]
pub trait DeriveDerParserFromTryFrom {}

impl<'i, E, T> DerParser<'i> for T
where
T: Tagged,
T: CheckDerConstraints,
T: DeriveDerParserFromTryFrom,
T: TryFrom<Any<'i>, Error = E>,
E: ParseError<Input<'i>> + From<BerError<Input<'i>>>,
{
type Error = E;

fn check_tag(tag: Tag) -> bool {
tag == Self::TAG
}

fn from_any_der(input: Input<'i>, header: Header<'i>) -> IResult<Input<'i>, Self, Self::Error> {
let (rem, data) =
DerMode::get_object_content(input, &header, MAX_RECURSION).map_err(Err::convert)?;

let any = Any::new(header, data);
let obj = T::try_from(any).map_err(|e| Err::Error(e))?;
Ok((rem, obj))
}
}

0 comments on commit 2eb3200

Please sign in to comment.