A helper function format_group_id that formats group IDs for display with a "GROUP-" prefix.
-
Created:
contracts/stellar-save/src/helpers.rs- New module containing the
format_group_idfunction - Includes comprehensive documentation
- Contains 4 unit tests covering edge cases
- New module containing the
-
Modified:
contracts/stellar-save/src/lib.rs- Added
pub mod helpers;declaration - Added
pub use helpers::format_group_id;export
- Added
-
Created:
HELPER_USAGE.md- Usage documentation and examples
Signature:
pub fn format_group_id(env: &Env, group_id: u64) -> StringFeatures:
- Converts numeric group ID to formatted string
- Adds "GROUP-" prefix
- Works in no_std environment (Soroban compatible)
- Handles all u64 values including edge cases
Examples:
format_group_id(&env, 1) // Returns: "GROUP-1"
format_group_id(&env, 42) // Returns: "GROUP-42"
format_group_id(&env, 12345) // Returns: "GROUP-12345"- ✅
test_format_group_id_single_digit- Single digit input - ✅
test_format_group_id_multi_digit- Multi-digit input - ✅
test_format_group_id_zero- Zero edge case - ✅
test_format_group_id_max_value- Maximum u64 value
The function uses Soroban SDK's Bytes type to build the string:
- Converts u64 to individual digit bytes
- Constructs "GROUP-" prefix as bytes
- Concatenates prefix and digits
- Converts to Soroban
String
This approach avoids std library dependencies and works within Soroban's no_std environment.
The code compiles successfully:
cargo check --lib # ✅ No errors in helpers moduleImport and use in your contract:
use stellar_save::format_group_id;
// In any contract function
let formatted = format_group_id(&env, group_id);- The implementation is minimal and efficient
- No external dependencies beyond soroban-sdk
- Fully documented with rustdoc comments
- Ready for integration into contract functions
- Can be used for logging, events, or display purposes