Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and serialize most Message kinds #37

Merged
merged 5 commits into from
Dec 8, 2023
Merged

Create and serialize most Message kinds #37

merged 5 commits into from
Dec 8, 2023

Conversation

diehuxx
Copy link

@diehuxx diehuxx commented Dec 7, 2023

Implements Close, Quote, Order, and OrderStatus messages as currently defined in the spec https://github.com/TBD54566975/tbdex/tree/main/specs/protocol.

Copy link
Contributor

@amika-sq amika-sq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few minor comments, but absolutely nothing blocking. Awesome quick work on these!


assert_eq!(
close.data.reason,
Some("I don't want to do business with you any more".to_string())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣 love it!

Comment on lines 45 to 47
/// Current status of Order that's being executed

order_status: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: empty line between comment & field can be removed

Comment on lines +41 to +44
pub struct CloseData {
/// an explanation of why the exchange is being closed/completed
reason: Option<String>,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized, we might want to not serialize empty fields. I'm not sure if this behavior is defined in the current tbDEX spec or not. On first glance, I don't think there's been a decision made.

Regardless, there's two ways I know of where we could skip serializing empty optional fields:

  • Annotate the optional field with #[serde(skip_serializing_if = "Option::is_none")]
  • Use the serde_with crate, which comes with a nice #[skip_serializing_none] macro

For reference, we're using the serde_with crate over in web5-rs, as it makes the code a little bit less messy, and maintains the readability a bit nicer.

Don't think we should take action on this right now, but I'll go ahead and make a task in the backlog for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added task here: #38

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! IMO we should not serialize None fields. I pushed a commit adding serde_with since it was a pretty easy additional. If we change our minds I'll strip it out.

Copy link
Contributor

@amika-sq amika-sq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

/// ISO 3166 currency code string
pub currency_code: String,
/// The amount of currency expressed in the smallest respective unit
pub amount_subunits: String,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elsewhere, should the struct Deserialize the json into types specific to the semantics? For example, see below, where Decimal comes from the crate described in https://crates.io/crates/rust_decimal.

Suggested change
pub amount_subunits: String,
pub amount_subunits: Decimal,

If not, where should that parsing and validation be done?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the spec defines them as strings here: https://github.com/TBD54566975/tbdex/tree/main/specs/protocol
We also define them as strings in Javascript & Kotlin

Definitely open to changing, but we might want to modify the spec if this is something we want to do. For now, should be fine though because it meets the existing spec!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say this is a language dependent matter. The spec mentions it's an amount, which I agree should be further specified. But we can likely accept that amount means that it's a number. So a value of "Not A Number" shouldn't be possible in this message.

Thinking from a consumers perspective, it would sure be nice that when using the library, I can only put values that are valid. Even better would be that any mistakes are caught at compile time, instead of at runtime.

I'm going to create two tickets for this. One to clarify the spec, the second to have more discussion around this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. One thing I do want to mention though is that tbDEX messages/resources will need to traverse the language boundary from Rust -> other foreign languages. Strings work really well in this regard, third-party crate types not so much.

It's definitely going to be some tradeoffs, but I think we can find a happy medium here. Looking forward to discussing this more!

Copy link
Author

@diehuxx diehuxx Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andresuribe87 There's ongoing discussion about currency amount representation in TBD54566975/tbdex#199. For now I'm going to stick with plain ol strings until we make a decision in the spec.

@diehuxx diehuxx merged commit f5c9371 into main Dec 8, 2023
1 check failed
@diehuxx diehuxx deleted the message-kinds-1 branch December 8, 2023 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants