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

conversion to base64 string #35

Merged
merged 2 commits into from
Oct 21, 2024
Merged
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
15 changes: 15 additions & 0 deletions src/utils/std.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::lib::String;
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`
Expand All @@ -9,3 +10,17 @@ use crate::lib::String;
pub trait StellarTypeToString<T, E: From<sp_std::str::Utf8Error>> {
fn as_encoded_string(&self) -> Result<String, E>;
}

/// 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 <T: XdrCodec> StellarTypeToBase64String for T {
fn as_base64_encoded_string(&self) -> String {
let xdr = self.to_base64_xdr();
// safe to use `unwrap`, since `to_base64_xdr()` will always return a valid vec of
String::from_utf8(xdr.clone()).unwrap()
}
}
Loading