1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
pub use indy_data_types::anoncreds::schema::{
    AttributeNames, Schema, SchemaV1, MAX_ATTRIBUTES_COUNT,
};

use super::constants::{GET_SCHEMA, SCHEMA};
use super::did::ShortDidValue;
use super::{get_sp_key_marker, ProtocolVersion, RequestType};
use crate::common::error::prelude::*;

use std::collections::HashSet;

#[derive(Serialize, PartialEq, Debug)]
pub struct SchemaOperation {
    #[serde(rename = "type")]
    pub _type: String,
    pub data: SchemaOperationData,
}

impl SchemaOperation {
    pub fn new(data: SchemaOperationData) -> Self {
        Self {
            data,
            _type: Self::get_txn_type().to_string(),
        }
    }
}

impl RequestType for SchemaOperation {
    fn get_txn_type<'a>() -> &'a str {
        SCHEMA
    }
}

#[derive(Serialize, PartialEq, Debug, Deserialize)]
pub struct SchemaOperationData {
    pub name: String,
    pub version: String,
    pub attr_names: HashSet<String>,
}

impl SchemaOperationData {
    pub fn new(name: String, version: String, attr_names: HashSet<String>) -> Self {
        Self {
            name,
            version,
            attr_names,
        }
    }
}

#[derive(Serialize, PartialEq, Debug)]
pub struct GetSchemaOperation {
    #[serde(rename = "type")]
    pub _type: String,
    pub dest: ShortDidValue,
    pub data: GetSchemaOperationData,
}

impl GetSchemaOperation {
    pub fn new(dest: ShortDidValue, data: GetSchemaOperationData) -> Self {
        Self {
            _type: Self::get_txn_type().to_string(),
            dest,
            data,
        }
    }
}

impl RequestType for GetSchemaOperation {
    fn get_txn_type<'a>() -> &'a str {
        GET_SCHEMA
    }

    fn get_sp_key(&self, protocol_version: ProtocolVersion) -> VdrResult<Option<Vec<u8>>> {
        let marker = get_sp_key_marker(2, protocol_version);
        Ok(Some(
            format!(
                "{}:{}:{}:{}",
                &*self.dest, marker, self.data.name, self.data.version
            )
            .as_bytes()
            .to_vec(),
        ))
    }
}

#[derive(Serialize, PartialEq, Debug, Deserialize)]
pub struct GetSchemaOperationData {
    pub name: String,
    pub version: String,
}

impl GetSchemaOperationData {
    pub fn new(name: String, version: String) -> Self {
        Self { name, version }
    }
}