diff --git a/oid4vc-core/src/subject_syntax_type.rs b/oid4vc-core/src/subject_syntax_type.rs index 50fc1950..20edb827 100644 --- a/oid4vc-core/src/subject_syntax_type.rs +++ b/oid4vc-core/src/subject_syntax_type.rs @@ -80,7 +80,7 @@ impl FromStr for DidMethod { type Err = serde_json::Error; fn from_str(s: &str) -> Result { - let mut did_scheme = s.splitn(3, ':'); + let mut did_scheme = s.splitn(4, ':'); match ( did_scheme.next(), @@ -88,6 +88,11 @@ impl FromStr for DidMethod { did_scheme.next(), did_scheme.next(), ) { + // (Some("did"), Some(method), Some(submethod), _) + // if !method.is_empty() && !submethod.is_empty() && method.chars().all(char::is_alphanumeric) => + // { + // Ok(DidMethod(format!("{}:{}", method, submethod))) + // } (Some("did"), Some(method), _, None) if !method.is_empty() && method.chars().all(char::is_alphanumeric) => { Ok(DidMethod(method.to_owned())) } @@ -106,6 +111,11 @@ impl Display for DidMethod { mod tests { use super::*; + #[test] + fn test() { + assert!(DidMethod::from_str("did:example:123").is_err()); + } + #[test] fn test_did_method() { assert!(DidMethod::from_str("").is_err()); @@ -114,6 +124,18 @@ mod tests { assert!(DidMethod::from_str("invalid:").is_err()); assert!(DidMethod::from_str("did:example_").is_err()); assert!(DidMethod::from_str("did:example").is_ok()); + assert!(DidMethod::from_str("did:example:submethod").is_ok()); + // assert!(DidMethod::from_str("did:example:submethod:0x123").is_ok()); + + assert_eq!(DidMethod::from_str("did:example").unwrap().to_string(), "did:example"); + assert_eq!( + DidMethod::from_str("did:example:submethod").unwrap().to_string(), + "did:example:submethod" + ); + assert_eq!( + DidMethod::from_str("did:example:submethod:0x123").unwrap().to_string(), + "did:example:submethod" + ); } #[test]