winres_edit/
id.rs

1use windows::core::PCSTR;
2
3///
4/// Id enum that contains a windows resource id representation.  Windows resource ids can be
5/// a pointer to a string or if the pointer value is below 0xffff the id represents an integer
6/// resource id.  [`Id`] encapsulates this representation into a Rust enum and provides `From`
7/// and `Into` trait implementations to interop with the Windows API.
8///
9#[derive(Debug, Clone, Eq, PartialEq, Hash)]
10pub enum Id {
11    Integer(u16),
12    Text(String),
13}
14
15/// Convert a string pointer to an `Id`
16impl From<PCSTR> for Id {
17    fn from(v: PCSTR) -> Self {
18        let pv = v.0 as usize;
19        if pv < 0xffff {
20            Id::Integer(pv as u16)
21        } else {
22            unsafe { Id::Text(v.display().to_string()) }
23        }
24    }
25}
26
27/// Convert a `u16` value to an `Id`
28impl From<u16> for Id {
29    fn from(v: u16) -> Self {
30        Id::Integer(v)
31    }
32}
33
34/// Convert an `Id` to a zero-terminated string pointer or
35/// an integer resource representation.
36impl Into<PCSTR> for Id {
37    fn into(self) -> PCSTR {
38        match self {
39            Id::Integer(id) => PCSTR(id as *const u8),
40            Id::Text(text) => PCSTR::from_raw(format!("{text}\0").as_ptr()),
41        }
42    }
43}
44
45impl Into<PCSTR> for &Id {
46    fn into(self) -> PCSTR {
47        match self {
48            Id::Integer(id) => PCSTR(*id as *const u8),
49            Id::Text(text) => PCSTR::from_raw(format!("{text}\0").as_ptr()),
50        }
51    }
52}