From 0e74c9ec6147fdc48b0066d23b4f56d64593164a Mon Sep 17 00:00:00 2001 From: Nathaniel McCallum Date: Sun, 20 Feb 2022 14:56:02 -0500 Subject: [PATCH] Add AlgorithmIdentifier::oids() helper function Signed-off-by: Nathaniel McCallum --- spki/src/algorithm.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spki/src/algorithm.rs b/spki/src/algorithm.rs index b138e5075..146fe6420 100644 --- a/spki/src/algorithm.rs +++ b/spki/src/algorithm.rs @@ -74,6 +74,26 @@ impl<'a> AlgorithmIdentifier<'a> { pub fn parameters_oid(&self) -> Result { Ok(ObjectIdentifier::try_from(self.parameters_any()?)?) } + + /// Convert to a pair of [`ObjectIdentifier`]s. + /// + /// This method is helpful for decomposing in match statements. Note in + /// particular that `NULL` parameters are treated the same as missing + /// parameters. + /// + /// Returns an error if parameters are present but not an OID. + pub fn oids(&self) -> der::Result<(ObjectIdentifier, Option)> { + Ok(( + self.oid, + match self.parameters { + None => None, + Some(p) => match p { + Any::NULL => None, + _ => Some(p.oid()?), + }, + }, + )) + } } impl<'a> Decodable<'a> for AlgorithmIdentifier<'a> {