Skip to main content

lastid_sdk/types/
request_id.rs

1//! Request ID type definition.
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7/// Opaque identifier for credential requests.
8///
9/// This type wraps the request ID returned by the IDP and provides
10/// type safety to prevent mixing with other string identifiers.
11#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct RequestId(String);
14
15impl RequestId {
16    /// Create a new request ID.
17    #[must_use]
18    pub fn new(id: impl Into<String>) -> Self {
19        Self(id.into())
20    }
21
22    /// Get the request ID as a string slice.
23    #[must_use]
24    pub fn as_str(&self) -> &str {
25        &self.0
26    }
27}
28
29impl fmt::Display for RequestId {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "{}", self.0)
32    }
33}
34
35impl From<String> for RequestId {
36    fn from(s: String) -> Self {
37        Self::new(s)
38    }
39}
40
41impl From<&str> for RequestId {
42    fn from(s: &str) -> Self {
43        Self::new(s)
44    }
45}
46
47impl AsRef<str> for RequestId {
48    fn as_ref(&self) -> &str {
49        &self.0
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_request_id_new() {
59        let id = RequestId::new("req_123");
60        assert_eq!(id.as_str(), "req_123");
61    }
62
63    #[test]
64    fn test_request_id_display() {
65        let id = RequestId::new("req_abc");
66        assert_eq!(format!("{id}"), "req_abc");
67    }
68
69    #[test]
70    fn test_request_id_serialize() {
71        let id = RequestId::new("req_test");
72        let json = serde_json::to_string(&id).unwrap();
73        assert_eq!(json, "\"req_test\"");
74    }
75
76    #[test]
77    fn test_request_id_deserialize() {
78        let id: RequestId = serde_json::from_str("\"req_test\"").unwrap();
79        assert_eq!(id.as_str(), "req_test");
80    }
81
82    #[test]
83    fn test_request_id_equality() {
84        let id1 = RequestId::new("req_123");
85        let id2 = RequestId::new("req_123");
86        let id3 = RequestId::new("req_456");
87
88        assert_eq!(id1, id2);
89        assert_ne!(id1, id3);
90    }
91}