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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
use failure::{err_msg, format_err, Error};
use serde_derive::{Deserialize, Serialize};
use std::fmt;

use crate::{
    AccountId, Addressable, AgentId, Authenticable, Destination, EventSubscription,
    RequestSubscription, ResponseSubscription, SharedGroup, Source,
};

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug)]
pub enum ConnectionMode {
    Agent,
    Bridge,
}

impl fmt::Display for ConnectionMode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                ConnectionMode::Agent => "agents",
                ConnectionMode::Bridge => "bridge-agents",
            }
        )
    }
}

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Deserialize)]
pub struct AgentConfig {
    uri: String,
}

#[derive(Debug)]
pub struct AgentBuilder {
    agent_id: AgentId,
    version: String,
    mode: ConnectionMode,
}

impl AgentBuilder {
    pub fn new(agent_id: AgentId) -> Self {
        Self {
            agent_id,
            version: String::from("v1.mqtt3"),
            mode: ConnectionMode::Agent,
        }
    }

    pub fn version(self, version: &str) -> Self {
        Self {
            agent_id: self.agent_id,
            version: version.to_owned(),
            mode: self.mode,
        }
    }

    pub fn mode(self, mode: ConnectionMode) -> Self {
        Self {
            agent_id: self.agent_id,
            version: self.version,
            mode,
        }
    }

    pub fn start(
        self,
        config: &AgentConfig,
    ) -> Result<(Agent, rumqtt::Receiver<rumqtt::Notification>), Error> {
        let options = Self::mqtt_options(&self.mqtt_client_id(), &config)?;
        let (tx, rx) = rumqtt::MqttClient::start(options)?;

        let agent = Agent::new(self.agent_id, tx);
        Ok((agent, rx))
    }

    fn mqtt_client_id(&self) -> String {
        format!(
            "{version}/{mode}/{agent_id}",
            version = self.version,
            mode = self.mode,
            agent_id = self.agent_id,
        )
    }

    fn mqtt_options(client_id: &str, config: &AgentConfig) -> Result<rumqtt::MqttOptions, Error> {
        let uri = config.uri.parse::<http::Uri>()?;
        let host = uri.host().ok_or_else(|| err_msg("missing MQTT host"))?;
        let port = uri
            .port_part()
            .ok_or_else(|| err_msg("missing MQTT port"))?;

        Ok(rumqtt::MqttOptions::new(client_id, host, port.as_u16())
            .set_keep_alive(30)
            .set_reconnect_opts(rumqtt::ReconnectOptions::AfterFirstSuccess(5)))
    }
}

pub struct Agent {
    id: AgentId,
    tx: rumqtt::MqttClient,
}

impl Agent {
    fn new(id: AgentId, tx: rumqtt::MqttClient) -> Self {
        Self { id, tx }
    }

    pub fn id(&self) -> &AgentId {
        &self.id
    }

    pub fn publish<M>(&mut self, message: &M) -> Result<(), Error>
    where
        M: Publishable,
    {
        let topic = message.destination_topic(&self.id)?;
        let bytes = message.to_bytes()?;

        self.tx
            .publish(topic, QoS::AtLeastOnce, false, bytes)
            .map_err(|_| err_msg("Error publishing an MQTT message"))
    }

    pub fn subscribe<S>(
        &mut self,
        subscription: &S,
        qos: QoS,
        maybe_group: Option<&SharedGroup>,
    ) -> Result<(), Error>
    where
        S: SubscriptionTopic,
    {
        let mut topic = subscription.subscription_topic(&self.id)?;
        if let Some(ref group) = maybe_group {
            topic = format!("$share/{group}/{topic}", group = group, topic = topic);
        };

        self.tx.subscribe(topic, qos)?;
        Ok(())
    }
}

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone)]
pub(crate) struct AuthnProperties {
    agent_id: AgentId,
}

impl Authenticable for AuthnProperties {
    fn account_id(&self) -> &AccountId {
        &self.agent_id.account_id()
    }
}

impl Addressable for AuthnProperties {
    fn agent_id(&self) -> &AgentId {
        &self.agent_id
    }
}

impl From<AgentId> for AuthnProperties {
    fn from(agent_id: AgentId) -> Self {
        Self { agent_id }
    }
}

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Deserialize)]
pub struct IncomingEventProperties {
    #[serde(flatten)]
    authn: AuthnProperties,
}

impl Authenticable for IncomingEventProperties {
    fn account_id(&self) -> &AccountId {
        &self.authn.account_id()
    }
}

impl Addressable for IncomingEventProperties {
    fn agent_id(&self) -> &AgentId {
        &self.authn.agent_id()
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IncomingRequestProperties {
    method: String,
    correlation_data: String,
    response_topic: String,
    #[serde(flatten)]
    authn: AuthnProperties,
}

impl IncomingRequestProperties {
    pub fn method(&self) -> &str {
        &self.method
    }

    pub fn to_response(
        &self,
        status: &'static OutgoingResponseStatus,
    ) -> OutgoingResponseProperties {
        OutgoingResponseProperties::new(status, &self.correlation_data, Some(&self.response_topic))
    }
}

impl Authenticable for IncomingRequestProperties {
    fn account_id(&self) -> &AccountId {
        &self.authn.account_id()
    }
}

impl Addressable for IncomingRequestProperties {
    fn agent_id(&self) -> &AgentId {
        &self.authn.agent_id()
    }
}

#[derive(Debug, Deserialize)]
pub struct IncomingResponseProperties {
    correlation_data: String,
    #[serde(flatten)]
    authn: AuthnProperties,
}

impl Authenticable for IncomingResponseProperties {
    fn account_id(&self) -> &AccountId {
        &self.authn.account_id()
    }
}

impl Addressable for IncomingResponseProperties {
    fn agent_id(&self) -> &AgentId {
        &self.authn.agent_id()
    }
}

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug)]
pub struct IncomingMessage<T, P>
where
    P: Addressable,
{
    payload: T,
    properties: P,
}

impl<T, P> IncomingMessage<T, P>
where
    P: Addressable,
{
    pub fn new(payload: T, properties: P) -> Self {
        Self {
            payload,
            properties,
        }
    }

    pub fn payload(&self) -> &T {
        &self.payload
    }

    pub fn properties(&self) -> &P {
        &self.properties
    }
}

impl<T> IncomingRequest<T> {
    pub fn to_response<R>(
        &self,
        data: R,
        status: &'static OutgoingResponseStatus,
    ) -> OutgoingResponse<R>
    where
        R: serde::Serialize,
    {
        OutgoingMessage::new(
            data,
            self.properties.to_response(status),
            Destination::Unicast(self.properties().agent_id().clone()),
        )
    }
}

pub type IncomingEvent<T> = IncomingMessage<T, IncomingEventProperties>;
pub type IncomingRequest<T> = IncomingMessage<T, IncomingRequestProperties>;
pub type IncomingResponse<T> = IncomingMessage<T, IncomingResponseProperties>;

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Serialize)]
pub struct OutgoingEventProperties {
    label: &'static str,
}

impl OutgoingEventProperties {
    pub fn new(label: &'static str) -> Self {
        Self { label }
    }
}

#[derive(Debug, Serialize)]
pub struct OutgoingRequestProperties {
    method: &'static str,
}

impl OutgoingRequestProperties {
    pub fn new(method: &'static str) -> Self {
        Self { method }
    }
}

#[derive(Debug, Serialize)]
pub struct OutgoingResponseProperties {
    #[serde(with = "crate::serde::HttpStatusCodeRef")]
    status: &'static OutgoingResponseStatus,
    correlation_data: String,
    #[serde(skip)]
    response_topic: Option<String>,
}

impl OutgoingResponseProperties {
    pub fn new(
        status: &'static OutgoingResponseStatus,
        correlation_data: &str,
        response_topic: Option<&str>,
    ) -> Self {
        Self {
            status,
            correlation_data: correlation_data.to_owned(),
            response_topic: response_topic.map(|val| val.to_owned()),
        }
    }
}

pub type OutgoingResponseStatus = http::StatusCode;

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug)]
pub struct OutgoingMessage<T, P>
where
    T: serde::Serialize,
{
    payload: T,
    properties: P,
    destination: Destination,
}

impl<T, P> OutgoingMessage<T, P>
where
    T: serde::Serialize,
{
    fn new(payload: T, properties: P, destination: Destination) -> Self {
        Self {
            payload,
            properties,
            destination,
        }
    }
}

impl<T> OutgoingEvent<T>
where
    T: serde::Serialize,
{
    pub fn broadcast(payload: T, properties: OutgoingEventProperties, to_uri: &str) -> Self {
        OutgoingMessage::new(
            payload,
            properties,
            Destination::Broadcast(to_uri.to_owned()),
        )
    }
}

impl<T> OutgoingRequest<T>
where
    T: serde::Serialize,
{
    pub fn multicast(
        payload: T,
        properties: OutgoingRequestProperties,
        to: &dyn Addressable,
    ) -> Self {
        OutgoingMessage::new(
            payload,
            properties,
            Destination::Multicast(to.account_id().clone()),
        )
    }

    pub fn unicast(
        payload: T,
        properties: OutgoingRequestProperties,
        to: &dyn Addressable,
    ) -> Self {
        OutgoingMessage::new(
            payload,
            properties,
            Destination::Unicast(to.agent_id().clone()),
        )
    }
}

impl<T> OutgoingResponse<T>
where
    T: serde::Serialize,
{
    pub fn unicast(
        payload: T,
        properties: OutgoingResponseProperties,
        to: &dyn Addressable,
    ) -> Self {
        OutgoingMessage::new(
            payload,
            properties,
            Destination::Unicast(to.agent_id().clone()),
        )
    }
}

pub type OutgoingEvent<T> = OutgoingMessage<T, OutgoingEventProperties>;
pub type OutgoingRequest<T> = OutgoingMessage<T, OutgoingRequestProperties>;
pub type OutgoingResponse<T> = OutgoingMessage<T, OutgoingResponseProperties>;

impl<T> compat::IntoEnvelope for OutgoingEvent<T>
where
    T: serde::Serialize,
{
    fn into_envelope(self) -> Result<compat::OutgoingEnvelope, Error> {
        let payload = serde_json::to_string(&self.payload)?;
        let envelope = compat::OutgoingEnvelope::new(
            &payload,
            compat::OutgoingEnvelopeProperties::Event(self.properties),
            self.destination,
        );
        Ok(envelope)
    }
}

impl<T> compat::IntoEnvelope for OutgoingRequest<T>
where
    T: serde::Serialize,
{
    fn into_envelope(self) -> Result<compat::OutgoingEnvelope, Error> {
        let payload = serde_json::to_string(&self.payload)?;
        let envelope = compat::OutgoingEnvelope::new(
            &payload,
            compat::OutgoingEnvelopeProperties::Request(self.properties),
            self.destination,
        );
        Ok(envelope)
    }
}

impl<T> compat::IntoEnvelope for OutgoingResponse<T>
where
    T: serde::Serialize,
{
    fn into_envelope(self) -> Result<compat::OutgoingEnvelope, Error> {
        let payload = serde_json::to_string(&self.payload)?;
        let envelope = compat::OutgoingEnvelope::new(
            &payload,
            compat::OutgoingEnvelopeProperties::Response(self.properties),
            self.destination,
        );
        Ok(envelope)
    }
}

////////////////////////////////////////////////////////////////////////////////

pub trait Publishable {
    fn destination_topic(&self, me: &dyn Addressable) -> Result<String, Error>;
    fn to_bytes(&self) -> Result<String, Error>;
}

////////////////////////////////////////////////////////////////////////////////

pub trait Publish<'a> {
    fn publish(&'a self, tx: &mut Agent) -> Result<(), Error>;
}

impl<'a, T> Publish<'a> for T
where
    T: Publishable,
{
    fn publish(&'a self, tx: &mut Agent) -> Result<(), Error> {
        tx.publish(self)?;
        Ok(())
    }
}

impl<'a, T1, T2> Publish<'a> for (T1, T2)
where
    T1: Publishable,
    T2: Publishable,
{
    fn publish(&'a self, tx: &mut Agent) -> Result<(), Error> {
        tx.publish(&self.0)?;
        tx.publish(&self.1)?;
        Ok(())
    }
}

////////////////////////////////////////////////////////////////////////////////

trait DestinationTopic {
    fn destination_topic(&self, me: &dyn Addressable, dest: &Destination) -> Result<String, Error>;
}

impl DestinationTopic for OutgoingEventProperties {
    fn destination_topic(&self, me: &dyn Addressable, dest: &Destination) -> Result<String, Error> {
        match dest {
            Destination::Broadcast(ref uri) => Ok(format!(
                "apps/{app}/api/v1/{uri}",
                app = me.account_id(),
                uri = uri,
            )),
            _ => Err(format_err!(
                "destination = '{:?}' is incompatible with event message type",
                dest,
            )),
        }
    }
}

impl DestinationTopic for OutgoingRequestProperties {
    fn destination_topic(&self, me: &dyn Addressable, dest: &Destination) -> Result<String, Error> {
        match dest {
            Destination::Unicast(ref agent_id) => Ok(format!(
                "agents/{agent_id}/api/v1/in/{app}",
                agent_id = agent_id,
                app = me.account_id(),
            )),
            Destination::Multicast(ref account_id) => Ok(format!(
                "agents/{agent_id}/api/v1/out/{app}",
                agent_id = me.agent_id(),
                app = account_id,
            )),
            _ => Err(format_err!(
                "destination = '{:?}' is incompatible with request message type",
                dest,
            )),
        }
    }
}

impl DestinationTopic for OutgoingResponseProperties {
    fn destination_topic(&self, me: &dyn Addressable, dest: &Destination) -> Result<String, Error> {
        match &self.response_topic {
            Some(ref val) => Ok(val.to_owned()),
            None => match dest {
                Destination::Unicast(ref agent_id) => Ok(format!(
                    "agents/{agent_id}/api/v1/in/{app}",
                    agent_id = agent_id,
                    app = me.account_id(),
                )),
                _ => Err(format_err!(
                    "destination = '{:?}' is incompatible with response message type",
                    dest,
                )),
            },
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

pub trait SubscriptionTopic {
    fn subscription_topic(&self, agent_id: &dyn Addressable) -> Result<String, Error>;
}

impl<'a> SubscriptionTopic for EventSubscription<'a> {
    fn subscription_topic(&self, _me: &dyn Addressable) -> Result<String, Error> {
        match self.source {
            Source::Broadcast(ref from, ref uri) => Ok(format!(
                "apps/{app}/api/v1/{uri}",
                app = from.account_id(),
                uri = uri,
            )),
            _ => Err(format_err!(
                "source = '{:?}' is incompatible with event subscription",
                self.source,
            )),
        }
    }
}

impl<'a> SubscriptionTopic for RequestSubscription<'a> {
    fn subscription_topic(&self, me: &dyn Addressable) -> Result<String, Error> {
        match self.source {
            Source::Multicast => Ok(format!("agents/+/api/v1/out/{app}", app = me.account_id())),
            Source::Unicast(Some(ref from)) => Ok(format!(
                "agents/{agent_id}/api/v1/in/{app}",
                agent_id = me.agent_id(),
                app = from.account_id(),
            )),
            Source::Unicast(None) => Ok(format!(
                "agents/{agent_id}/api/v1/in/+",
                agent_id = me.agent_id(),
            )),
            _ => Err(format_err!(
                "source = '{:?}' is incompatible with request subscription",
                self.source,
            )),
        }
    }
}

impl<'a> SubscriptionTopic for ResponseSubscription<'a> {
    fn subscription_topic(&self, me: &dyn Addressable) -> Result<String, Error> {
        match self.source {
            Source::Unicast(Some(ref from)) => Ok(format!(
                "agents/{agent_id}/api/v1/in/{app}",
                agent_id = me.agent_id(),
                app = from.account_id(),
            )),
            Source::Unicast(None) => Ok(format!(
                "agents/{agent_id}/api/v1/in/+",
                agent_id = me.agent_id(),
            )),
            _ => Err(format_err!(
                "source = '{:?}' is incompatible with response subscription",
                self.source,
            )),
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

pub mod compat {

    use super::{
        Destination, DestinationTopic, IncomingEvent, IncomingEventProperties, IncomingMessage,
        IncomingRequest, IncomingRequestProperties, IncomingResponse, IncomingResponseProperties,
        OutgoingEventProperties, OutgoingRequestProperties, OutgoingResponseProperties,
        Publishable,
    };
    use crate::Addressable;
    use failure::{err_msg, format_err, Error};
    use serde_derive::{Deserialize, Serialize};

    ////////////////////////////////////////////////////////////////////////////////

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "lowercase")]
    #[serde(tag = "type")]
    pub enum IncomingEnvelopeProperties {
        Event(IncomingEventProperties),
        Request(IncomingRequestProperties),
        Response(IncomingResponseProperties),
    }

    #[derive(Debug, Deserialize)]
    pub struct IncomingEnvelope {
        payload: String,
        properties: IncomingEnvelopeProperties,
    }

    impl IncomingEnvelope {
        pub fn properties(&self) -> &IncomingEnvelopeProperties {
            &self.properties
        }

        pub fn payload<T>(&self) -> Result<T, Error>
        where
            T: serde::de::DeserializeOwned,
        {
            let payload = serde_json::from_str::<T>(&self.payload)?;
            Ok(payload)
        }
    }

    pub fn into_event<T>(envelope: IncomingEnvelope) -> Result<IncomingEvent<T>, Error>
    where
        T: serde::de::DeserializeOwned,
    {
        let payload = envelope.payload::<T>()?;
        match envelope.properties {
            IncomingEnvelopeProperties::Event(props) => Ok(IncomingMessage::new(payload, props)),
            val => Err(format_err!("error converting into event = {:?}", val)),
        }
    }

    pub fn into_request<T>(envelope: IncomingEnvelope) -> Result<IncomingRequest<T>, Error>
    where
        T: serde::de::DeserializeOwned,
    {
        let payload = envelope.payload::<T>()?;
        match envelope.properties {
            IncomingEnvelopeProperties::Request(props) => Ok(IncomingMessage::new(payload, props)),
            _ => Err(err_msg("Error converting into request")),
        }
    }

    pub fn into_response<T>(envelope: IncomingEnvelope) -> Result<IncomingResponse<T>, Error>
    where
        T: serde::de::DeserializeOwned,
    {
        let payload = envelope.payload::<T>()?;
        match envelope.properties {
            IncomingEnvelopeProperties::Response(props) => Ok(IncomingMessage::new(payload, props)),
            _ => Err(err_msg("error converting into response")),
        }
    }

    ////////////////////////////////////////////////////////////////////////////////

    #[derive(Debug, Serialize)]
    #[serde(rename_all = "lowercase")]
    #[serde(tag = "type")]
    pub enum OutgoingEnvelopeProperties {
        Event(OutgoingEventProperties),
        Request(OutgoingRequestProperties),
        Response(OutgoingResponseProperties),
    }

    #[derive(Debug, Serialize)]
    pub struct OutgoingEnvelope {
        payload: String,
        properties: OutgoingEnvelopeProperties,
        #[serde(skip)]
        destination: Destination,
    }

    impl OutgoingEnvelope {
        pub fn new(
            payload: &str,
            properties: OutgoingEnvelopeProperties,
            destination: Destination,
        ) -> Self {
            Self {
                payload: payload.to_owned(),
                properties,
                destination,
            }
        }
    }

    impl DestinationTopic for OutgoingEnvelopeProperties {
        fn destination_topic(
            &self,
            me: &dyn Addressable,
            dest: &Destination,
        ) -> Result<String, Error> {
            match self {
                OutgoingEnvelopeProperties::Event(val) => val.destination_topic(me, dest),
                OutgoingEnvelopeProperties::Request(val) => val.destination_topic(me, dest),
                OutgoingEnvelopeProperties::Response(val) => val.destination_topic(me, dest),
            }
        }
    }

    impl<'a> Publishable for OutgoingEnvelope {
        fn destination_topic(&self, me: &dyn Addressable) -> Result<String, Error> {
            self.properties.destination_topic(me, &self.destination)
        }

        fn to_bytes(&self) -> Result<String, Error> {
            Ok(serde_json::to_string(&self)?)
        }
    }

    ////////////////////////////////////////////////////////////////////////////////

    pub trait IntoEnvelope {
        fn into_envelope(self) -> Result<OutgoingEnvelope, Error>;
    }
}

////////////////////////////////////////////////////////////////////////////////

pub use rumqtt::client::Notification;
pub use rumqtt::QoS;