A PLC Mirror written in Rust
9
fork

Configure Feed

Select the types of activity you want to include in your feed.

schemars

Mia eb5551e0 ab111e8a

+54 -22
+2
Cargo.lock
··· 1408 1408 "eyre", 1409 1409 "ipld-core", 1410 1410 "reqwest", 1411 + "schemars", 1411 1412 "serde", 1412 1413 "serde_json", 1413 1414 "slog", ··· 1705 1706 source = "registry+https://github.com/rust-lang/crates.io-index" 1706 1707 checksum = "3fbf2ae1b8bc8e02df939598064d22402220cd5bbcca1c76f7d6a310974d5615" 1707 1708 dependencies = [ 1709 + "chrono", 1708 1710 "dyn-clone", 1709 1711 "schemars_derive", 1710 1712 "serde",
+2 -1
Cargo.toml
··· 10 10 eyre = "0.6.12" 11 11 ipld-core = { version = "0.4.1", features = ["serde"] } 12 12 reqwest = { version = "0.12.12", features = ["native-tls", "json"] } 13 + schemars = { version = "0.8.22", features = ["chrono"] } 13 14 serde = { version = "1.0.217", features = ["derive"] } 14 15 serde_json = "1.0.134" 15 16 slog = { version = "2.7.0" } 16 17 slog-scope = { version = "4.4.0" } 17 18 slog-stdlog = { version = "4.1.1" } 18 - tokio = { version = "1.0", features = [ "full" ] } 19 + tokio = { version = "1.0", features = ["full"] } 19 20 tokio-postgres = { version = "0.7.13", features = ["with-chrono-0_4", "with-serde_json-1"] }
+1 -1
src/import.rs
··· 156 156 pub struct PlcExportEntry { 157 157 pub did: String, 158 158 pub operation: PlcOperationType, 159 - #[serde(deserialize_with = "cid_from_string")] 159 + #[serde(deserialize_with = "cid_de_string")] 160 160 pub cid: Cid, 161 161 pub nullified: bool, 162 162 pub created_at: DateTime<Utc>,
+42 -11
src/types.rs
··· 1 1 use crate::utils::*; 2 2 use ipld_core::cid::Cid; 3 + use schemars::schema::Schema; 4 + use schemars::{JsonSchema, SchemaGenerator}; 3 5 use serde::{Deserialize, Serialize}; 6 + use std::borrow::Cow; 4 7 use std::collections::HashMap; 8 + use std::fmt::{Debug, Display}; 5 9 6 - #[derive(Debug, Deserialize, Serialize)] 10 + #[derive(Debug, JsonSchema, Deserialize, Serialize)] 7 11 #[serde(tag = "type")] 8 12 pub enum PlcOperationType { 9 13 #[serde(rename = "create")] ··· 14 18 Operation(PlcOperation), 15 19 } 16 20 17 - #[derive(Debug, Deserialize, Serialize)] 21 + #[derive(Debug, JsonSchema, Deserialize, Serialize)] 18 22 #[serde(rename_all = "camelCase")] 19 23 pub struct PlcCreate { 20 24 pub sig: String, 21 - #[serde(deserialize_with = "option_cid_from_string")] 22 - pub prev: Option<Cid>, 25 + pub prev: Option<JsonCid>, 23 26 pub signing_key: String, 24 27 pub recovery_key: String, 25 28 pub handle: String, 26 29 pub service: String, 27 30 } 28 31 29 - #[derive(Debug, Deserialize, Serialize)] 32 + #[derive(Debug, JsonSchema, Deserialize, Serialize)] 30 33 #[serde(rename_all = "camelCase")] 31 34 pub struct PlcTombstone { 32 35 pub sig: String, 33 - #[serde(deserialize_with = "option_cid_from_string")] 34 - pub prev: Option<Cid>, 36 + pub prev: Option<JsonCid>, 35 37 } 36 38 37 - #[derive(Debug, Deserialize, Serialize)] 39 + #[derive(Debug, JsonSchema, Deserialize, Serialize)] 38 40 #[serde(rename_all = "camelCase")] 39 41 pub struct PlcOperation { 40 42 pub sig: String, 41 - #[serde(deserialize_with = "option_cid_from_string")] 42 - pub prev: Option<Cid>, 43 + pub prev: Option<JsonCid>, 43 44 pub rotation_keys: Vec<String>, 44 45 pub verification_methods: HashMap<String, String>, 45 46 pub also_known_as: Vec<String>, 46 47 pub services: HashMap<String, PlcService>, 47 48 } 48 49 49 - #[derive(Debug, Deserialize, Serialize)] 50 + #[derive(Debug, JsonSchema, Deserialize, Serialize)] 50 51 pub struct PlcService { 51 52 #[serde(rename = "type")] 52 53 pub ty: String, 53 54 pub endpoint: String, 54 55 } 56 + 57 + #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Deserialize, Serialize)] 58 + pub struct JsonCid(#[serde(deserialize_with = "cid_de_string", serialize_with = "cid_ser_string")] pub Cid); 59 + 60 + // the default cid debugger outputs a byte array, which just sucks to read 61 + impl Debug for JsonCid { 62 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 63 + Display::fmt(&self.0, f) 64 + } 65 + } 66 + 67 + impl JsonCid { 68 + pub fn to_string(&self) -> String { 69 + self.0.to_string() 70 + } 71 + } 72 + 73 + impl JsonSchema for JsonCid { 74 + fn schema_name() -> String { 75 + "Cid".to_string() 76 + } 77 + 78 + fn schema_id() -> Cow<'static, str> { 79 + Cow::Borrowed(concat!(module_path!(), "::Cid")) 80 + } 81 + 82 + fn json_schema(generator: &mut SchemaGenerator) -> Schema { 83 + String::json_schema(generator) 84 + } 85 + }
+7 -9
src/utils.rs
··· 1 1 use ipld_core::cid::Cid; 2 - use serde::{Deserialize, Deserializer}; 2 + use serde::{Deserialize, Deserializer, Serialize, Serializer}; 3 3 4 - pub fn cid_from_string<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Cid, D::Error> { 4 + pub fn cid_de_string<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Cid, D::Error> { 5 5 let str = String::deserialize(deserializer)?; 6 6 7 7 Cid::try_from(str).map_err(serde::de::Error::custom) 8 8 } 9 9 10 - pub fn option_cid_from_string<'de, D: Deserializer<'de>>( 11 - deserializer: D, 12 - ) -> Result<Option<Cid>, D::Error> { 13 - #[derive(Deserialize)] 14 - struct Wrapper(#[serde(deserialize_with = "cid_from_string")] Cid); 15 - 16 - Ok(Option::deserialize(deserializer)?.map(|Wrapper(v)| v)) 10 + pub fn cid_ser_string<S>(inp: &Cid, serializer: S) -> Result<S::Ok, S::Error> 11 + where 12 + S: Serializer, 13 + { 14 + inp.to_string().serialize(serializer) 17 15 }