prople_did_core/context/ext/
user_connection.rs

1use rst_common::standard::serde::{self, Deserialize, Serialize};
2use rst_common::standard::serde_json;
3
4use crate::types::{DIDError, JSONValue, ToJSON, CONTEXT_VC};
5
6pub const CONTEXT_USER_AGENT_ADDRESS: &str = "https://schema.org/identifier";
7
8/// `UserConnectionCredentialProperties` is a properties designed to  fullfil the connection
9/// request contexts when user want to connect with others
10#[derive(Debug, Serialize, Deserialize, Clone)]
11#[serde(rename_all = "camelCase")]
12#[serde(crate = "self::serde")]
13pub struct UserConnectionCredentialProperties {
14    pub user_agent_address: String,
15    pub user_did: String,
16}
17
18impl Default for UserConnectionCredentialProperties {
19    fn default() -> Self {
20        Self {
21            user_agent_address: String::from(CONTEXT_USER_AGENT_ADDRESS),
22            user_did: String::from(CONTEXT_VC),
23        }
24    }
25}
26
27/// `UserConnectionCredentialContext` is a custom context used specifically for the `Prople` needs
28#[derive(Debug, Serialize, Deserialize, Default, Clone)]
29#[serde(crate = "self::serde")]
30pub struct UserConnectionCredentialContext {
31    #[serde(rename = "@context")]
32    pub context: UserConnectionCredentialProperties,
33}
34
35impl UserConnectionCredentialContext {
36    pub fn new(properties: UserConnectionCredentialProperties) -> Self {
37        Self {
38            context: properties,
39        }
40    }
41}
42
43/// `UserConnectionCredential` is an object used to generate main credential when connecting with others
44#[derive(Debug, Serialize, Deserialize, Default, Clone)]
45#[serde(rename_all = "PascalCase")]
46#[serde(crate = "self::serde")]
47pub struct UserConnectionCredential {
48    pub user_connection_credential: UserConnectionCredentialContext,
49}
50
51impl UserConnectionCredential {
52    pub fn new(context: UserConnectionCredentialContext) -> Self {
53        Self {
54            user_connection_credential: context,
55        }
56    }
57}
58
59impl ToJSON for UserConnectionCredential {
60    fn to_json(&self) -> Result<JSONValue, DIDError> {
61        let jsonstr = serde_json::to_string(self)
62            .map_err(|err| DIDError::GenerateJSONError(err.to_string()))?;
63        Ok(JSONValue::from(jsonstr))
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_build_user_connection_credential_json() {
73        let credential = UserConnectionCredential::default();
74        let to_json = credential.to_json();
75        assert!(!to_json.is_err());
76
77        let expected_json = r#"{"UserConnectionCredential":{"@context":{"userAgentAddress":"https://schema.org/identifier","userDid":"https://www.w3.org/2018/credentials/#VerifiableCredential"}}}"#;
78        assert_eq!(to_json.unwrap(), JSONValue::from(expected_json))
79    }
80}