🗝
summary refs log tree commit diff
path: root/src/ipc.rs
blob: f3e7d0a1a63032d0adbafaf8d97c70a7e623d022 (plain) (blame)
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
use std::fmt::{Display, Formatter};

use bincode::{Decode, Encode};
use paste::paste;

pub trait RequestDefinition: Encode {
    type Response: ResponseDefinition;
    fn into_request(self) -> IPCRequest;
}

pub trait ResponseDefinition: Sized + Decode {
    type Error: std::error::Error;
    fn from_response(response: IPCResponse) -> Option<Result<Self, Self::Error>>;
    fn into_response(self) -> IPCResponse;
}

macro_rules! define {
    ($(@$name:ident { $($req_n:ident: $req_t:ty),* } => { $($resp_n:ident: $resp_t:ty)* } | $($error:ident),*)*) => {
        $(
            paste! {
                #[derive(Encode, Decode)]
                pub struct [<$name Request>] { $(pub $req_n: $req_t),* }
                impl RequestDefinition for [<$name Request>] {
                    type Response = [<$name Response>];
                    fn into_request(self) -> IPCRequest {
                        IPCRequest::$name(self)
                    }
                }

                #[derive(Encode, Decode)]
                pub struct [<$name Response>] { $(pub $resp_n: $resp_t),* }
                impl ResponseDefinition for [<$name Response>] {
                    type Error = [<$name Error>];
                    fn from_response(response: IPCResponse) -> Option<Result<Self, Self::Error>> {
                        match response {
                            IPCResponse::$name(inner) => Some(inner),
                            _ => None,
                        }
                    }
                    fn into_response(self) -> IPCResponse {
                        IPCResponse::$name(Ok(self))
                    }
                }

                #[derive(Encode, Decode, Debug)]
                pub enum [<$name Error>] {
                    $($error,)*
                }

                impl Display for [<$name Error>] {
                    #[allow(unused)]
                    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
                        match *self {
                            $(Self::$error => f.write_str(stringify!($error)),)*
                        }
                    }
                }

                impl std::error::Error for [<$name Error>] {}
            }
        )*
        #[derive(Encode, Decode)]
        pub enum IPCRequest {
            $(
                $name(paste!([<$name Request>])),
            )*
        }
        #[derive(Encode, Decode)]
        pub enum IPCResponse {
            $(
                $name(Result<paste!([<$name Response>]), paste!([<$name Error>])>),
            )*
        }
    };
}

define! {
    @ListAccounts {} => { names: Vec<String> } |
    @GetAccount { name: String } => { scopes: Vec<String> } | NotFound
    @DeleteAccount { name: String } => {} |
    @CreateInvite {} => { link: String } |
    @UpdateScopes { account: String, scopes: Vec<String> } => {} | NotFound
}