-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from DiLeqL/add-data-tx
- add read for data transaction
- Loading branch information
Showing
9 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::model::data_entry::DataEntry::{ | ||
BinaryEntry, BooleanEntry, DeleteEntry, IntegerEntry, StringEntry, | ||
}; | ||
use serde_json::Value; | ||
|
||
#[derive(Clone)] | ||
pub enum DataEntry { | ||
IntegerEntry { key: String, value: u64 }, | ||
BooleanEntry { key: String, value: bool }, | ||
BinaryEntry { key: String, value: Vec<u8> }, | ||
StringEntry { key: String, value: String }, | ||
DeleteEntry { key: String }, | ||
} | ||
|
||
impl From<&Value> for DataEntry { | ||
fn from(value: &Value) -> Self { | ||
let key_field = value["key"].as_str().unwrap().into(); | ||
let value_field = &value["value"]; | ||
match value["type"].as_str().unwrap() { | ||
"" => DeleteEntry { key: key_field }, | ||
"binary" => BinaryEntry { | ||
key: key_field, | ||
value: base64::decode(value_field.as_str().unwrap()).unwrap(), | ||
}, | ||
"boolean" => BooleanEntry { | ||
key: key_field, | ||
value: value_field.as_bool().unwrap(), | ||
}, | ||
"integer" => IntegerEntry { | ||
key: key_field, | ||
value: value_field.as_u64().unwrap(), | ||
}, | ||
"string" => StringEntry { | ||
key: key_field, | ||
value: value_field.as_str().unwrap().into(), | ||
}, | ||
_ => panic!("unknown type"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use crate::model::data_entry::DataEntry; | ||
use serde_json::Value; | ||
|
||
pub struct DataTransaction { | ||
data: Vec<DataEntry>, | ||
} | ||
|
||
impl DataTransaction { | ||
// todo return Result<DataTransaction, Error> | ||
pub fn from_json(value: Value) -> DataTransaction { | ||
let data = value["data"] | ||
.as_array() | ||
.unwrap() | ||
.iter() | ||
.map(|entry| entry.into()) | ||
.collect::<Vec<DataEntry>>(); | ||
|
||
DataTransaction { data } | ||
} | ||
|
||
pub fn data(&self) -> Vec<DataEntry> { | ||
self.data.clone() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod data_transaction; | ||
mod transaction_info; | ||
mod transfer_transaction; | ||
|
||
pub use data_transaction::*; | ||
pub use transaction_info::*; | ||
pub use transfer_transaction::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters