Skip to content

Commit

Permalink
Change some ConversionErrors to Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Sep 8, 2023
1 parent 85fba22 commit 950750d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
23 changes: 11 additions & 12 deletions soroban-env-common/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,20 @@ sa::const_assert!(CODE_MASK == 0x3f);
sa::const_assert!(CODE_BITS * MAX_SMALL_CHARS + 2 == BODY_BITS);

impl<E: Env> TryFromVal<E, &str> for Symbol {
type Error = ConversionError;
type Error = crate::Error;

fn try_from_val(env: &E, v: &&str) -> Result<Self, Self::Error> {
if let Ok(ss) = SymbolSmall::try_from_str(v) {
Ok(Self(ss.0))
} else if let Ok(so) = env.symbol_new_from_slice(v) {
Ok(Self(so.0))
} else {
Err(ConversionError)
let sobj = env.symbol_new_from_slice(v).map_err(Into::into)?;
Ok(Self(sobj.0))
}
}
}

impl<E: Env> TryFromVal<E, &[u8]> for Symbol {
type Error = ConversionError;
type Error = crate::Error;

fn try_from_val(env: &E, v: &&[u8]) -> Result<Self, Self::Error> {
// We don't know this byte-slice is actually utf-8 ...
Expand Down Expand Up @@ -312,7 +311,7 @@ impl From<SymbolSmall> for SymbolStr {
}

impl<E: Env> TryFromVal<E, Symbol> for SymbolStr {
type Error = ConversionError;
type Error = crate::Error;

fn try_from_val(env: &E, v: &Symbol) -> Result<Self, Self::Error> {
if let Ok(ss) = SymbolSmall::try_from(*v) {
Expand All @@ -321,7 +320,7 @@ impl<E: Env> TryFromVal<E, Symbol> for SymbolStr {
let obj: SymbolObject = unsafe { SymbolObject::unchecked_from_val(v.0) };
let mut arr = [0u8; SCSYMBOL_LIMIT as usize];
env.symbol_copy_to_slice(obj, Val::U32_ZERO, &mut arr)
.map_err(|_| ConversionError)?;
.map_err(Into::into)?;
Ok(SymbolStr(arr))
}
}
Expand Down Expand Up @@ -433,7 +432,7 @@ impl TryFrom<&ScVal> for SymbolSmall {

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, ScVal> for Symbol {
type Error = ConversionError;
type Error = crate::Error;

fn try_from_val(env: &E, v: &ScVal) -> Result<Self, Self::Error> {
Symbol::try_from_val(env, &v)
Expand All @@ -442,19 +441,19 @@ impl<E: Env> TryFromVal<E, ScVal> for Symbol {

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, &ScVal> for Symbol {
type Error = ConversionError;
type Error = crate::Error;
fn try_from_val(env: &E, v: &&ScVal) -> Result<Self, Self::Error> {
if let ScVal::Symbol(sym) = v {
Symbol::try_from_val(env, &sym)
} else {
Err(ConversionError)
Err(ConversionError.into())
}
}
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, ScSymbol> for Symbol {
type Error = ConversionError;
type Error = crate::Error;

fn try_from_val(env: &E, v: &ScSymbol) -> Result<Self, Self::Error> {
Symbol::try_from_val(env, &v)
Expand All @@ -463,7 +462,7 @@ impl<E: Env> TryFromVal<E, ScSymbol> for Symbol {

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, &ScSymbol> for Symbol {
type Error = ConversionError;
type Error = crate::Error;
fn try_from_val(env: &E, v: &&ScSymbol) -> Result<Self, Self::Error> {
Symbol::try_from_val(env, &v.0.as_slice())
}
Expand Down
6 changes: 6 additions & 0 deletions soroban-env-common/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ impl From<stellar_xdr::Error> for ConversionError {
}
}

impl From<crate::Error> for ConversionError {
fn from(_: crate::Error) -> Self {
ConversionError
}
}

/// Trait abstracting over types that can be converted into [Val], similar to
/// [TryFrom] but with a different signature that enables generating slightly
/// more efficient conversion code. An implementation of `TryFrom<Val>` is also
Expand Down
2 changes: 1 addition & 1 deletion soroban-native-sdk-macros/src/derive_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn derive_type_enum(ident: &Ident, data: &DataEnum) -> TokenStream2 {
quote! {

impl #ident {
fn discriminant_sym(&self, env: &crate::Host) -> Result<crate::Symbol, crate::ConversionError> {
fn discriminant_sym(&self, env: &crate::Host) -> Result<crate::Symbol, crate::Error> {
use soroban_env_common::TryFromVal;
match self {
#(#syms,)*
Expand Down

0 comments on commit 950750d

Please sign in to comment.