1
0

feat(wfassoc): enhance CLSID handling and parsing

- Add `getrandom` dependency for UUID v4 feature
- Enable UUID v4 feature in `Cargo.toml`
- Implement `Clsid::with_random()` for generating random CLSIDs
- Improve `Clsid::new()` to accept `&Uuid` directly
- Update `Clsid::from_str()` to use `uuid::fmt::Braced` for stricter parsing
- Add comprehensive tests for valid and invalid CLSID strings
- Fix minor typo in test assertion code
- Enhance documentation comments for CLSID struct and error type
This commit is contained in:
2025-10-28 12:50:15 +08:00
parent 1342799303
commit 4d679588f8
4 changed files with 30 additions and 11 deletions

1
Cargo.lock generated
View File

@ -674,6 +674,7 @@ version = "1.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f87b8aa10b915a06587d0dec516c282ff295b475d94abf425d62b57710070a2" checksum = "2f87b8aa10b915a06587d0dec516c282ff295b475d94abf425d62b57710070a2"
dependencies = [ dependencies = [
"getrandom",
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
] ]

View File

@ -22,4 +22,4 @@ widestring = "1.2.1"
indexmap = "2.11.4" indexmap = "2.11.4"
itertools = "0.14.0" itertools = "0.14.0"
regex = "1.11.3" regex = "1.11.3"
uuid = "1.18.1" uuid = { version = "1.18.1", features = ["v4"] }

View File

@ -232,23 +232,31 @@ impl FromStr for ProgId {
// region: CLSID // region: CLSID
/// The struct representing Windows CLSID looks like
/// `{26EE0668-A00A-44D7-9371-BEB064C98683}` (case insensitive).
/// The brace is essential part.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Clsid { pub struct Clsid {
inner: Uuid, inner: Uuid,
} }
impl Clsid { impl Clsid {
pub fn new(uuid: &str) -> Result<Self, ParseClsidError> { /// Create new CLSID from underlying UUID.
Self::from_str(uuid) fn new(uuid: &Uuid) -> Self {
Self { inner: *uuid }
} }
// TODO: May add CLSID generator in there. /// Create new random CLSID.
pub fn with_random() -> Self {
Self::new(&Uuid::new_v4())
}
} }
/// The error occurs when parsing CLSID /// The error occurs when parsing CLSID.
#[derive(Debug, TeError)] #[derive(Debug, TeError)]
#[error("given string \"{inner}\" is invalid for uuid")] #[error("given string \"{inner}\" is invalid for uuid")]
pub struct ParseClsidError { pub struct ParseClsidError {
/// The clone of string which is not a valid CLSID.
inner: String, inner: String,
} }
@ -264,9 +272,8 @@ impl FromStr for Clsid {
type Err = ParseClsidError; type Err = ParseClsidError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self { let brace_uuid = uuid::fmt::Braced::from_str(s).map_err(|_| ParseClsidError::new(s))?;
inner: Uuid::parse_str(s).map_err(|_| ParseClsidError::new(s))?, Ok(Self::new(brace_uuid.as_uuid()))
})
} }
} }

View File

@ -81,8 +81,19 @@ fn test_prog_id_parse() {
#[test] #[test]
fn test_clsid() { fn test_clsid() {
fn ok_tester(s: &str) {} fn ok_tester(s: &str) {
fn err_tester(s: &str) {} let rv = Clsid::from_str(s);
assert!(rv.is_ok());
}
fn err_tester(s: &str) {
let rv = Clsid::from_str(s);
assert!(rv.is_err());
}
ok_tester("{59031a47-3f72-44a7-89c5-5595fe6b30ee}");
ok_tester("{26EE0668-A00A-44D7-9371-BEB064C98683}");
err_tester("26EE0668-A00A-44D7-9371-BEB064C98683");
err_tester("{26EE0668A00A-44D7-9371-BEB064C98683}");
} }
#[test] #[test]