templar_proxy_oracle_kernel/primitive/
account_id.rs1#[cfg(feature = "borsh")]
2use alloc::string::ToString;
3
4#[cfg_attr(
5 feature = "borsh",
6 derive(
7 ::borsh::BorshSerialize,
8 ::borsh::BorshDeserialize,
9 ::borsh::BorshSchema
10 )
11)]
12#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
13#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct AccountId(
15 #[cfg_attr(
16 feature = "serde",
17 serde(
18 serialize_with = "hex::serde::serialize",
19 deserialize_with = "hex::serde::deserialize"
20 )
21 )]
22 pub [u8; 64],
23);
24
25impl AccountId {
26 #[must_use]
27 pub const fn from_bytes(bytes: [u8; 64]) -> Self {
28 Self(bytes)
29 }
30
31 #[must_use]
32 pub const fn as_bytes(&self) -> &[u8; 64] {
33 &self.0
34 }
35}
36
37#[cfg(feature = "schemars")]
38impl schemars::JsonSchema for AccountId {
39 fn schema_name() -> alloc::string::String {
40 alloc::string::String::from("AccountId")
41 }
42
43 fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
44 let mut schema = schemars::schema::SchemaObject::default();
45 schema.instance_type = Some(schemars::schema::InstanceType::String.into());
46 schema.metadata().description = Some(alloc::string::String::from("Account ID"));
47 schema.string().pattern = Some(alloc::string::String::from("^[0-9a-fA-F]{128}$"));
48 schema.string().min_length = Some(128);
49 schema.string().max_length = Some(128);
50 schema.into()
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 #[cfg(feature = "schemars")]
57 use super::AccountId;
58
59 #[cfg(feature = "schemars")]
60 #[test]
61 fn account_id_schema_is_fixed_length_hex() {
62 let schema = schemars::schema_for!(AccountId).schema;
63 let string = schema
64 .string
65 .expect("AccountId should use string validation");
66
67 assert_eq!(string.pattern.as_deref(), Some("^[0-9a-fA-F]{128}$"));
68 assert_eq!(string.min_length, Some(128));
69 assert_eq!(string.max_length, Some(128));
70 }
71}