Skip to content

Commit

Permalink
Merge branch 'main' into rust-add-schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored May 14, 2024
2 parents 3a12f34 + 204ccb8 commit 315c0b3
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage = "https://github.com/stellar/rs-stellar-xdr"
repository = "https://github.com/stellar/rs-stellar-xdr"
authors = ["Stellar Development Foundation <[email protected]>"]
license = "Apache-2.0"
version = "20.1.0"
version = "21.0.1"
edition = "2021"
rust-version = "1.74.0"

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CARGO_DOC_ARGS?=--open

XDRGEN_VERSION=736398401ba08dfa8f5fcb577c1d7ed935cc51de
# XDRGEN_LOCAL=1
XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12
XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12
XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12,ClaimableBalanceId
XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12,ClaimableBalanceId
XDRGEN_TYPES_CUSTOM_JSONSCHEMA_IMPL_CURR=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12
XDRGEN_TYPES_CUSTOM_JSONSCHEMA_IMPL_NEXT=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,23 @@ stellar-xdr decode --type BucketEntry --input stream-framed --output json-format
```

License: Apache-2.0


### For Developers: How to Regenerate From XDR
To regenerate types from XDR definitions
1. Update XDR definitions
```concole
git submodule update --init --remote
```
The `--init` flag is only required for the first time setting up the local project.
`--remote` flag will make sure to fetch the latest changes from from the remote-tracking branches `curr` and `next` at [stellar/stellar-xdr].

**_NOTE:_** if you had multiple remotes specified in the submodules (e.g. one tracking `stellar/stellar-xdr`, the other tracking `your-fork/stellar-xdr`),
make sure the remote that tracks [stellar/stellar-xdr] match with what's specifies in the `.git/config` or `.gitsubmodules` (with `.git/config` taking precedence. If neither file specifies it, then `origin` is used).

2. Recompile and test
```console
make
```

When the regenerated types are ready to be merged, make sure to commit the regenerated code file `src/curr/generated.rs`, `src/next/generated.rs`, the version string file `xdr/curr-version`, `xdr/next-version`, as well as the submodule files `xdr/curr`, `xdr/next`.
3 changes: 1 addition & 2 deletions src/curr/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14750,8 +14750,7 @@ impl WriteXdr for ClaimableBalanceIdType {
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(
all(feature = "serde", feature = "alloc"),
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[allow(clippy::large_enum_variant)]
Expand Down
45 changes: 43 additions & 2 deletions src/curr/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
//# - AssetCode
//# - AssetCode4
//# - AssetCode12
//#
//# ## Other
//# - ClaimableBalanceId
#![cfg(feature = "alloc")]

use super::{
AccountId, AssetCode, AssetCode12, AssetCode4, Error, Hash, MuxedAccount, MuxedAccountMed25519,
NodeId, PublicKey, ScAddress, SignerKey, SignerKeyEd25519SignedPayload, Uint256,
AccountId, AssetCode, AssetCode12, AssetCode4, ClaimableBalanceId, Error, Hash, MuxedAccount,
MuxedAccountMed25519, NodeId, PublicKey, ScAddress, SignerKey, SignerKeyEd25519SignedPayload,
Uint256,
};

impl From<stellar_strkey::DecodeError> for Error {
Expand Down Expand Up @@ -336,3 +340,40 @@ impl core::fmt::Display for AssetCode {
}
}
}

impl core::str::FromStr for ClaimableBalanceId {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
// This conversion to a hex string could be done by XDR encoding the
// self value, but because XDR encoding requires the std feature, this
// approach is taken instead to preserve the fact that the serde feature
// is available with alloc only.
let bytes = hex::decode(s).map_err(|_| Error::InvalidHex)?;
match bytes.as_slice() {
[0, 0, 0, 0, ..] => Ok(ClaimableBalanceId::ClaimableBalanceIdTypeV0(Hash(
(&bytes[4..]).try_into()?,
))),
_ => Err(Error::Invalid),
}
}
}

impl core::fmt::Display for ClaimableBalanceId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// This conversion from a hex string could be done by XDR decoding the
// self value, but because XDR decoding requires the std feature, this
// approach is taken instead to preserve the fact that the serde feature
// is available with alloc only.
match self {
ClaimableBalanceId::ClaimableBalanceIdTypeV0(Hash(bytes)) => {
for b in [0u8, 0, 0, 0] {
write!(f, "{b:02x}")?;
}
for b in bytes {
write!(f, "{b:02x}")?;
}
}
}
Ok(())
}
}
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
//! 1. `base64` – Enables support for base64 encoding and decoding.
//! 2. `serde` – Enables support for serializing and deserializing types with
//! the serde crate.
//! 3. `arbitrary` – Enables support for interop with the arbitrary crate.
//! 3. `serde_json` – Enables support for built-in functionality specifically
//! for serde_json. Often not required to use the types with serde_json, and
//! only necessary to use utility functions that depend on serde_json.
//! 4. `arbitrary` – Enables support for interop with the arbitrary crate.
//! 5. `hex` – Enables support for hex in string representations of some types.
//! Automatically enabled when serde is enabled.
//!
//! Channels of XDR:
//!
Expand Down
3 changes: 1 addition & 2 deletions src/next/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14750,8 +14750,7 @@ impl WriteXdr for ClaimableBalanceIdType {
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(
all(feature = "serde", feature = "alloc"),
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "snake_case")
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[allow(clippy::large_enum_variant)]
Expand Down
45 changes: 43 additions & 2 deletions src/next/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
//# - AssetCode
//# - AssetCode4
//# - AssetCode12
//#
//# ## Other
//# - ClaimableBalanceId
#![cfg(feature = "alloc")]

use super::{
AccountId, AssetCode, AssetCode12, AssetCode4, Error, Hash, MuxedAccount, MuxedAccountMed25519,
NodeId, PublicKey, ScAddress, SignerKey, SignerKeyEd25519SignedPayload, Uint256,
AccountId, AssetCode, AssetCode12, AssetCode4, ClaimableBalanceId, Error, Hash, MuxedAccount,
MuxedAccountMed25519, NodeId, PublicKey, ScAddress, SignerKey, SignerKeyEd25519SignedPayload,
Uint256,
};

impl From<stellar_strkey::DecodeError> for Error {
Expand Down Expand Up @@ -325,3 +329,40 @@ impl core::fmt::Display for AssetCode {
}
}
}

impl core::str::FromStr for ClaimableBalanceId {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
// This conversion to a hex string could be done by XDR encoding the
// self value, but because XDR encoding requires the std feature, this
// approach is taken instead to preserve the fact that the serde feature
// is available with alloc only.
let bytes = hex::decode(s).map_err(|_| Error::InvalidHex)?;
match bytes.as_slice() {
[0, 0, 0, 0, ..] => Ok(ClaimableBalanceId::ClaimableBalanceIdTypeV0(Hash(
(&bytes[4..]).try_into()?,
))),
_ => Err(Error::Invalid),
}
}
}

impl core::fmt::Display for ClaimableBalanceId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// This conversion from a hex string could be done by XDR decoding the
// self value, but because XDR decoding requires the std feature, this
// approach is taken instead to preserve the fact that the serde feature
// is available with alloc only.
match self {
ClaimableBalanceId::ClaimableBalanceIdTypeV0(Hash(bytes)) => {
for b in [0u8, 0, 0, 0] {
write!(f, "{b:02x}")?;
}
for b in bytes {
write!(f, "{b:02x}")?;
}
}
}
Ok(())
}
}
26 changes: 24 additions & 2 deletions tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
use stellar_xdr::curr as stellar_xdr;

use stellar_xdr::{
AccountId, AssetCode, AssetCode12, AssetCode4, Error, Hash, MuxedAccount, MuxedAccountMed25519,
NodeId, PublicKey, ScAddress, SignerKey, SignerKeyEd25519SignedPayload, Uint256,
AccountId, AssetCode, AssetCode12, AssetCode4, ClaimableBalanceId, Error, Hash, MuxedAccount,
MuxedAccountMed25519, NodeId, PublicKey, ScAddress, SignerKey, SignerKeyEd25519SignedPayload,
Uint256,
};

use std::str::FromStr;
Expand Down Expand Up @@ -541,3 +542,24 @@ fn asset_code_from_str_to_string_roundtrip_unicode() {
assert_eq!(AssetCode::CreditAlphanum4(AssetCode4(*b"a\xc3\xc3d")).to_string(), r"a\xc3\xc3d");
assert_eq!(AssetCode::from_str(r"a\xc3\xc3d"), Ok(AssetCode::CreditAlphanum4(AssetCode4(*b"a\xc3\xc3d"))));
}

#[test]
#[rustfmt::skip]
fn claimable_balance_id() {
assert_eq!(
ClaimableBalanceId::ClaimableBalanceIdTypeV0(Hash([1u8; 32])).to_string(),
"000000000101010101010101010101010101010101010101010101010101010101010101",
);
// Valid
assert_eq!(ClaimableBalanceId::from_str("000000000101010101010101010101010101010101010101010101010101010101010101"), Ok(ClaimableBalanceId::ClaimableBalanceIdTypeV0(Hash([1u8; 32]))));
// Half byte short.
assert_eq!(ClaimableBalanceId::from_str("00000000010101010101010101010101010101010101010101010101010101010101010"), Err(Error::InvalidHex));
// Full byte short.
assert_eq!(ClaimableBalanceId::from_str("0000000001010101010101010101010101010101010101010101010101010101010101"), Err(Error::LengthMismatch));
// Half byte too long.
assert_eq!(ClaimableBalanceId::from_str("0000000001010101010101010101010101010101010101010101010101010101010101011"), Err(Error::InvalidHex));
// Full byte too long.
assert_eq!(ClaimableBalanceId::from_str("00000000010101010101010101010101010101010101010101010101010101010101010101"), Err(Error::LengthMismatch));
// Unrecognized discriminant value.
assert_eq!(ClaimableBalanceId::from_str("000000010101010101010101010101010101010101010101010101010101010101010101"), Err(Error::Invalid));
}
2 changes: 1 addition & 1 deletion xdr/curr
Submodule curr updated 0 files
2 changes: 1 addition & 1 deletion xdr/curr-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
59062438237d5f77fd6feb060b76288e88b7e222
1a04392432dacc0092caaeae22a600ea1af3c6a5
2 changes: 1 addition & 1 deletion xdr/next
Submodule next updated 0 files
2 changes: 1 addition & 1 deletion xdr/next-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9b19baf22b12d93e6a1a6eb05cd1586c8658a967
4bd4827b3361f266d295cbc83c5d7f7396d782d9

0 comments on commit 315c0b3

Please sign in to comment.