Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit ff05ce8

Browse files
committed
feat: impl TryFrom<AttributeTypeAndValue> for LocalName
1 parent b5af461 commit ff05ce8

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/types/federation_id.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,13 @@ mod test {
262262
oid: OID_RDN_UID,
263263
value: Any::encode_from(&directory_string).unwrap(),
264264
};
265-
// Ok so this works! that means, that we can convert a directorystring into a Rust string, because Rust strings are utf8
266-
// I think we should test this with the other types of DirectoryString, and then finish the
267-
// conversion
268265
assert!(FederationId::try_from(attribute_and_value).is_ok());
269266

270267
let directory_string = DirectoryString::Utf8String(String::from("input@in.put"));
271268
let attribute_and_value = AttributeTypeAndValue {
272269
oid: OID_RDN_UID,
273270
value: Any::encode_from(&directory_string).unwrap(),
274271
};
275-
// Ok so this works! that means, that we can convert a directorystring into a Rust string, because Rust strings are utf8
276-
// I think we should test this with the other types of DirectoryString, and then finish the
277-
// conversion
278272
assert!(FederationId::try_from(attribute_and_value).is_err())
279273
}
280274
}

src/types/local_name.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::str::FromStr;
22

3-
use crate::Constrained;
3+
use x509_cert::attr::AttributeTypeAndValue;
4+
45
use crate::errors::ConstraintError;
6+
use crate::{Constrained, OID_RDN_COMMON_NAME};
57

68
/// Rust-flavor regular expression describing valid format for the local name of a Federation ID.
79
pub const REGEX_LOCAL_NAME: &str = r#"\b([a-z0-9._%+-]+)$"#;
@@ -32,3 +34,46 @@ impl FromStr for LocalName {
3234
LocalName::new(s)
3335
}
3436
}
37+
38+
impl TryFrom<AttributeTypeAndValue> for LocalName {
39+
type Error = ConstraintError;
40+
41+
fn try_from(value: AttributeTypeAndValue) -> Result<Self, Self::Error> {
42+
if value.oid != OID_RDN_COMMON_NAME {
43+
return Err(crate::errors::InvalidInput::Malformed(format!(
44+
"This value has OID {}, which does not match OID {OID_RDN_COMMON_NAME}",
45+
value.oid
46+
))
47+
.into());
48+
}
49+
let attribute_value = value.value.value();
50+
let string = String::from_utf8_lossy(attribute_value);
51+
Self::new(&string)
52+
}
53+
}
54+
55+
#[cfg(test)]
56+
mod test {
57+
use der::Any;
58+
use x509_cert::ext::pkix::name::DirectoryString;
59+
60+
use super::*;
61+
62+
#[test]
63+
#[allow(clippy::unwrap_used)]
64+
fn from_attribute_type_and_value() {
65+
let directory_string = DirectoryString::Utf8String(String::from("input"));
66+
let attribute_and_value = AttributeTypeAndValue {
67+
oid: OID_RDN_COMMON_NAME,
68+
value: Any::encode_from(&directory_string).unwrap(),
69+
};
70+
assert!(LocalName::try_from(attribute_and_value).is_ok());
71+
72+
let directory_string = DirectoryString::Utf8String(String::from("input"));
73+
let attribute_and_value = AttributeTypeAndValue {
74+
oid: OID_RDN_COMMON_NAME,
75+
value: Any::encode_from(&directory_string).unwrap(),
76+
};
77+
assert!(LocalName::try_from(attribute_and_value).is_err())
78+
}
79+
}

0 commit comments

Comments
 (0)