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: add Codec trait #5

Merged
merged 4 commits into from
Mar 14, 2024
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.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories = ["data-structures", "encoding"]

[features]
default = ["std"]
# Makes the error implement `std::error::Error`.
# Makes the error implement `std::error::Error` and the `Codec` trait available.
std = ["cid/std", "serde?/std"]
# Enables support for Serde serialization into/deserialization from the `Ipld` enum.
serde = ["dep:serde", "cid/serde-codec"]
Expand Down
43 changes: 43 additions & 0 deletions src/codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! This module contains traits to have a unified API across codecs.
//!
//! There are two traits defined, [`Codec`] and [`Links`]. Those are separate traits as the `Links`
//! trait is not generic over a certain type.

use cid::Cid;

use std::io::{BufRead, Write};

/// Each IPLD codec implementation should implement this Codec trait. This way codecs can be more
/// easily exchanged or combined.
pub trait Codec<T>: Links {
/// The multicodec code of the IPLD codec.
const CODE: u64;
Copy link
Member

@rvagg rvagg Mar 15, 2024

Choose a reason for hiding this comment

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

I think we're going to want NAME eventually too, it's pretty helpful. This trait is almost the same as the interfaces we landed on in JS, although we ended up splitting encode and decode operations into two parts and combining them into a single BlockCodec: https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't want the name. People should really use the constants as identifiers and not strings that might change.

Splitting encode and decode: I'd keep it simple for now, if the need of splitting ever occurs (which I currently doubt), we can do it then.

/// The error that is returned if encoding or decoding fails.
type Error;

/// Decode a reader into the desired type.
fn decode<R: BufRead>(reader: R) -> Result<T, Self::Error>;
/// Encode a type into a writer.
fn encode<W: Write>(writer: W, data: &T) -> Result<(), Self::Error>;

/// Decode a slice into the desired type.
fn decode_from_slice(bytes: &[u8]) -> Result<T, Self::Error> {
Self::decode(bytes)
}

/// Encode a type into bytes.
fn encode_to_vec(data: &T) -> Result<Vec<u8>, Self::Error> {
let mut output = Vec::new();
Self::encode(&mut output, data)?;
Ok(output)
}
}

/// Trait for returning the links of a serialized IPLD data.
pub trait Links {
Copy link
Contributor

Choose a reason for hiding this comment

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

not Links<T>?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I wonder if we want a relationship with Codec<T>? Are there any codecs where (a) we support links but (b) don't actually support enumerating them?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a separate trait, so it doesn't have to be <T>. The idea is that you can extract links out of the encoded data. If it would be <T>, then it could/would be part of the Codec trait.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, makes sense. You don't care about T.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm.

  1. It's kind of odd that Links doesn't have the code. But I don't have a great solution.
  2. It would be kind of nice to have Codec<T>: Links. That way if I take a codec as C: Codec<T> I also, get links.

/// The error that is returned if the link extraction fails.
type LinksError;

/// Return all links (CIDs) that the given encoded data contains.
fn links(bytes: &[u8]) -> Result<impl Iterator<Item = Cid>, Self::LinksError>;
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

extern crate alloc;

#[cfg(feature = "std")]
pub mod codec;
pub mod convert;
pub mod ipld;
#[cfg(feature = "serde")]
Expand Down