diff --git a/node/Cargo.toml b/node/Cargo.toml index 0c6c9cc9..3d1892e8 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -19,6 +19,7 @@ git-url = { version = "0.3.5", features = ["serde1"] } multibase = { version = "0.9.1" } log = { version = "0.4.17", features = ["std"] } once_cell = { version = "1.13" } +olpc-cjson = { version = "0.1.1" } sha2 = { version = "0.10.2" } serde = { version = "1", features = ["derive"] } serde_json = { version = "1", features = ["preserve_order"] } @@ -28,7 +29,6 @@ nonempty = { version = "0.8.0", features = ["serialize"] } nakamoto-net = { version = "0.3.0" } nakamoto-net-poll = { version = "0.3.0" } thiserror = { version = "1" } -toml = { version = "0.5.9" } [dev-dependencies] quickcheck = { version = "1", default-features = false } diff --git a/node/src/identity.rs b/node/src/identity.rs index f9214c09..ba0713d9 100644 --- a/node/src/identity.rs +++ b/node/src/identity.rs @@ -12,7 +12,7 @@ use crate::hash; use crate::serde_ext; use crate::storage::Remotes; -pub static IDENTITY_PATH: Lazy<&Path> = Lazy::new(|| Path::new("Radicle.toml")); +pub static IDENTITY_PATH: Lazy<&Path> = Lazy::new(|| Path::new("radicle.json")); /// A user's identifier is simply their public key. pub type UserId = crypto::PublicKey; @@ -154,8 +154,8 @@ pub struct Project { #[derive(Error, Debug)] pub enum DocError { - #[error("toml: {0}")] - Toml(#[from] toml::ser::Error), + #[error("json: {0}")] + Json(#[from] serde_json::Error), #[error("i/o: {0}")] Io(#[from] io::Error), } @@ -178,17 +178,25 @@ pub struct Doc { impl Doc { pub fn write(&self, mut writer: W) -> Result { - let buf = toml::to_string_pretty(self)?; - let digest = hash::Digest::new(buf.as_bytes()); + let mut buf = Vec::new(); + let mut ser = + serde_json::Serializer::with_formatter(&mut buf, olpc_cjson::CanonicalFormatter::new()); + + self.serialize(&mut ser)?; + + let digest = hash::Digest::new(&buf); let id = ProjId::from(digest); - writer.write_all(buf.as_bytes())?; + // TODO: Currently, we serialize the document in canonical JSON. This isn't strictly + // necessary, as we could use CJSON just to get the hash, but then write a prettified + // version to disk to make it easier for users to edit. + writer.write_all(&buf)?; Ok(id) } - pub fn from_toml(bytes: &[u8]) -> Result { - toml::from_slice(bytes) + pub fn from_json(bytes: &[u8]) -> Result { + serde_json::from_slice(bytes) } } diff --git a/node/src/storage/git.rs b/node/src/storage/git.rs index ef759ef7..1cad163f 100644 --- a/node/src/storage/git.rs +++ b/node/src/storage/git.rs @@ -216,7 +216,7 @@ impl Repository { Err(e) => return Err(e.into()), Ok(doc) => doc, }; - let doc = identity::Doc::from_toml(doc.content()).unwrap(); + let doc = identity::Doc::from_json(doc.content()).unwrap(); Ok(Some(doc)) }