-
Notifications
You must be signed in to change notification settings - Fork 18
feat: trait for generating cbor bytes and cids (part 1) #477
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
Conversation
c660f7a to
5866b60
Compare
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.
IMO using a proc macro for this is overkill (plus it won't work for lifetimes/generics)... a simple derive macro would work and makes it simpler to understand... e.g. this with the error type as a param if desired
#[derive(serde::Serialize, serde::Deserialize)]
struct Dog {
s: String,
}
macro_rules! derive_serde_ipld {
($name: ident, $err: ty) => {
impl $name {
/// serde serialize to dag-json
pub fn to_json(&self) -> Result<String, Box<$err> {
let json_bytes = serde_ipld_dagjson::to_vec(self)?;
String::from_utf8(json_bytes).map_err(Into::into)
}
/// serde serialize to dag-cbor
pub fn to_cbor(&self) -> Result<Vec<u8>, $err> {
serde_ipld_dagcbor::to_vec(self).map_err(Into::into)
}
/// CID of serde serialize to dag-cbor
pub fn to_cid(&self) -> Result<Cid, $err> {
let cbor_bytes = self.to_cbor()?;
use multihash_codetable::MultihashDigest;
let hash = multihash_codetable::Code::Sha2_256.digest(&cbor_bytes);
Ok(Cid::new_v1(0x71, hash))
}
}
};
}
derive_serde_ipld!(Dog, Box<dyn std::error::Error>);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 feels like too much complexity to abstract away calling serde_ipld_* crate methods.
I won't push back super hard but this feels like a lot of code to maintain for a little ergonomic benefit.
Maybe just a crate with helper methods that take an impl of serde::Serialize and serde::Deserialize is all we need?
59d2b2b to
bbe3482
Compare
bbe3482 to
1e57390
Compare
nathanielc
left a comment
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.
Overall really like how this has simplified the code. Really good use of a trait. With some consensus on the name this should be good to go
core/src/serialize_ext.rs
Outdated
| } | ||
|
|
||
| impl<T: Serialize> SerializeExt for T { | ||
| type Error = anyhow::Error; |
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.
Why are we using anyhow here?
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.
It looks like the ipld crates use two different error types and don't just use a serde error, so we need to provide something that matches both. Should we use an error that passes through the variants or is using anyhow okay?
// ipld_dag_json
#[derive(Debug)]
pub enum EncodeError {
Message(String),
}
// ipld_dag_cbor
#[derive(Debug)]
pub enum EncodeError<E> {
/// Custom error message.
Msg(String),
/// IO Error.
Write(E),
}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.
Lets use thiserror and create a variant for each type of error that can be produced. Thiserror make this very easy. With this approach we expose the underlying errors which is what we want in the context of a library trait.
bf03e06 to
05cc7d5
Compare
core/src/serialize_ext.rs
Outdated
| /// SerializeExt is a trait for serialization, deserialization, CID calculation assuming dag-cbor. | ||
| pub trait SerializeExt: Serialize { | ||
| /// Error type for SerializeExt | ||
| // type Error; |
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.
nit, remove these comments.
a5b2338 to
949c02e
Compare
No description provided.