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

Add parity-scale-codec and borsh feature for Any #48

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add parity-scale-codec and borsh for Any ([#47](https://github.com/cosmos/ibc-
proto-rs/issues/47))
17 changes: 17 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,20 @@ jobs:
with:
command: test
args: --all-features --no-fail-fast --workspace -- --nocapture

test-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- uses: Swatinem/rust-cache@v1
- uses: actions-rs/cargo@v1
with:
command: build
args: --features=borsh,parity-scale-codec
- uses: actions-rs/cargo@v1
with:
command: build
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ schemars = { version = "0.8", optional = true }
subtle-encoding = { version = "0.5", default-features = false }
base64 = { version = "0.13", default-features = false, features = ["alloc"] }
flex-error = { version = "0.4.4", default-features = false }
## for codec encode or decode
parity-scale-codec = { version = "3.0.0", default-features = false, features = ["full"], optional = true }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"], optional = true }
## for borsh encode or decode
borsh = {version = "0.9.3", default-features = false, optional = true }

[dependencies.tendermint-proto]
version = "0.28"
Expand All @@ -41,3 +46,5 @@ std = []
client = ["std", "tonic", "tonic/codegen", "tonic/transport", "tonic/prost"]
json-schema = ["std", "schemars"]
server = ["std", "tonic", "tonic/codegen", "tonic/transport", "tonic/prost"]
parity-scale-codec = ["dep:parity-scale-codec", "dep:scale-info"]
borsh = ["dep:borsh"]
91 changes: 91 additions & 0 deletions src/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,95 @@ pub mod protobuf {
})
}
}

#[cfg(any(feature = "borsh", feature = "parity-scale-codec"))]
mod sealed {
use super::Any;

use alloc::string::String;
use alloc::vec::Vec;

#[cfg_attr(
feature = "parity-scale-codec",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
scale_info::TypeInfo
)
)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
struct InnerAny {
pub type_url: String,
pub value: Vec<u8>,
}

#[cfg(feature = "borsh")]
impl borsh::BorshSerialize for Any {
fn serialize<W: borsh::maybestd::io::Write>(
&self,
writer: &mut W,
) -> borsh::maybestd::io::Result<()> {
let inner_any = InnerAny {
type_url: self.type_url.clone(),
value: self.value.clone(),
};

borsh::BorshSerialize::serialize(&inner_any, writer)
}
}

#[cfg(feature = "borsh")]
impl borsh::BorshDeserialize for Any {
fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
let inner_any = InnerAny::deserialize(buf)?;

Ok(Any {
type_url: inner_any.type_url,
value: inner_any.value,
})
}
}

#[cfg(feature = "parity-scale-codec")]
impl parity_scale_codec::Encode for Any {
fn encode_to<T: parity_scale_codec::Output + ?Sized>(&self, writer: &mut T) {
let inner_any = InnerAny {
type_url: self.type_url.clone(),
value: self.value.clone(),
};
inner_any.encode_to(writer);
}
}
#[cfg(feature = "parity-scale-codec")]
impl parity_scale_codec::Decode for Any {
fn decode<I: parity_scale_codec::Input>(
input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
let inner_any = InnerAny::decode(input)?;
Ok(Any {
type_url: inner_any.type_url.clone(),
value: inner_any.value,
})
}
}

#[cfg(feature = "parity-scale-codec")]
impl scale_info::TypeInfo for Any {
type Identity = Self;

fn type_info() -> scale_info::Type {
scale_info::Type::builder()
.path(scale_info::Path::new("HeaderAttribute", module_path!()))
// i128 is chosen before we represent the timestamp is nanoseconds, which is represented as a i128 by Time
.composite(
scale_info::build::Fields::named()
.field(|f| f.ty::<String>().name("type_url").type_name("String"))
.field(|f| f.ty::<Vec<u8>>().name("value").type_name("Vec<u8>")),
)
}
}
}
}