From 10abda211cb1cc198a445f8f8add66718d672f1a Mon Sep 17 00:00:00 2001 From: b-yap <2826165+b-yap@users.noreply.github.com> Date: Tue, 15 Oct 2024 23:20:27 +0800 Subject: [PATCH 1/2] conversion to base64 string --- src/lib.rs | 7 +++++++ src/utils/std.rs | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 7138bf1..cb255c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,9 +10,16 @@ mod lib { #[cfg(not(feature = "std"))] pub use alloc::string::{FromUtf8Error, String, ToString}; + #[cfg(not(feature = "std"))] + pub use alloc::format; + #[cfg(feature = "std")] #[allow(unused_imports)] pub use std::string::{FromUtf8Error, String, ToString}; + + #[cfg(feature = "std")] + #[allow(unused_imports)] + pub use std::format; } mod amount; diff --git a/src/utils/std.rs b/src/utils/std.rs index 9dc8784..7fb3c9f 100644 --- a/src/utils/std.rs +++ b/src/utils/std.rs @@ -1,4 +1,5 @@ -use crate::lib::String; +use crate::lib::{String, format}; +use crate::XdrCodec; /// A trait used to convert any Stellar specific type `T` as a String /// This also immediately converts the standard Error to a user-defined Error `E` @@ -9,3 +10,16 @@ use crate::lib::String; pub trait StellarTypeToString> { fn as_encoded_string(&self) -> Result; } + +/// A trait used to convert a structure into Base64 String +pub trait StellarTypeToBase64String { + /// returns a Base64 encoded String or a vec of bytes [xdr] + fn as_base64_encoded_string(&self) -> String; +} + +impl StellarTypeToBase64String for T { + fn as_base64_encoded_string(&self) -> String { + let xdr = self.to_base64_xdr(); + String::from_utf8(xdr.clone()).unwrap_or(format!("{:?}", xdr)) + } +} \ No newline at end of file From 52e366e786889baa77e7e4f36831d528cfafc166 Mon Sep 17 00:00:00 2001 From: b-yap <2826165+b-yap@users.noreply.github.com> Date: Wed, 16 Oct 2024 13:39:32 +0800 Subject: [PATCH 2/2] https://github.com/pendulum-chain/substrate-stellar-sdk/pull/35/files#r1801904523 --- src/lib.rs | 7 ------- src/utils/std.rs | 5 +++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cb255c5..7138bf1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,16 +10,9 @@ mod lib { #[cfg(not(feature = "std"))] pub use alloc::string::{FromUtf8Error, String, ToString}; - #[cfg(not(feature = "std"))] - pub use alloc::format; - #[cfg(feature = "std")] #[allow(unused_imports)] pub use std::string::{FromUtf8Error, String, ToString}; - - #[cfg(feature = "std")] - #[allow(unused_imports)] - pub use std::format; } mod amount; diff --git a/src/utils/std.rs b/src/utils/std.rs index 7fb3c9f..811ea1a 100644 --- a/src/utils/std.rs +++ b/src/utils/std.rs @@ -1,4 +1,4 @@ -use crate::lib::{String, format}; +use crate::lib::String; use crate::XdrCodec; /// A trait used to convert any Stellar specific type `T` as a String @@ -20,6 +20,7 @@ pub trait StellarTypeToBase64String { impl StellarTypeToBase64String for T { fn as_base64_encoded_string(&self) -> String { let xdr = self.to_base64_xdr(); - String::from_utf8(xdr.clone()).unwrap_or(format!("{:?}", xdr)) + // safe to use `unwrap`, since `to_base64_xdr()` will always return a valid vec of + String::from_utf8(xdr.clone()).unwrap() } } \ No newline at end of file