use error::ParserResult;
use nom::{
bytes::complete::tag,
character::complete::i128,
combinator::{map, opt},
Parser,
};
use crate::intermediate::{ASN1Type, ASN1Value, INTEGER};
use super::{constraint::*, *};
pub fn integer_value(input: Input<'_>) -> ParserResult<'_, ASN1Value> {
map(skip_ws_and_comments(i128), ASN1Value::Integer).parse(input)
}
pub fn integer(input: Input<'_>) -> ParserResult<'_, ASN1Type> {
map(
(
into_inner(skip_ws_and_comments(tag(INTEGER))),
opt(skip_ws_and_comments(distinguished_values)),
opt(skip_ws_and_comments(constraints)),
),
|m| ASN1Type::Integer(m.into()),
)
.parse(input)
}
#[cfg(test)]
mod tests {
use crate::intermediate::{constraints::*, types::*, *};
use super::*;
#[test]
fn parses_integer() {
assert_eq!(
integer("INTEGER".into()).unwrap().1,
ASN1Type::Integer(Integer::default())
);
assert_eq!(
integer("INTEGER (-9..-4, ...)".into()).unwrap().1,
ASN1Type::Integer(Integer {
constraints: vec![Constraint::Subtype(ElementSetSpecs {
set: ElementOrSetOperation::Element(SubtypeElements::ValueRange {
min: Some(ASN1Value::Integer(-9)),
max: Some(ASN1Value::Integer(-4)),
extensible: true
}),
extensible: false
})],
distinguished_values: None,
})
);
assert_eq!(
integer("\r\nINTEGER(-9..-4)".into()).unwrap().1,
ASN1Type::Integer(Integer {
constraints: vec![Constraint::Subtype(ElementSetSpecs {
set: ElementOrSetOperation::Element(SubtypeElements::ValueRange {
min: Some(ASN1Value::Integer(-9)),
max: Some(ASN1Value::Integer(-4)),
extensible: false
}),
extensible: false
})],
distinguished_values: None,
})
);
}
}