Skip to content

Commit

Permalink
feat: add DWN response types
Browse files Browse the repository at this point in the history
  • Loading branch information
enmand committed Oct 29, 2024
1 parent 5783628 commit 5fd5d5c
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/dwn-rs-core/src/interfaces/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod messages;
pub mod replies;

pub use messages::*;
pub use replies::{Reply, Response};
26 changes: 26 additions & 0 deletions crates/dwn-rs-core/src/interfaces/replies/messages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use cid::Cid;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use crate::{Cursor, Descriptor, Message};

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct ReadEntry {
#[serde(rename = "messageCid")]
pub cid: Cid,
pub message: Option<Message<Descriptor>>,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Read {
pub entry: Option<ReadEntry>,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Query {
pub entries: Option<Vec<Cid>>,
pub cursor: Option<Cursor>,
}
42 changes: 42 additions & 0 deletions crates/dwn-rs-core/src/interfaces/replies/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pub mod messages;
pub mod protocols;
pub mod records;

use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use crate::SubscriptionID;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Status {
pub code: i32,
pub detail: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Response {
pub status: Status,
#[serde(flatten)]
pub reply: Reply,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Empty {}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Subscribe {
pub subscription: Option<SubscriptionID>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(untagged)]
pub enum Reply {
Empty(Empty),
RecordsRead(records::Read),
RecordsQuery(records::Query),
MessageRead(messages::Read),
MessageQuery(messages::Query),
ProtocolsQuery(protocols::Query),
Subscribe(Subscribe),
}
10 changes: 10 additions & 0 deletions crates/dwn-rs-core/src/interfaces/replies/protocols.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use crate::{descriptors::protocols, Message};

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Query {
pub entries: Option<Message<protocols::ConfigureDescriptor>>,
}
42 changes: 42 additions & 0 deletions crates/dwn-rs-core/src/interfaces/replies/records.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use crate::{
descriptors::{records::WriteDescriptor, DeleteDescriptor},
Cursor, Message,
};

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct ReadEntry {
#[serde(rename = "recordsWrite")]
pub records_write: Option<Message<WriteDescriptor>>,
#[serde(rename = "recordsDelete")]
pub records_delete: Option<Message<DeleteDescriptor>>,
#[serde(rename = "initialWrite")]
pub initial_write: Option<Message<WriteDescriptor>>,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Read {
pub entry: Option<ReadEntry>,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct QueryEntry {
#[serde(rename = "initialWrite")]
pub initial_write: Option<Message<WriteDescriptor>>,
#[serde(rename = "encodedData")]
pub encoded_data: Option<String>,
#[serde(flatten)]
pub message: Message<WriteDescriptor>,
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Query {
pub entries: Option<Vec<QueryEntry>>,
pub cursor: Option<Cursor>,
}

0 comments on commit 5fd5d5c

Please sign in to comment.