Embrace split_first.

This commit is contained in:
2017-12-21 17:01:57 -08:00
parent 89c657854e
commit 97990f2ebe

View File

@@ -401,12 +401,12 @@ fn get_signature_alg(x: &ASN1Block)
fn get_version(bs: &[ASN1Block]) fn get_version(bs: &[ASN1Block])
-> Result<(u32, &[ASN1Block]),X509ParseError> -> Result<(u32, &[ASN1Block]),X509ParseError>
{ {
match bs.first() { match bs.split_first() {
Some(&ASN1Block::Integer(_, ref v)) => { Some((&ASN1Block::Integer(_, ref v), rest)) => {
match v.to_u8() { match v.to_u8() {
Some(0) => Ok((1, &bs[1..])), Some(0) => Ok((1, rest)),
Some(1) => Ok((2, &bs[1..])), Some(1) => Ok((2, rest)),
Some(2) => Ok((3, &bs[1..])), Some(2) => Ok((3, rest)),
_ => Ok((1, &bs)) _ => Ok((1, &bs))
} }
} }
@@ -418,10 +418,10 @@ fn get_version(bs: &[ASN1Block])
fn get_serial_number(bs: &[ASN1Block]) fn get_serial_number(bs: &[ASN1Block])
-> Result<(BigUint, &[ASN1Block]),X509ParseError> -> Result<(BigUint, &[ASN1Block]),X509ParseError>
{ {
match bs.first() { match bs.split_first() {
Some(&ASN1Block::Integer(_, ref v)) => { Some((&ASN1Block::Integer(_, ref v), rest)) => {
match v.to_biguint() { match v.to_biguint() {
Some(sn) => Ok((sn, &bs[1..])), Some(sn) => Ok((sn, rest)),
_ => Err(X509ParseError::NoSerialNumber) _ => Err(X509ParseError::NoSerialNumber)
} }
} }
@@ -433,10 +433,10 @@ fn get_serial_number(bs: &[ASN1Block])
fn get_signature_info(bs: &[ASN1Block]) fn get_signature_info(bs: &[ASN1Block])
-> Result<(SignatureAlgorithm, &[ASN1Block]),X509ParseError> -> Result<(SignatureAlgorithm, &[ASN1Block]),X509ParseError>
{ {
match bs.first() { match bs.split_first() {
Some(x) => { Some((x, rest)) => {
let alg = get_signature_alg(&x)?; let alg = get_signature_alg(&x)?;
Ok((alg, &bs[1..])) Ok((alg, rest))
} }
_ => _ =>
Err(X509ParseError::NoSignatureAlgorithm) Err(X509ParseError::NoSignatureAlgorithm)
@@ -489,8 +489,8 @@ fn empty_block() -> InfoBlock {
fn get_name_data(bs: &[ASN1Block]) fn get_name_data(bs: &[ASN1Block])
-> Result<(InfoBlock,&[ASN1Block]),X509ParseError> -> Result<(InfoBlock,&[ASN1Block]),X509ParseError>
{ {
match bs.first() { match bs.split_first() {
Some(x) => { Some((x,rest)) => {
match x { match x {
// Name ::= CHOICE { -- only one possibility for now -- // Name ::= CHOICE { -- only one possibility for now --
// rdnSequence RDNSequence } // rdnSequence RDNSequence }
@@ -512,7 +512,7 @@ fn get_name_data(bs: &[ASN1Block])
return Err(X509ParseError::IllFormedNameInformation) return Err(X509ParseError::IllFormedNameInformation)
} }
} }
Ok((iblock, &bs[1..])) Ok((iblock, rest))
} }
_ => _ =>
Err(X509ParseError::NoNameInformation) Err(X509ParseError::NoNameInformation)
@@ -717,17 +717,17 @@ struct Validity {
fn get_validity_data(bs: &[ASN1Block]) fn get_validity_data(bs: &[ASN1Block])
-> Result<(Validity,&[ASN1Block]),X509ParseError> -> Result<(Validity,&[ASN1Block]),X509ParseError>
{ {
match bs.first() { match bs.split_first() {
// Validity ::= SEQUENCE { // Validity ::= SEQUENCE {
// notBefore Time, // notBefore Time,
// notAfter Time } // notAfter Time }
Some(&ASN1Block::Sequence(_, ref valxs)) => { Some((&ASN1Block::Sequence(_, ref valxs), rest)) => {
if valxs.len() != 2 { if valxs.len() != 2 {
return Err(X509ParseError::ImproperValidityInfo); return Err(X509ParseError::ImproperValidityInfo);
} }
let nb = get_time(&valxs[0])?; let nb = get_time(&valxs[0])?;
let na = get_time(&valxs[1])?; let na = get_time(&valxs[1])?;
Ok((Validity{ notBefore: nb, notAfter: na }, &bs[1..])) Ok((Validity{ notBefore: nb, notAfter: na }, rest))
} }
_ => _ =>
Err(X509ParseError::NoValidityInfo) Err(X509ParseError::NoValidityInfo)