diff --git a/soroban-sdk-macros/src/derive_struct.rs b/soroban-sdk-macros/src/derive_struct.rs index 697793b13..a35f9d9c5 100644 --- a/soroban-sdk-macros/src/derive_struct.rs +++ b/soroban-sdk-macros/src/derive_struct.rs @@ -6,7 +6,6 @@ use syn::{Attribute, DataStruct, Error, Ident, Path, Visibility}; use stellar_xdr::curr as stellar_xdr; use stellar_xdr::{ ScSpecEntry, ScSpecTypeDef, ScSpecUdtStructFieldV0, ScSpecUdtStructV0, StringM, WriteXdr, - SCSYMBOL_LIMIT, }; use crate::{doc::docs_from_attrs, map_type::map_type, DEFAULT_XDR_RW_LIMITS}; @@ -37,13 +36,13 @@ pub fn derive_type_struct( let field_ident = field.ident.as_ref().unwrap(); let field_name = field_ident.to_string(); let field_idx_lit = Literal::usize_unsuffixed(field_num); - - if field_name.len() > SCSYMBOL_LIMIT as usize { - errors.push(Error::new(field_ident.span(), format!("struct field name is too long: {}, max is {}", field_name.len(), SCSYMBOL_LIMIT))); - } let spec_field = ScSpecUdtStructFieldV0 { doc: docs_from_attrs(&field.attrs).try_into().unwrap(), // TODO: Truncate docs, or display friendly compile error. - name: field_name.clone().try_into().unwrap_or_else(|_| StringM::default()), + name: field_name.clone().try_into().unwrap_or_else(|_| { + const MAX: u32 = 30; + errors.push(Error::new(field_ident.span(), format!("struct field name is too long: {}, max is {MAX}", field_name.len()))); + StringM::::default() + }), type_: match map_type(&field.ty) { Ok(t) => t, Err(e) => { diff --git a/soroban-sdk/src/tests/contract_udt_struct.rs b/soroban-sdk/src/tests/contract_udt_struct.rs index 32aeaa29e..f602b5ec4 100644 --- a/soroban-sdk/src/tests/contract_udt_struct.rs +++ b/soroban-sdk/src/tests/contract_udt_struct.rs @@ -18,7 +18,7 @@ pub struct Udt { #[derive(Copy, Clone, Debug, Eq, PartialEq)] #[contracttype] pub struct UdtWithLongName { - pub this_is_a_very_long_name_1234567: u64, + pub this_is_a_very_long_name_12345: u64, } #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -40,7 +40,7 @@ impl Contract { } pub fn add_udt_with_long_name(a: UdtWithLongName, b: UdtWithLongName) -> u64 { - a.this_is_a_very_long_name_1234567 + b.this_is_a_very_long_name_1234567 + a.this_is_a_very_long_name_12345 + b.this_is_a_very_long_name_12345 } } @@ -61,10 +61,10 @@ fn test_long_names_functional() { let contract_id = env.register_contract(None, Contract); let a = UdtWithLongName { - this_is_a_very_long_name_1234567: 1_000_000_000_000, + this_is_a_very_long_name_12345: 1_000_000_000_000, }; let b = UdtWithLongName { - this_is_a_very_long_name_1234567: 5_000_000_000_000, + this_is_a_very_long_name_12345: 5_000_000_000_000, }; assert_eq!( ContractClient::new(&env, &contract_id).add_udt_with_long_name(&a, &b), diff --git a/soroban-sdk/test_snapshots/tests/contract_udt_struct/test_long_names_functional.1.json b/soroban-sdk/test_snapshots/tests/contract_udt_struct/test_long_names_functional.1.json index 842f2b3e7..06d7b3642 100644 --- a/soroban-sdk/test_snapshots/tests/contract_udt_struct/test_long_names_functional.1.json +++ b/soroban-sdk/test_snapshots/tests/contract_udt_struct/test_long_names_functional.1.json @@ -96,7 +96,7 @@ "map": [ { "key": { - "symbol": "this_is_a_very_long_name_1234567" + "symbol": "this_is_a_very_long_name_12345" }, "val": { "u64": 1000000000000 @@ -108,7 +108,7 @@ "map": [ { "key": { - "symbol": "this_is_a_very_long_name_1234567" + "symbol": "this_is_a_very_long_name_12345" }, "val": { "u64": 5000000000000