1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! RPC support module
//!
//! `libqaul-types` by itself simply provides a set of Rust types used
//! across libqaul and associated crates and services.  In order to
//! support encoding and decoding these types for the RPC layer, you
//! can enable the RPC module, which provides a set of builder
//! functions to transform types.

// mod util;
// mod error;
// mod users;

mod tests;

// pub use error::ConvertError;
// pub(crate) use error::try_read;

use crate::{
    error::Error,
    messages::{IdType, Mode, MsgQuery},
    services::Service,
    users::{UserAuth, UserProfile, UserUpdate},
    Identity,
};
use alexandria_tags::TagSet;
use serde::{Deserialize, Serialize};

pub const ADDRESS: &'static str = "org.qaul.libqaul";

/// Capabilities are functions that can be executed on a remote
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(tag = "context")]
pub enum Capabilities {
    #[serde(rename = "users")]
    Users(UserCapabilities),
    #[serde(rename = "services")]
    Services(ServiceCapabilities),
    #[serde(rename = "messages")]
    Messages(MessageCapabilities),
    #[serde(rename = "contacts")]
    Contacts(ContactCapabilities),
}

impl Capabilities {
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).expect("Invalid type: can't be made into json!")
    }

    pub fn from_json(s: &str) -> Option<Self> {
        serde_json::from_str(s).ok()
    }
}

/// User scope libqaul functions
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(tag = "cmd", content = "data", rename_all = "kebab-case")]
pub enum UserCapabilities {
    List,
    ListRemote,
    IsAuthenticated { auth: UserAuth },
    Create { pw: String },
    Delete { auth: UserAuth },
    ChangePw { auth: UserAuth, new_pw: String },
    Login { id: Identity, pw: String },
    Logout { auth: UserAuth },
    Get { id: Identity },
    Update { auth: UserAuth, update: UserUpdate },
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(tag = "cmd", content = "data", rename_all = "kebab-case")]
pub enum ServiceCapabilities {}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(tag = "cmd", content = "data", rename_all = "kebab-case")]
pub enum MessageCapabilities {
    Send {
        auth: UserAuth,
        mode: Mode,
        id_type: IdType,
        service: Service,
        tags: TagSet,
        payload: Vec<u8>,
    },
    Subscribe {
        auth: UserAuth,
        service: Service,
        tags: TagSet,
    },
    Query {
        auth: UserAuth,
        service: Service,
        query: MsgQuery,
    },
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(tag = "cmd", content = "data", rename_all = "kebab-case")]
pub enum ContactCapabilities {}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(tag = "context")]
pub enum Reply {
    Users(UserReply),
    Error(Error),
}

impl Reply {
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).expect("Invalid type: can't be made into json!")
    }

    pub fn from_json(s: &str) -> Option<Self> {
        serde_json::from_str(s).ok()
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(tag = "type", content = "data", rename_all = "kebab-case")]
pub enum UserReply {
    List(Vec<UserProfile>),
    Authenticated(bool),
    Auth(UserAuth),
    Ok,
    Profile(UserProfile),
}