Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Apr 30, 2023
1 parent 09decf7 commit b5d967d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion benches/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Bench {
#[derive(AsnType, Decode, Encode)]
pub struct EmptySequence {}

#[derive(AsnType, Clone, Copy, Decode, Encode, PartialEq)]
#[derive(AsnType, Clone, Copy, Decode, Debug, Encode, PartialEq)]
#[rasn(enumerated)]
pub enum BenchEnum {
A,
Expand Down
2 changes: 1 addition & 1 deletion macros/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Enum {
let enumerated_impl = self.config.enumerated.then(|| {
let (variants, extended_variants): (Vec<_>, Vec<_>) = self.variants.iter()
.map(|variant| VariantConfig::new(variant, &self.generics, &self.config))
.partition(|config| config.extension_addition);
.partition(|config| !config.extension_addition);

let discriminants = variants.iter().enumerate().map(|(i, config)| {
let discriminant = config.discriminant().unwrap_or(i) as isize;
Expand Down
8 changes: 4 additions & 4 deletions src/per.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ mod tests {
#[rasn(enumerated, crate_root = "crate")]
enum Enum1 { Green, Red, Blue, }

// round_trip!(uper, Enum1, Enum1::Green, &[0]);
// round_trip!(uper, Enum1, Enum1::Red, &[0x40]);
// round_trip!(uper, Enum1, Enum1::Blue, &[0x80]);
round_trip!(uper, Enum1, Enum1::Green, &[0]);
round_trip!(uper, Enum1, Enum1::Red, &[0x40]);
round_trip!(uper, Enum1, Enum1::Blue, &[0x80]);

#[derive(AsnType, Clone, Copy, Debug, Decode, Encode, PartialEq)]
#[rasn(enumerated, crate_root = "crate")]
Expand All @@ -128,7 +128,7 @@ mod tests {
Purple,
}

// round_trip!(uper, Enum2, Enum2::Red, &[0]);
round_trip!(uper, Enum2, Enum2::Red, &[0]);
round_trip!(uper, Enum2, Enum2::Yellow, &[0x80]);
round_trip!(uper, Enum2, Enum2::Purple, &[0x81]);
}
Expand Down
25 changes: 19 additions & 6 deletions src/per/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ impl<'input> Decoder<'input> {
self.parse_integer(Constraints::new(&[constraints]))
}

fn parse_non_negative_binary_integer(&mut self, range: i128) -> Result<types::Integer> {
let bits = super::log2(range);
let (input, data) = nom::bytes::streaming::take(bits)(self.input)?;
self.input = input;
let data = if data.len() < 8 {
let mut buffer = types::BitString::repeat(false, 8-data.len());
buffer.extend_from_bitslice(&data);
buffer
} else {
data.to_bitvec()
};

Ok(num_bigint::BigUint::from_bytes_le(&dbg!(data).into_vec()).into())
}

fn parse_integer(&mut self, constraints: Constraints) -> Result<types::Integer> {
let extensible = self.parse_extensible_bit(&constraints)?;
let value_constraint = constraints.value();
Expand All @@ -237,11 +252,9 @@ impl<'input> Decoder<'input> {
{
if range == 0 {
return Ok(value_constraint.constraint.minimum().into())
} else {
self.parse_non_negative_binary_integer(range)?
}
let bits = super::log2(range);
let (input, data) = nom::bytes::streaming::take(bits)(self.input)?;
self.input = input;
num_bigint::BigUint::from_bytes_be(&data.to_bitvec().into_vec()).into()
} else {
let bytes = self.decode_octets()?.into_vec();
value_constraint
Expand Down Expand Up @@ -277,12 +290,12 @@ impl<'input> crate::Decoder for Decoder<'input> {
fn decode_enumerated<E: Enumerated>(&mut self, _: Tag) -> Result<E> {
let extensible = E::EXTENDED_VARIANTS.is_some().then(|| self.parse_one_bit()).transpose()?.unwrap_or_default();

if extensible {
if dbg!(extensible) {
let index: usize = self.parse_normally_small_integer()?.try_into().map_err(Error::custom)?;
E::from_extended_enumeration_index(index)
.ok_or_else(|| Error::custom(format!("Extended index {} not found", index)))
} else {
let index = self.parse_non_negative_binary_integer(E::variance() as i128)?.try_into().map_err(Error::custom)?;
let index = dbg!(self.parse_non_negative_binary_integer(E::variance() as i128)?).try_into().map_err(Error::custom)?;
E::from_enumeration_index(index)
.ok_or_else(|| Error::custom(format!("Index {} not found", index)))
}
Expand Down
4 changes: 2 additions & 2 deletions src/per/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ impl crate::Encoder for Encoder {
) -> Result<Self::Ok, Self::Error> {
let mut buffer = BitString::default();
let index = value.enumeration_index();
if E::EXTENDED_VARIANTS.is_some() {
buffer.push(value.is_extended_variant());
if dbg!(E::EXTENDED_VARIANTS.is_some()) {
buffer.push(dbg!(value.is_extended_variant()));
}

if value.is_extended_variant() {
Expand Down

0 comments on commit b5d967d

Please sign in to comment.