From 7e053cd22469f391fd581868f269768c705906e1 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 15 Sep 2021 12:17:45 -0700 Subject: [PATCH] add enums for connection state and contact authorization --- Cargo.lock | 83 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- examples/echobot.rs | 5 ++- src/structs.rs | 54 +++++++++++++++++++++++++---- 4 files changed, 135 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8e3aed..002e88b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,53 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "darling" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "757c0ded2af11d8e739c4daea1ac623dd1624b06c844cf3f5a39f1bdbd99bb12" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c34d8efb62d0c2d7f60ece80f75e5c63c1588ba68032740494b0b9a996466e3" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade7bff147130fe5e6d39f089c6bd49ec0250f35d70b2eebf72afdfc919f15cc" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "itoa" version = "0.4.7" @@ -21,6 +68,7 @@ dependencies = [ "libc", "serde", "serde_json", + "serde_with", ] [[package]] @@ -41,6 +89,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rustversion" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61b3909d758bb75c79f23d4736fac9433868679d3ad2ea7a61e3c25cfda9a088" + [[package]] name = "ryu" version = "1.0.5" @@ -78,6 +132,35 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "062b87e45d8f26714eacfaef0ed9a583e2bfd50ebd96bdd3c200733bd5758e2c" +dependencies = [ + "rustversion", + "serde", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98c1fcca18d55d1763e1c16873c4bde0ac3ef75179a28c7b372917e0494625be" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.74" diff --git a/Cargo.toml b/Cargo.toml index 2426410..228fa1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,4 @@ repository = "https://git.openprivacy.ca/cwtch.im/libcwtch-rs" libc = "0.2" serde_json = "1.0" serde = { version = "1.0.127", features = ["derive"] } - +serde_with = { version = "1.10.0" } diff --git a/examples/echobot.rs b/examples/echobot.rs index c6ec5d5..7c961e6 100644 --- a/examples/echobot.rs +++ b/examples/echobot.rs @@ -6,7 +6,10 @@ use libcwtch::structs::{*}; fn main() { let bot_home: String = "example_cwtch_dir".to_string(); - std::fs::remove_dir_all(&bot_home); + match std::fs::remove_dir_all(&bot_home) { + Err(_e) => (), + _ => (), + } std::fs::create_dir_all(&bot_home).unwrap(); let cwtch = libcwtch::new_cwtchlib_go(); diff --git a/src/structs.rs b/src/structs.rs index 472a2e3..8d60b68 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,6 +1,44 @@ - use serde::{Deserialize, Serialize}; +use serde_with::{serde_as, DefaultOnError}; use std::collections::HashMap; +use crate::structs::ConnectionState::Disconnected; + +#[derive(Serialize, Deserialize, Debug)] +/// Defines the states a Cwtch connection can be in +pub enum ConnectionState { + /// The Cwtch connection is not conected at all + Disconnected, + /// Cwtch is attempting to connect to the address, it may or may not be online + Connecting, + /// Cwtch has made a basic connection to the address, but not done authorization. The address is online but unverified as the desired target + Connected, + /// Cwtch has authenticated the desired connection + Authenticated, + /// In the case of a server connection, Cwtch has finished syncing messages from the server and is caught up + Synced, + /// The connection attempt failed + Failed, + /// The connection has been killed + Killed +} + +impl Default for ConnectionState { + fn default() -> ConnectionState { + Disconnected + } +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "lowercase")] +/// Defines the various authorization modes a contact can be in +pub enum ContactAuthorization { + /// This is an unknown (new?) contact. The user has not approved them + Unknown, + /// The contact is approved by the user (manual action) + Approved, + /// The contact is blocked by the user, should be ignored + Blocked +} #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "PascalCase")] @@ -15,6 +53,7 @@ pub struct CwtchEvent { pub data: HashMap, } +#[serde_as] #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] /// Struct to serialize/deserialize contacts coming from libcwtch-go @@ -23,10 +62,11 @@ pub struct Contact { pub onion: String, /// display name of the contact, as determined in libcwtch-go from name specified by contact pub name: String, - /// contact connection status: [DISCONNECTED, CONNECTING, CONNECTED, AUTHENTICATED, SYNCED, FAILED, KILLED] - pub status: String, - /// contact authorization state as set by profile: [unknown, approved, blocked] - pub authorization: String, + #[serde_as(deserialize_as = "DefaultOnError")] // cwtch loads profile/contacts from storage and leaves status blank, it's filled in "soon" by events... + /// contact connection status + pub status: ConnectionState, + /// contact authorization state as set by profile + pub authorization: ContactAuthorization, /// is this contact a group? if so "onion" will be a group ID pub is_group: bool, //attr: HashMap, @@ -37,8 +77,8 @@ pub struct Contact { pub struct Server { /// onion address of the server pub onion: String, - /// server connection status: [DISCONNECTED, CONNECTING, CONNECTED, AUTHENTICATED, SYNCED, FAILED, KILLED] - pub status: String, + /// server connection status + pub status: ConnectionState, } #[derive(Debug)]