General types (#1)

* Added JsonResponse type

* Add dependencies

* Added wakumessage

* Adjust go build to link with missing simbols on osx

* Added general types

* Nitpick in docs
This commit is contained in:
Daniel Sanchez 2022-09-28 16:03:47 +02:00 committed by GitHub
parent 7ce8cadaa5
commit 7b7ba2975d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 192 additions and 3 deletions

56
Cargo.lock generated
View File

@ -163,6 +163,12 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -281,6 +287,43 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "serde"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "shlex"
version = "1.1.0"
@ -293,6 +336,17 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52205623b1b0f064a4e71182c3b18ae902267282930c6d5462c91b859668426e"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "termcolor"
version = "1.1.3"
@ -318,6 +372,8 @@ checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
name = "waku"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
"waku-sys",
]

View File

@ -3,6 +3,9 @@ name = "waku-sys"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["rlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -1 +1 @@
Subproject commit b6ff0386129fcbc34c04f08ac45ea3e19cfd3e44
Subproject commit 5b42c98780fe938ff79c54229aa80cdd47a17568

View File

@ -6,4 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
waku-sys = { path = "../waku-sys" }
waku-sys = { path = "../waku-sys" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

128
waku/src/general/mod.rs Normal file
View File

@ -0,0 +1,128 @@
// std
// crates
use serde::{Deserialize, Serialize};
// internal
/// JsonResponse wrapper.
/// `go-waku` ffi returns this type as a `char *` as per the [specification](https://rfc.vac.dev/spec/36/#jsonresponse-type)
/// This is internal, as it is better to use rust plain `Result` type.
#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum JsonResponse<T> {
Result(T),
Error(String),
}
/// Waku response, just a `Result` with an `String` error.
/// Convenient we can transform a [`JsonResponse`] into a [`Response`] (`Result`)
type Response<T> = Result<T, String>;
impl<T> From<JsonResponse<T>> for Response<T> {
fn from(response: JsonResponse<T>) -> Self {
match response {
JsonResponse::Result(t) => Ok(t),
JsonResponse::Error(e) => Err(e),
}
}
}
/// JsonMessage, Waku message in JSON format.
/// as per the [specification](https://rfc.vac.dev/spec/36/#jsonmessage-type)
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WakuMessage {
payload: Box<[u8]>,
/// The content topic to be set on the message
content_topic: String,
/// The Waku Message version number
version: usize,
/// Unix timestamp in nanoseconds
timestamp: usize,
}
/// A payload once decoded, used when a received Waku Message is encrypted
pub struct DecodedPayload {
/// Public key that signed the message (optional), hex encoded with 0x prefix
public_key: Option<String>,
/// Message signature (optional), hex encoded with 0x prefix
signature: Option<String>,
/// Decrypted message payload base64 encoded
data: String,
/// Padding base64 encoded
padding: String,
}
/// The content topic of a Waku message
/// as per the [specification](https://rfc.vac.dev/spec/36/#contentfilter-type)
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContentFilter {
/// The content topic of a Waku message
content_topic: String,
}
/// The criteria to create subscription to a light node in JSON Format
/// as per the [specification](https://rfc.vac.dev/spec/36/#filtersubscription-type)
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FilterSubscription {
/// Array of [`ContentFilter`] being subscribed to / unsubscribed from
content_filters: Vec<ContentFilter>,
/// Optional pubsub topic
pubsub_topic: Option<String>,
}
/// Criteria used to retrieve historical messages
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StoreQuery {
/// The pubsub topic on which messages are published
pubsub_topic: Option<String>,
/// Array of [`ContentFilter`] to query for historical messages
content_filters: Vec<ContentFilter>,
/// The inclusive lower bound on the timestamp of queried messages.
/// This field holds the Unix epoch time in nanoseconds
start_time: Option<usize>,
/// The inclusive upper bound on the timestamp of queried messages.
/// This field holds the Unix epoch time in nanoseconds
end_time: Option<usize>,
/// Paging information in [`PagingOptions`] format
paging_options: Option<PagingOptions>,
}
/// The response received after doing a query to a store node
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StoreResponse {
/// Array of retrieved historical messages in [`WakuMessage`] format
messages: Vec<WakuMessage>,
/// Paging information in [`PagingOptions`] format from which to resume further historical queries
paging_options: Option<PagingOptions>,
}
/// Paging information
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PagingOptions {
/// Number of messages to retrieve per page
page_size: usize,
/// Message Index from which to perform pagination.
/// If not included and forward is set to `true`, paging will be performed from the beginning of the list.
/// If not included and forward is set to `false`, paging will be performed from the end of the list
cursor: Option<MessageIndex>,
/// `true` if paging forward, `false` if paging backward
forward: bool,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MessageIndex {
/// Hash of the message at this [`MessageIndex`]
digest: String,
/// UNIX timestamp in nanoseconds at which the message at this [`MessageIndex`] was received
receiver_time: usize,
/// UNIX timestamp in nanoseconds at which the message is generated by its sender
sender_time: usize,
/// The pubsub topic of the message at this [`MessageIndex`]
pubsub_topic: String,
}

View File

@ -1,4 +1,4 @@
use waku_sys;
mod general;
#[cfg(test)]
mod tests {