-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: borsh and json schemas support #43
Changes from 9 commits
3430691
e83e1c7
30c6640
3dad2fe
3834421
cc4fe0a
7d4b43d
e08a4bd
80997f6
2c29416
bad09a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: test borsh | ||
run-name: ${{ github.actor }}'s patch | ||
on: [push] | ||
jobs: | ||
build-and-test: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions-rust-lang/setup-rust-toolchain@v1 | ||
with: | ||
cache: true | ||
toolchain: nightly | ||
- run: | | ||
cargo test --no-default-features --features=borsh | ||
cargo test --no-default-features --features=borsh,std |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stable |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1911,3 +1911,55 @@ fn serde() { | |
"invalid value: integer `-1`, expected u128", | ||
); | ||
} | ||
|
||
#[cfg(all(feature = "borsh", feature = "std"))] | ||
dzmitry-lahoda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[test] | ||
fn borsh() { | ||
dzmitry-lahoda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use borsh::schema::BorshSchemaContainer; | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
let mut buf = Vec::new(); | ||
let input = u9::new(42); | ||
input.serialize(&mut buf).unwrap(); | ||
let output = u9::deserialize(&mut buf.as_ref()).unwrap(); | ||
assert_eq!(buf.len(), (u9::BITS + 7) / 8); | ||
dzmitry-lahoda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_eq!(input, output); | ||
|
||
let input = u63::MAX; | ||
dzmitry-lahoda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let mut buf = Vec::new(); | ||
input.serialize(&mut buf).unwrap(); | ||
let output: u63 = u63::deserialize(&mut buf.as_ref()).unwrap(); | ||
assert_eq!(buf.len(), (u63::BITS + 7) / 8); | ||
assert_eq!(input, output); | ||
|
||
let schema = BorshSchemaContainer::for_type::<u9>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to understand this: BorschSchemaContainer does NOT require schemars? So we have two different things that are called schema? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One schema is JSON/OpenAPI shema integrated with serde. Other shema is borshschema integrated with borsh. There are schemas for protobuf/scale/etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not require, just supporting several schemas |
||
match schema.get_definition("u9").expect("exists") { | ||
borsh::schema::Definition::Primitive(2) => {} | ||
_ => panic!("unexpected schema"), | ||
} | ||
} | ||
|
||
#[cfg(feature = "schemars")] | ||
#[test] | ||
fn schemars() { | ||
use schemars::schema_for; | ||
let mut u8 = schema_for!(u8); | ||
let u9 = schema_for!(u9); | ||
assert_eq!( | ||
u8.schema.format.clone().unwrap().replace("8", "9"), | ||
u9.schema.format.clone().unwrap() | ||
); | ||
u8.schema.format = u9.schema.format.clone(); | ||
assert_eq!( | ||
u8.schema | ||
.metadata | ||
.clone() | ||
.unwrap() | ||
.title | ||
.unwrap() | ||
.replace("8", "9"), | ||
u9.schema.metadata.clone().unwrap().title.unwrap() | ||
); | ||
u8.schema.metadata = u9.schema.metadata.clone(); | ||
u8.schema.number = u9.schema.number.clone(); | ||
assert_eq!(u8, u9); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified:
No need for the expect, try_into() etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value is not primitive, so 'as' does not work as i tried
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could use https://docs.rs/num/latest/num/traits/trait.AsPrimitive.html , but do not want to force num traits usage