bitwarden_ipc/
message.rs

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
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::endpoint::Endpoint;

#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct OutgoingMessage {
    #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
    pub payload: Vec<u8>,
    pub destination: Endpoint,
    #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
    pub topic: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(test, derive(PartialEq))]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct IncomingMessage {
    #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
    pub payload: Vec<u8>,
    pub destination: Endpoint,
    pub source: Endpoint,
    #[cfg_attr(feature = "wasm", wasm_bindgen(getter_with_clone))]
    pub topic: Option<String>,
}

#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct TypedOutgoingMessage<Payload> {
    pub payload: Payload,
    pub destination: Endpoint,
}

impl<Payload> TryFrom<OutgoingMessage> for TypedOutgoingMessage<Payload>
where
    Payload: TryFrom<Vec<u8>>,
{
    type Error = <Payload as TryFrom<Vec<u8>>>::Error;

    fn try_from(value: OutgoingMessage) -> Result<Self, Self::Error> {
        Ok(Self {
            payload: Payload::try_from(value.payload)?,
            destination: value.destination,
        })
    }
}

impl<Payload> TryFrom<TypedOutgoingMessage<Payload>> for OutgoingMessage
where
    Payload: TryInto<Vec<u8>> + PayloadTypeName,
{
    type Error = <Payload as TryInto<Vec<u8>>>::Error;

    fn try_from(value: TypedOutgoingMessage<Payload>) -> Result<Self, Self::Error> {
        Ok(Self {
            payload: value.payload.try_into()?,
            destination: value.destination,
            topic: Some(Payload::name()),
        })
    }
}

#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct TypedIncomingMessage<Payload: PayloadTypeName> {
    pub payload: Payload,
    pub destination: Endpoint,
    pub source: Endpoint,
}

/// This trait is used to ensure that the payload type has a topic associated with it.
pub trait PayloadTypeName {
    fn name() -> String;
}

impl<Payload> TryFrom<IncomingMessage> for TypedIncomingMessage<Payload>
where
    Payload: TryFrom<Vec<u8>> + PayloadTypeName,
{
    type Error = <Payload as TryFrom<Vec<u8>>>::Error;

    fn try_from(value: IncomingMessage) -> Result<Self, Self::Error> {
        Ok(Self {
            payload: Payload::try_from(value.payload)?,
            destination: value.destination,
            source: value.source,
        })
    }
}

impl<Payload> TryFrom<TypedIncomingMessage<Payload>> for IncomingMessage
where
    Payload: TryInto<Vec<u8>> + PayloadTypeName,
{
    type Error = <Payload as TryInto<Vec<u8>>>::Error;

    fn try_from(value: TypedIncomingMessage<Payload>) -> Result<Self, Self::Error> {
        Ok(Self {
            payload: value.payload.try_into()?,
            destination: value.destination,
            source: value.source,
            topic: Some(Payload::name()),
        })
    }
}