-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: TxnResult for returning result or base64 xdr
- Loading branch information
1 parent
4716bec
commit 5f7a6e7
Showing
11 changed files
with
126 additions
and
37 deletions.
There are no files selected for viewing
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
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
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
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,56 @@ | ||
use std::fmt::{Display, Formatter}; | ||
|
||
use soroban_sdk::xdr::{self, Limits, WriteXdr}; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error("Expect xdr string")] | ||
XdrStringExpected, | ||
#[error("Expect result")] | ||
ResultExpected, | ||
} | ||
|
||
pub enum TxnResult<T> { | ||
Xdr(String), | ||
Res(T), | ||
} | ||
|
||
impl<T> TxnResult<T> { | ||
pub fn from_xdr(res: &impl WriteXdr) -> Result<Self, xdr::Error> { | ||
Ok(TxnResult::Xdr(res.to_xdr_base64(Limits::none())?)) | ||
} | ||
|
||
pub fn xdr(&self) -> Option<&str> { | ||
match self { | ||
TxnResult::Xdr(xdr) => Some(xdr), | ||
TxnResult::Res(_) => None, | ||
} | ||
} | ||
|
||
pub fn res(self) -> Option<T> { | ||
match self { | ||
TxnResult::Res(res) => Some(res), | ||
TxnResult::Xdr(_) => None, | ||
} | ||
} | ||
|
||
pub fn try_xdr(&self) -> Result<&str, Error> { | ||
self.xdr().ok_or(Error::XdrStringExpected) | ||
} | ||
|
||
pub fn try_res(self) -> Result<T, Error> { | ||
self.res().ok_or(Error::ResultExpected) | ||
} | ||
} | ||
|
||
impl<T> Display for TxnResult<T> | ||
where | ||
T: Display, | ||
{ | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
TxnResult::Xdr(xdr) => write!(f, "{xdr}"), | ||
TxnResult::Res(res) => write!(f, "{res}"), | ||
} | ||
} | ||
} |
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