Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding parse_stellar_type macro #25

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 38 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sha2 = { default-features = false, version = "0.9.9" }
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
base64 = { default-features = false, version = "0.13.0" }
num-rational = {version = "0.4", default-features = false}

scale-info = {version = "2.1.1", default-features = false, features = ["derive"]}

[features]
default = [ "offchain", "std" ]
Expand All @@ -34,6 +34,7 @@ std = [
"sp-io/std",
"serde_json/std",
"serde/std",
"scale-info/std",
"hex/std",
"num-rational/std"
]
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use hex::FromHexError;
#[cfg(feature = "offchain")]
use crate::horizon::FetchError;

use sp_std::vec::Vec;

#[derive(Debug, Clone, PartialEq)]
pub enum StellarSdkError {
InvalidBase32Character {
Expand Down Expand Up @@ -86,4 +88,6 @@ pub enum StellarSdkError {

#[cfg(feature = "offchain")]
FetchError(FetchError),

DecodeError(Vec<u8>), // String converted as Bytes of u8
}
3 changes: 2 additions & 1 deletion src/xdr/impls/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{lib::String, types::Error};
use scale_info::prelude::format;

impl<E: From<sp_std::str::Utf8Error>> crate::StellarTypeToString<Self, E> for Error {
fn as_encoded_string(&self) -> Result<String, E> {
let msg = self.msg.get_vec();
let msg = sp_std::str::from_utf8(msg).map_err(E::from)?.to_string();
let msg = sp_std::str::from_utf8(msg).map_err(E::from)?;
Ok(format!("Error{{ code:{:?} message:{msg} }}", self.code))
}
}
12 changes: 12 additions & 0 deletions src/xdr/impls/transaction_set_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ impl TransactionSetType {
}
}

impl From<TransactionSet> for TransactionSetType {
fn from(value: TransactionSet) -> Self {
TransactionSetType::TransactionSet(value)
}
}

impl From<GeneralizedTransactionSet> for TransactionSetType {
fn from(value: GeneralizedTransactionSet) -> Self {
TransactionSetType::GeneralizedTransactionSet(value)
}
}

impl XdrCodec for TransactionSetType {
fn to_xdr_buffered(&self, write_stream: &mut WriteStream) {
match self {
Expand Down
2 changes: 2 additions & 0 deletions src/xdr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ pub mod compound_types;
pub mod impls;
pub mod streams;
pub mod types;

#[macro_use]
pub mod xdr_codec;
23 changes: 23 additions & 0 deletions src/xdr/xdr_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,26 @@ impl<T: XdrCodec> XdrCodec for Box<T> {
Ok(Box::new(T::from_xdr_buffered(read_stream)?))
}
}

/// To easily convert any bytes to a Stellar type.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use crate::types::Auth;
/// let auth_xdr = [0, 0, 0, 1];
/// let result = parse_stellar_type!(auth_xdr,Auth);
/// assert_eq!(result, Ok(Auth { flags: 1 }))
/// ```
#[macro_export]
macro_rules! parse_stellar_type {
($ref:ident, $struct_str:ident) => {{
use $crate::{types::$struct_str, StellarSdkError};

let ret: Result<$struct_str, StellarSdkError> =
$struct_str::from_xdr($ref).map_err(|_| StellarSdkError::DecodeError(stringify!($struct_str).into()));
ret
}};
}