Skip to content

Commit

Permalink
Adapt SDK to changes to {Try,}{From,Into}Val in env crates.
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Jan 17, 2023
1 parent a7cf3f0 commit 4237a60
Show file tree
Hide file tree
Showing 24 changed files with 296 additions and 672 deletions.
6 changes: 3 additions & 3 deletions soroban-auth/src/tests/test_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl ExampleContract {
&env,
&sig,
symbol!("examplefn"),
(&sig.identifier(&env), arg1, arg2),
(sig.identifier(&env), arg1, arg2),
);
}
}
Expand Down Expand Up @@ -54,7 +54,7 @@ fn test() {
&signer,
&contract_id,
symbol!("examplefn"),
(&id, &1, &2),
(id, 1, 2),
);
std::println!("signature: {:?}", sig);

Expand Down Expand Up @@ -92,7 +92,7 @@ fn test_build_keypair() {
&signer,
&contract_id,
symbol!("examplefn"),
(&id, &1, &2),
(id, 1, 2),
);
std::println!("signature: {:?}", sig);

Expand Down
6 changes: 3 additions & 3 deletions soroban-auth/src/tests/test_ed25519_with_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl TestContract {

verify_and_consume_nonce(&e, &auth_id, nonce);

verify(&e, &sig, symbol!("verify_sig"), (&auth_id, nonce));
verify(&e, &sig, symbol!("verify_sig"), (auth_id, nonce));
}

pub fn nonce(e: Env, id: Identifier) -> i128 {
Expand All @@ -54,7 +54,7 @@ pub struct OuterTestContract;
#[contractimpl]
impl OuterTestContract {
pub fn authorize(e: Env, contract_id: BytesN<32>) {
let client = TestContractClient::new(&e, contract_id);
let client = TestContractClient::new(&e, &contract_id);
client.verify_sig(&Signature::Invoker, &0);
}
}
Expand All @@ -74,7 +74,7 @@ fn test() {
&signer,
&contract_id,
symbol!("verify_sig"),
(&id, &nonce),
(id, nonce),
);

client.verify_sig(&sig, &nonce);
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub fn derive_client(name: &str, fns: &[ClientFn]) -> TokenStream {
}

impl #client_ident {
pub fn new(env: &soroban_sdk::Env, contract_id: impl soroban_sdk::IntoVal<soroban_sdk::Env, soroban_sdk::BytesN<32>>) -> Self {
pub fn new(env: &soroban_sdk::Env, contract_id: &impl soroban_sdk::IntoVal<soroban_sdk::Env, soroban_sdk::BytesN<32>>) -> Self {
Self {
env: env.clone(),
contract_id: contract_id.into_val(env),
Expand Down
81 changes: 24 additions & 57 deletions soroban-sdk-macros/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn derive_type_enum(
let mut errors = Vec::<Error>::new();

let variants = &data.variants;
let (spec_cases, discriminant_consts, try_froms, intos, try_from_xdrs, into_xdrs): (Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>) = variants
let (spec_cases, discriminant_consts, try_froms, try_intos, try_from_xdrs, into_xdrs): (Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>) = variants
.iter()
.map(|v| {
// TODO: Choose discriminant type based on repr type of enum.
Expand Down Expand Up @@ -72,7 +72,12 @@ pub fn derive_type_enum(
Self::#ident(iter.next().ok_or(#path::ConversionError)??.try_into_val(env)?)
}
};
let into = quote! { #enum_ident::#ident(ref value) => (#discriminant_const_sym_ident, value).into_val(env) };
let try_into = quote! {
#enum_ident::#ident(ref value) => {
let tup: (#path::RawVal, #path::RawVal) = (#discriminant_const_sym_ident.into(), value.try_into_val(env)?);
tup.try_into_val(env)
}
};
let try_from_xdr = quote! {
#name => {
if iter.len() > 1 {
Expand All @@ -83,7 +88,7 @@ pub fn derive_type_enum(
}
};
let into_xdr = quote! { #enum_ident::#ident(value) => (#name, value).try_into().map_err(|_| #path::xdr::Error::Invalid)? };
(spec_case, discriminant_const, try_from, into, try_from_xdr, into_xdr)
(spec_case, discriminant_const, try_from, try_into, try_from_xdr, into_xdr)
} else {
let spec_case = ScSpecUdtUnionCaseV0 {
name: name.try_into().unwrap_or_else(|_| StringM::default()),
Expand All @@ -97,7 +102,12 @@ pub fn derive_type_enum(
Self::#ident
}
};
let into = quote! { #enum_ident::#ident => (#discriminant_const_sym_ident,).into_val(env) };
let try_into = quote! {
#enum_ident::#ident => {
let tup: (#path::RawVal,) = (#discriminant_const_sym_ident.into(),);
tup.try_into_val(env)
}
};
let try_from_xdr = quote! {
#name => {
if iter.len() > 0 {
Expand All @@ -107,7 +117,7 @@ pub fn derive_type_enum(
}
};
let into_xdr = quote! { #enum_ident::#ident => (#name,).try_into().map_err(|_| #path::xdr::Error::Invalid)? };
(spec_case, discriminant_const, try_from, into, try_from_xdr, into_xdr)
(spec_case, discriminant_const, try_from, try_into, try_from_xdr, into_xdr)
}
})
.multiunzip();
Expand Down Expand Up @@ -150,7 +160,7 @@ pub fn derive_type_enum(
impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident {
type Error = #path::ConversionError;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: #path::RawVal) -> Result<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result<Self, Self::Error> {
use #path::TryIntoVal;
#(#discriminant_consts)*
let vec: #path::Vec<#path::RawVal> = val.try_into_val(env)?;
Expand All @@ -163,30 +173,14 @@ pub fn derive_type_enum(
}
}

impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::RawVal {
impl #path::TryFromVal<#path::Env, #enum_ident> for #path::RawVal {
type Error = #path::ConversionError;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

impl #path::IntoVal<#path::Env, #path::RawVal> for #enum_ident {
#[inline(always)]
fn into_val(self, env: &#path::Env) -> #path::RawVal {
#(#discriminant_consts)*
match &self {
#(#intos,)*
}
}
}

impl #path::IntoVal<#path::Env, #path::RawVal> for &#enum_ident {
#[inline(always)]
fn into_val(self, env: &#path::Env) -> #path::RawVal {
fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result<Self, Self::Error> {
use #path::TryIntoVal;
#(#discriminant_consts)*
match self {
#(#intos,)*
match val {
#(#try_intos,)*
}
}
}
Expand All @@ -195,7 +189,7 @@ pub fn derive_type_enum(
impl #path::TryFromVal<#path::Env, #path::xdr::ScVec> for #enum_ident {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: #path::xdr::ScVec) -> Result<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVec) -> Result<Self, Self::Error> {
use #path::xdr::Validate;
use #path::TryIntoVal;

Expand All @@ -211,20 +205,11 @@ pub fn derive_type_enum(
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScVec {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryFromVal<#path::Env, #path::xdr::ScObject> for #enum_ident {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: #path::xdr::ScObject) -> Result<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::xdr::ScObject) -> Result<Self, Self::Error> {
if let #path::xdr::ScObject::Vec(vec) = val {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, vec)
} else {
Expand All @@ -233,20 +218,11 @@ pub fn derive_type_enum(
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScObject {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #enum_ident {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: #path::xdr::ScVal) -> Result<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result<Self, Self::Error> {
if let #path::xdr::ScVal::Object(Some(obj)) = val {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, obj)
} else {
Expand All @@ -255,15 +231,6 @@ pub fn derive_type_enum(
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScVal {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

#[cfg(any(test, feature = "testutils"))]
impl TryInto<#path::xdr::ScVec> for &#enum_ident {
type Error = #path::xdr::Error;
Expand Down
58 changes: 18 additions & 40 deletions soroban-sdk-macros/src/derive_enum_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn derive_type_enum_int(
let mut errors = Vec::<Error>::new();

let variants = &data.variants;
let (spec_cases, try_froms, intos): (Vec<_>, Vec<_>, Vec<_>) = variants
let (spec_cases, try_froms, try_intos): (Vec<_>, Vec<_>, Vec<_>) = variants
.iter()
.map(|v| {
let ident = &v.ident;
Expand Down Expand Up @@ -48,8 +48,8 @@ pub fn derive_type_enum_int(
value: discriminant,
};
let try_from = quote! { #discriminant => Self::#ident };
let into = quote! { #enum_ident::#ident => #discriminant.into_val(env) };
(spec_case, try_from, into)
let try_into = quote! { #enum_ident::#ident => #discriminant.into() };
(spec_case, try_from, try_into)
})
.multiunzip();

Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn derive_type_enum_int(
impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident {
type Error = #path::ConversionError;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: #path::RawVal) -> Result<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result<Self, Self::Error> {
use #path::TryIntoVal;
let discriminant: u32 = val.try_into_val(env)?;
Ok(match discriminant {
Expand All @@ -101,51 +101,29 @@ pub fn derive_type_enum_int(
}
}

impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::RawVal {
impl #path::TryFromVal<#path::Env, #enum_ident> for #path::RawVal {
type Error = #path::ConversionError;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

impl #path::IntoVal<#path::Env, #path::RawVal> for #enum_ident {
#[inline(always)]
fn into_val(self, env: &#path::Env) -> #path::RawVal {
match &self {
#(#intos,)*
}
}
}

impl #path::IntoVal<#path::Env, #path::RawVal> for &#enum_ident {
#[inline(always)]
fn into_val(self, env: &#path::Env) -> #path::RawVal {
match self {
#(#intos,)*
}
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #enum_ident {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: #path::xdr::ScVal) -> Result<Self, Self::Error> {
let discriminant: u32 = val.try_into().map_err(|_| #path::xdr::Error::Invalid)?;
Ok(match discriminant {
#(#try_froms,)*
_ => Err(#path::xdr::Error::Invalid)?,
fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result<Self, Self::Error> {
Ok(match val {
#(#try_intos,)*
})
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScVal {
impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #enum_ident {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result<Self, Self::Error> {
if let #path::xdr::ScVal::U32(discriminant) = val {
Ok(match *discriminant {
#(#try_froms,)*
_ => Err(#path::xdr::Error::Invalid)?,
})
} else {
Err(#path::xdr::Error::Invalid)
}
}
}

Expand Down
26 changes: 5 additions & 21 deletions soroban-sdk-macros/src/derive_error_enum_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,18 @@ pub fn derive_type_error_enum_int(
impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident {
type Error = #path::ConversionError;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: #path::RawVal) -> Result<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result<Self, Self::Error> {
use #path::TryIntoVal;
let status: #path::Status = val.try_into_val(env)?;
status.try_into().map_err(|_| #path::ConversionError)
}
}

impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::RawVal {
impl #path::TryFromVal<#path::Env, #enum_ident> for #path::RawVal {
type Error = #path::ConversionError;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

impl #path::IntoVal<#path::Env, #path::RawVal> for #enum_ident {
#[inline(always)]
fn into_val(self, env: &#path::Env) -> #path::RawVal {
let status: #path::Status = self.into();
status.into_val(env)
}
}

impl #path::IntoVal<#path::Env, #path::RawVal> for &#enum_ident {
#[inline(always)]
fn into_val(self, env: &#path::Env) -> #path::RawVal {
let status: #path::Status = self.into();
status.into_val(env)
fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result<Self, Self::Error> {
let status: #path::Status = val.into();
Ok(status.into())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions soroban-sdk-macros/src/derive_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn derive_fn(
<_ as soroban_sdk::unwrap::UnwrapOptimized>::unwrap_optimized(
<_ as soroban_sdk::TryFromVal<soroban_sdk::Env, soroban_sdk::RawVal>>::try_from_val(
&env,
#ident
&#ident
)
)
};
Expand Down Expand Up @@ -202,7 +202,7 @@ pub fn derive_fn(
#use_trait;
<_ as soroban_sdk::IntoVal<soroban_sdk::Env, soroban_sdk::RawVal>>::into_val(
#[allow(deprecated)]
#call(
&#call(
#env_call
#(#wrap_calls),*
),
Expand Down
Loading

0 comments on commit 4237a60

Please sign in to comment.