-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an optional way to handle contract errors with
Result
(#745)
- Loading branch information
Showing
13 changed files
with
258 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod code_generator; | ||
mod info_extractor; | ||
mod metadata; | ||
mod utils; | ||
pub use code_generator::*; | ||
pub use info_extractor::*; | ||
pub use metadata::metadata_visitor::MetadataVisitor; |
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,41 @@ | ||
use syn::{GenericArgument, Path, PathArguments, Type}; | ||
|
||
/// Checks whether the given path is literally "Result". | ||
/// Note that it won't match a fully qualified name `core::result::Result` or a type alias like | ||
/// `type StringResult = Result<String, String>`. | ||
pub(crate) fn path_is_result(path: &Path) -> bool { | ||
path.leading_colon.is_none() | ||
&& path.segments.len() == 1 | ||
&& path.segments.iter().next().unwrap().ident == "Result" | ||
} | ||
|
||
/// Equivalent to `path_is_result` except that it works on `Type` values. | ||
pub(crate) fn type_is_result(ty: &Type) -> bool { | ||
match ty { | ||
Type::Path(type_path) if type_path.qself.is_none() => path_is_result(&type_path.path), | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Extracts the Ok type from a `Result` type. | ||
/// | ||
/// For example, given `Result<String, u8>` type it will return `String` type. | ||
pub(crate) fn extract_ok_type(ty: &Type) -> Option<&Type> { | ||
match ty { | ||
Type::Path(type_path) if type_path.qself.is_none() && path_is_result(&type_path.path) => { | ||
// Get the first segment of the path (there should be only one, in fact: "Result"): | ||
let type_params = &type_path.path.segments.first()?.arguments; | ||
// We are interested in the first angle-bracketed param responsible for Ok type ("<String, _>"): | ||
let generic_arg = match type_params { | ||
PathArguments::AngleBracketed(params) => Some(params.args.first()?), | ||
_ => None, | ||
}?; | ||
// This argument must be a type: | ||
match generic_arg { | ||
GenericArgument::Type(ty) => Some(ty), | ||
_ => None, | ||
} | ||
} | ||
_ => None, | ||
} | ||
} |
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,50 @@ | ||
//! Testing FunctionError macro. | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use near_sdk::{near_bindgen, FunctionError}; | ||
use std::fmt; | ||
|
||
#[derive(FunctionError, BorshSerialize)] | ||
struct ErrorStruct { | ||
message: String, | ||
} | ||
|
||
impl fmt::Display for ErrorStruct { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "error ocurred: {}", self.message) | ||
} | ||
} | ||
|
||
#[derive(FunctionError, BorshSerialize)] | ||
enum ErrorEnum { | ||
NotFound, | ||
Banned { account_id: String }, | ||
} | ||
|
||
impl fmt::Display for ErrorEnum { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
ErrorEnum::NotFound => write!(f, "not found"), | ||
ErrorEnum::Banned { account_id } => write!(f, "account {} is banned", account_id) | ||
} | ||
} | ||
} | ||
|
||
#[near_bindgen] | ||
#[derive(BorshDeserialize, BorshSerialize, Default)] | ||
struct Contract {} | ||
|
||
#[near_bindgen] | ||
impl Contract { | ||
#[return_result] | ||
pub fn set(&self, value: String) -> Result<String, ErrorStruct> { | ||
Err(ErrorStruct { message: format!("Could not set to {}", value) }) | ||
} | ||
|
||
#[return_result] | ||
pub fn get(&self) -> Result<String, ErrorEnum> { | ||
Err(ErrorEnum::NotFound) | ||
} | ||
} | ||
|
||
fn main() {} |
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
Oops, something went wrong.