Skip to content

Commit

Permalink
cas types changes
Browse files Browse the repository at this point in the history
  • Loading branch information
seanses committed Sep 14, 2024
2 parents 4233860 + 2f7116a commit 8ed3347
Show file tree
Hide file tree
Showing 16 changed files with 5,048 additions and 30 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.idea
**/.idea
# Generated by Cargo
# will have compiled files and executables
debug/
target/
**/target/

# These are backup files generated by rustfmt
**/*.rs.bk
Expand All @@ -15,3 +15,4 @@ target/

# VS Code configs
.vscode
venv
36 changes: 17 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ members = [
"xet_error",
]

exclude = [
"hf_xet",
]

[profile.release]
opt-level = 3
lto = true
Expand Down
78 changes: 77 additions & 1 deletion cas_types/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,80 @@ impl Display for Key {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/{:x}", self.prefix, self.hash)
}
}
}

mod hex {
pub mod serde {
use merklehash::MerkleHash;
use serde::de::{self, Visitor};
use serde::{Deserializer, Serializer};
use std::fmt;

pub fn serialize<S>(value: &MerkleHash, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let hex = value.hex();
serializer.serialize_str(&hex)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<MerkleHash, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(HexVisitor)
}

// Visitor for deserialization
struct HexVisitor;

impl<'de> Visitor<'de> for HexVisitor {
type Value = MerkleHash;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a merklehash")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
MerkleHash::from_hex(v).map_err(|e| serde::de::Error::custom(e))
}
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HexMerkleHash(#[serde(with = "hex::serde")] pub MerkleHash);

impl From<MerkleHash> for HexMerkleHash {
fn from(value: MerkleHash) -> Self {
HexMerkleHash(value)
}
}

impl From<HexMerkleHash> for MerkleHash {
fn from(value: HexMerkleHash) -> Self {
value.0
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HexKey {
pub prefix: String,
#[serde(with = "hex::serde")]
pub hash: MerkleHash,
}

impl From<HexKey> for Key {
fn from(HexKey { prefix, hash }: HexKey) -> Self {
Key { prefix, hash }
}
}

impl From<Key> for HexKey {
fn from(Key { prefix, hash }: Key) -> Self {
HexKey { prefix, hash }
}
}
3 changes: 2 additions & 1 deletion cas_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ pub struct Range {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CASReconstructionTerm {
pub hash: String,
pub hash: HexMerkleHash,
pub unpacked_length: u64,
pub range: Range,
pub url: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod clean;
pub mod configurations;
mod constants;
mod data_processing;
mod errors;
pub mod errors;
mod metrics;
mod pointer_file;
mod remote_shard_interface;
Expand Down
2 changes: 1 addition & 1 deletion data/src/pointer_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const CURRENT_VERSION: &str = "0";
/// A struct that wraps a Xet pointer file.
/// Xet pointer file format is a TOML file,
/// and the first line must be of the form "# xet version <x.y>"
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PointerFile {
/// The version string of the pointer file
version_string: String,
Expand Down
Loading

0 comments on commit 8ed3347

Please sign in to comment.