neo3/neo_protocol/responses/
response_transaction_attribute.rs

1use crate::builder::OracleResponseCode;
2use primitive_types::H256;
3use serde::{Deserialize, Deserializer, Serialize};
4
5#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
6#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
7pub enum TransactionAttributeType {
8	HighPriority,
9	OracleResponse,
10	NotValidBefore,
11	Conflicts,
12	// Add other types as needed
13}
14
15// pub trait TransactionAttribute {
16//     fn get_type(&self) -> TransactionAttributeType;
17// }
18
19#[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq)]
20pub struct HighPriorityAttribute {
21	// #[serde(rename = "type")]
22	// pub attribute_type: TransactionAttributeType,
23	// Add other fields specific to HighPriorityAttribute if needed
24}
25
26// impl TransactionAttribute for HighPriorityAttribute {
27//     fn get_type(&self) -> TransactionAttributeType {
28//         self.attribute_type.clone()
29//     }
30// }
31
32#[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq)]
33pub struct OracleResponseAttribute {
34	// #[serde(rename = "type")]
35	// pub attribute_type: TransactionAttributeType,
36	#[serde(flatten)]
37	pub oracle_response: OracleResponse,
38	// Add other fields specific to OracleResponseAttribute if needed
39}
40
41// impl TransactionAttribute for OracleResponseAttribute {
42//     fn get_type(&self) -> TransactionAttributeType {
43//         self.attribute_type.clone()
44//     }
45// }
46
47// NotValidBeforeAttribute Struct and Implementation
48#[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq)]
49pub struct NotValidBeforeAttribute {
50	// #[serde(rename = "type")]
51	// pub attribute_type: TransactionAttributeType,
52	#[serde(rename = "height", deserialize_with = "deserialize_height")]
53	pub height: i64,
54	// Add other fields specific to NotValidBeforeAttribute if needed
55}
56
57// impl TransactionAttribute for NotValidBeforeAttribute {
58//     fn get_type(&self) -> TransactionAttributeType {
59//         self.attribute_type.clone()
60//     }
61// }
62
63// ConflictsAttribute Struct and Implementation
64#[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq)]
65pub struct ConflictsAttribute {
66	// #[serde(rename = "type")]
67	// pub attribute_type: TransactionAttributeType,
68	#[serde(rename = "hash")]
69	pub hash: H256,
70	// Add other fields specific to ConflictsAttribute if needed
71}
72
73// impl TransactionAttribute for ConflictsAttribute {
74//     fn get_type(&self) -> TransactionAttributeType {
75//         self.attribute_type.clone()
76//     }
77// }
78
79// Add similar structs and implementations for NotValidBeforeAttribute, ConflictsAttribute, etc.
80#[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq)]
81#[serde(tag = "type")] // Uses the "type" field in the JSON to determine the variant
82pub enum TransactionAttributeEnum {
83	#[serde(rename = "HighPriority")]
84	HighPriority(HighPriorityAttribute),
85
86	#[serde(rename = "OracleResponse")]
87	OracleResponse(OracleResponseAttribute),
88
89	#[serde(rename = "NotValidBefore")]
90	NotValidBefore(NotValidBeforeAttribute),
91
92	#[serde(rename = "Conflicts")]
93	Conflicts(ConflictsAttribute),
94	// Add other variants as needed
95}
96
97#[derive(Serialize, Deserialize, PartialEq, Hash, Debug, Clone)]
98pub struct OracleResponse {
99	pub(crate) id: u32,
100	#[serde(rename = "code")]
101	pub(crate) response_code: OracleResponseCode,
102	pub(crate) result: String,
103}
104
105// Custom deserialization function for height
106fn deserialize_height<'de, D>(deserializer: D) -> Result<i64, D::Error>
107where
108	D: Deserializer<'de>,
109{
110	let value: serde_json::Value = Deserialize::deserialize(deserializer)?;
111	match value {
112		serde_json::Value::Number(num) =>
113			num.as_i64().ok_or_else(|| serde::de::Error::custom("invalid number")),
114		serde_json::Value::String(s) => s.parse::<i64>().map_err(serde::de::Error::custom),
115		_ => Err(serde::de::Error::custom("invalid type for height")),
116	}
117}