add enums for connection state and contact authorization

This commit is contained in:
Dan Ballard 2021-09-15 12:17:45 -07:00
parent 4209e36d7e
commit 7e053cd224
4 changed files with 135 additions and 9 deletions

83
Cargo.lock generated
View File

@ -2,6 +2,53 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.7" version = "0.4.7"
@ -21,6 +68,7 @@ dependencies = [
"libc", "libc",
"serde", "serde",
"serde_json", "serde_json",
"serde_with",
] ]
[[package]] [[package]]
@ -41,6 +89,12 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rustversion"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61b3909d758bb75c79f23d4736fac9433868679d3ad2ea7a61e3c25cfda9a088"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.5" version = "1.0.5"
@ -78,6 +132,35 @@ dependencies = [
"serde", "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]] [[package]]
name = "syn" name = "syn"
version = "1.0.74" version = "1.0.74"

View File

@ -11,4 +11,4 @@ repository = "https://git.openprivacy.ca/cwtch.im/libcwtch-rs"
libc = "0.2" libc = "0.2"
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0.127", features = ["derive"] } serde = { version = "1.0.127", features = ["derive"] }
serde_with = { version = "1.10.0" }

View File

@ -6,7 +6,10 @@ use libcwtch::structs::{*};
fn main() { fn main() {
let bot_home: String = "example_cwtch_dir".to_string(); 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(); std::fs::create_dir_all(&bot_home).unwrap();
let cwtch = libcwtch::new_cwtchlib_go(); let cwtch = libcwtch::new_cwtchlib_go();

View File

@ -1,6 +1,44 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DefaultOnError};
use std::collections::HashMap; 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)] #[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")] #[serde(rename_all = "PascalCase")]
@ -15,6 +53,7 @@ pub struct CwtchEvent {
pub data: HashMap<String, String>, pub data: HashMap<String, String>,
} }
#[serde_as]
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
/// Struct to serialize/deserialize contacts coming from libcwtch-go /// Struct to serialize/deserialize contacts coming from libcwtch-go
@ -23,10 +62,11 @@ pub struct Contact {
pub onion: String, pub onion: String,
/// display name of the contact, as determined in libcwtch-go from name specified by contact /// display name of the contact, as determined in libcwtch-go from name specified by contact
pub name: String, pub name: String,
/// contact connection status: [DISCONNECTED, CONNECTING, CONNECTED, AUTHENTICATED, SYNCED, FAILED, KILLED] #[serde_as(deserialize_as = "DefaultOnError")] // cwtch loads profile/contacts from storage and leaves status blank, it's filled in "soon" by events...
pub status: String, /// contact connection status
/// contact authorization state as set by profile: [unknown, approved, blocked] pub status: ConnectionState,
pub authorization: String, /// contact authorization state as set by profile
pub authorization: ContactAuthorization,
/// is this contact a group? if so "onion" will be a group ID /// is this contact a group? if so "onion" will be a group ID
pub is_group: bool, pub is_group: bool,
//attr: HashMap<String, String>, //attr: HashMap<String, String>,
@ -37,8 +77,8 @@ pub struct Contact {
pub struct Server { pub struct Server {
/// onion address of the server /// onion address of the server
pub onion: String, pub onion: String,
/// server connection status: [DISCONNECTED, CONNECTING, CONNECTED, AUTHENTICATED, SYNCED, FAILED, KILLED] /// server connection status
pub status: String, pub status: ConnectionState,
} }
#[derive(Debug)] #[derive(Debug)]