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

feat: Expose FuncVisitor #325

Merged
merged 1 commit into from
Mar 22, 2022
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
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 rust/candid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "candid"
version = "0.7.12"
version = "0.7.13"
edition = "2018"
authors = ["DFINITY Team"]
description = "Candid is an interface description language (IDL) for interacting with canisters running on the Internet Computer."
Expand Down
41 changes: 22 additions & 19 deletions rust/candid/src/types/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,33 @@ impl CandidType for Service {
}
}

/// A [`Visitor`] to extract [`Func`]s.
pub struct FuncVisitor;

impl<'de> Visitor<'de> for FuncVisitor {
type Value = Func;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("Func value")
}
fn visit_byte_buf<E: de::Error>(self, bytes: Vec<u8>) -> Result<Func, E> {
if bytes[0] != 5u8 {
return Err(de::Error::custom("not func"));
}
let mut bytes = &bytes[1..];
let len = leb128::read::unsigned(&mut bytes).map_err(E::custom)? as usize;
let mut buf = vec![0; len];
bytes.read_exact(&mut buf).map_err(E::custom)?;
let method = String::from_utf8(buf).map_err(E::custom)?;
let principal = Principal::try_from(bytes).map_err(E::custom)?;
Ok(Func { principal, method })
}
}

impl<'de> Deserialize<'de> for Func {
fn deserialize<D>(deserializer: D) -> Result<Func, D::Error>
where
D: serde::Deserializer<'de>,
{
struct FuncVisitor;
impl<'de> Visitor<'de> for FuncVisitor {
type Value = Func;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("Func value")
}
fn visit_byte_buf<E: de::Error>(self, bytes: Vec<u8>) -> Result<Func, E> {
if bytes[0] != 5u8 {
return Err(de::Error::custom("not func"));
}
let mut bytes = &bytes[1..];
let len = leb128::read::unsigned(&mut bytes).map_err(E::custom)? as usize;
let mut buf = vec![0; len];
bytes.read_exact(&mut buf).map_err(E::custom)?;
let method = String::from_utf8(buf).map_err(E::custom)?;
let principal = Principal::try_from(bytes).map_err(E::custom)?;
Ok(Func { principal, method })
}
}
deserializer.deserialize_any(FuncVisitor)
}
}
Expand Down