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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! # nats-types
//!
//! The `nats-types` crate contains an enum `ProtocolMessage`. This enum can be used to
//! parse the string output from a NATS server as well as produce strings to be sent to
//! a NATS server.
//!
//! The primary use for this crate is to be used in support of building a NATS client, though
//! other potential uses might be possible.
//!
//! To produce a protocol message, simply create the enum:
//! ```rust
//! extern crate nats_types;
//!
//! use nats_types::{PublishMessage, ProtocolMessage};
//!
//! let publish = ProtocolMessage::Publish( PublishMessage {
//!     reply_to: Some("INBOX.42".to_string()),
//!     subject: "workdispatch".to_string(),
//!     payload_size: 11,
//!     payload: b"Hello World".to_vec(),
//! });
//!
//! let out = format!("{}", publish);
//! assert_eq!(out, "PUB workdispatch INBOX.42 11\r\nHello World\r\n");
//! ```
//!
//! The same message can be constructed from the 2-line message received from a NATS server:
//! ```rust
//! extern crate nats_types;
//!
//! use std::str::FromStr;
//! use nats_types::{ProtocolMessage};
//!
//! let msg = "PUB FOO 11\r\nHello NATS!\r\n";
//! let protomsg = ProtocolMessage::from_str(&msg).unwrap();
//! if let ProtocolMessage::Publish(pubm) = protomsg {
//!     assert_eq!(pubm.payload_size, 11);
//!     assert_eq!(pubm.subject, "FOO");
//!     assert_eq!(pubm.reply_to, None);
//!     assert_eq!(pubm.payload, b"Hello NATS!");
//! }
//! ```
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate nom;

use nom::AsBytes;
use std::error::Error;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use std::io::Write;
use std::str::FromStr;

pub use parser::parse_msg_header; 

/// An enum whose variants are all of the available protocol messages as defined by the
/// NATS protocol documentation.
#[derive(Debug, Clone, PartialEq)]
pub enum ProtocolMessage {
    Unsubscribe(UnsubscribeMessage),
    Publish(PublishMessage),
    Message(DeliveredMessage),
    Subscribe(SubscribeMessage),
    Ping,
    Pong,
    Ok,
    Error(String),
    Info(ServerInformation),
    Connect(ConnectionInformation),
}

impl Display for ProtocolMessage {
    fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
        match self {
            ProtocolMessage::Unsubscribe(m) => write!(f, "{}", m),
            ProtocolMessage::Subscribe(m) => write!(f, "{}", m),
            ProtocolMessage::Publish(m) => write!(f, "{}", m),
            ProtocolMessage::Message(m) => write!(f, "{}", m),
            ProtocolMessage::Ping => write!(f, "PING\r\n"),
            ProtocolMessage::Pong => write!(f, "PONG\r\n"),
            ProtocolMessage::Ok => write!(f, "+OK\r\n"),
            ProtocolMessage::Error(s) => write!(f, "-ERR '{}'", s),
            ProtocolMessage::Info(si) => write!(f, "{}", si),
            ProtocolMessage::Connect(ci) => write!(f, "{}", ci),
        }
    }
}

impl FromStr for ProtocolMessage {
    type Err = NatsParseError;

    fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
        if s.starts_with("UNSUB") {
            match UnsubscribeMessage::from_str(s) {
                Ok(m) => Ok(ProtocolMessage::Unsubscribe(m)),
                Err(e) => Err(e),
            }
        } else if s.starts_with("PUB") {
            match PublishMessage::from_str(s) {
                Ok(m) => Ok(ProtocolMessage::Publish(m)),
                Err(e) => Err(e),
            }
        } else if s.starts_with("MSG") {
            match DeliveredMessage::from_str(s) {
                Ok(m) => Ok(ProtocolMessage::Message(m)),
                Err(e) => Err(e),
            }
        } else if s.starts_with("SUB") {
            match SubscribeMessage::from_str(s) {
                Ok(m) => Ok(ProtocolMessage::Subscribe(m)),
                Err(e) => Err(e),
            }
        } else if s.starts_with("PING") {
            Ok(ProtocolMessage::Ping)
        } else if s.starts_with("PONG") {
            Ok(ProtocolMessage::Pong)
        } else if s.starts_with("+OK") {
            Ok(ProtocolMessage::Ok)
        } else if s.starts_with("-ERR") {
            match parser::parse_err_header(s) {
                Some(h) => Ok(ProtocolMessage::Error(h.message)),
                None => Err(NatsParseError {
                    msg: "Failed to parse protocol message of type ERR".to_string(),
                }),
            }
        } else if s.starts_with("INFO") {
            match ServerInformation::from_str(s) {
                Ok(m) => Ok(ProtocolMessage::Info(m)),
                Err(e) => Err(e),
            }
        } else if s.starts_with("CONNECT") {
            match ConnectionInformation::from_str(s) {
                Ok(m) => Ok(ProtocolMessage::Connect(m)),
                Err(e) => Err(e),
            }
        } else {
            Err(NatsParseError {
                msg: "Failed to parse protocol message - unknown message type?".to_string(),
            })
        }
    }
}

/// Represents server connection information sent by the client to configure the connection
/// immediately after connecting. The NATS protocol definition for this is as follows:
/// ```text
/// CONNECT [json]
/// ```
#[derive(Serialize, Debug, Clone, PartialEq, Deserialize)]
pub struct ConnectionInformation {
    pub verbose: bool,
    pub pedantic: bool,
    pub tls_required: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pass: Option<String>,
    pub lang: String,
    pub name: String,
    pub version: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub protocol: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sig: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jwt: Option<String>,
}

impl ConnectionInformation {
    /// Constructor to create a new connection information struct
    pub fn new(
        verbose: bool,
        pedantic: bool,
        tls_required: bool,
        auth_token: Option<String>,
        user: Option<String>,
        pass: Option<String>,
        lang: String,
        name: String,
        version: String,
        protocol: Option<u64>,
        sig: Option<String>,
        jwt: Option<String>,
    ) -> ConnectionInformation {
        ConnectionInformation {
            verbose,
            pedantic,
            tls_required,
            auth_token,
            user,
            pass,
            lang,
            name,
            version,
            protocol,
            sig,
            jwt,
        }
    }
}

impl Display for ConnectionInformation {
    fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
        let out = serde_json::to_string(self);
        match out {
            Ok(json) => write!(f, "CONNECT {}\r\n", json),
            Err(e) => write!(f, "<<BAD CONNECT INFO - CAN'T SERIALIZE>>: {}", e),
        }
    }
}

impl FromStr for ConnectionInformation {
    type Err = NatsParseError;

    fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
        let s = s.replace("CONNECT ", "");
        println!("{}", s);
        match serde_json::from_str(s.trim()) {
            Ok(ci) => Ok(ci),
            Err(e) => Err(NatsParseError {
                msg: format!("Failed to parse connection info JSON: {}", e),
            }),
        }
    }
}

/// Represents a NATS server information message, defined according to the NATS
/// protocol documentation:
/// ```text
/// INFO {["option_name":option_value],...}
/// ```
#[derive(Serialize, Debug, Clone, PartialEq, Deserialize)]
pub struct ServerInformation {
    pub server_id: String,
    pub version: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proto: Option<usize>,
    pub go: String,
    pub host: String,
    pub port: u64,
    #[serde(default)]
    pub auth_required: bool,
    #[serde(default)]
    pub tls_required: bool,
    #[serde(default)]
    pub max_payload: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connect_urls: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nonce: Option<String>,
}

impl ServerInformation {
    /// Constructor to create a new server information
    pub fn new(
        server_id: String,
        version: String,
        proto: Option<usize>,
        go: String,
        host: String,
        port: u64,
        auth_required: bool,
        tls_required: bool,
        max_payload: u64,
        client_id: Option<usize>,
        connect_urls: Option<Vec<String>>,
        nonce: Option<String>,
    ) -> ServerInformation {
        ServerInformation {
            server_id,
            version,
            proto,
            go,
            host,
            port,
            auth_required,
            tls_required,
            max_payload,
            client_id,
            connect_urls,
            nonce,
        }
    }
}

impl Display for ServerInformation {
    fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
        let out = serde_json::to_string(self);
        match out {
            Ok(json) => write!(f, "INFO {}\r\n", json),
            Err(e) => write!(f, "<<BAD SERVERINFO - CAN'T SERIALIZE>>: {}", e),
        }
    }
}

impl FromStr for ServerInformation {
    type Err = NatsParseError;

    fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
        let s = s.replace("INFO ", "");
        match serde_json::from_str(s.trim()) {
            Ok(si) => Ok(si),
            Err(_) => Err(NatsParseError {
                msg: "Failed to parse server info JSON".to_string(),
            }),
        }
    }
}

/// Represents a message as per the NATS protocol documentation:
/// ```text
/// MSG <subject> <sid> [reply-to] <#bytes>\r\n[payload]\r\n
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct DeliveredMessage {
    pub subject: String,
    pub subscription_id: usize,
    pub reply_to: Option<String>,
    pub payload_size: usize,
    pub payload: Vec<u8>,
}

impl DeliveredMessage {
    /// Constructor to build a new message from a given subject, payload, etc
    pub fn new(
        subject: String,
        subscription_id: usize,
        reply_to: Option<String>,
        payload: Vec<u8>,
    ) -> DeliveredMessage {
        DeliveredMessage {
            subject,
            subscription_id,
            reply_to,
            payload_size: payload.len(),
            payload,
        }
    }
}

impl Display for DeliveredMessage {
    fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
        match self.reply_to {
            None => write!(
                f,
                "MSG {} {} {}\r\n{}\r\n",
                self.subject,
                self.subscription_id,
                self.payload_size,
                vec_to_str(&self.payload)
            ),
            Some(ref rt) => write!(
                f,
                "MSG {} {} {} {}\r\n{}\r\n",
                self.subject,
                self.subscription_id,
                rt,
                self.payload_size,
                vec_to_str(&self.payload)
            ),
        }
    }
}

impl FromStr for DeliveredMessage {
    type Err = NatsParseError;

    fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
        let split = parser::split_header_and_payload(s);
        match split {
            None => Err(NatsParseError {
                msg: "Failed to parse message - possibly not a 2-line message".to_string(),
            }),
            Some(split) => {
                let res = parser::parse_msg_header(&split.0);
                match res {
                    Some(r) => Ok(DeliveredMessage {
                        subject: r.subject,
                        subscription_id: r.sid,
                        reply_to: r.reply_to,
                        payload_size: r.message_len,
                        payload: split.1,
                    }),
                    None => Err(NatsParseError {
                        msg: "Failed to parse delivered message".to_string(),
                    }),
                }
            }
        }
    }
}

/// A struct that represents a subscription message. This message conforms
/// to the following format from the NATS protocol definition:
/// ```text
/// SUB <subject> [queue group] <sid>\r\n
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct SubscribeMessage {
    pub subject: String,
    pub queue_group: Option<String>,
    pub subscription_id: usize,
}

impl SubscribeMessage {
    /// Constructor to create a new subscription message
    pub fn new(
        subject: String,
        queue_group: Option<String>,
        subscription_id: usize,
    ) -> SubscribeMessage {
        SubscribeMessage {
            subject,
            queue_group,
            subscription_id,
        }
    }

    /// Efficient single-allocation conversion into a byte vector suitable for network transmission
    pub fn as_vec(
        subject: &str,
        queue_group: Option<&str>,
        subscription_id: usize,
    ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        let mut buffer = Vec::with_capacity(subject.len() + queue_group.map_or(0, |g| g.len() + 1));

        write!(buffer, "SUB {}", subject)?;
        if let Some(qg) = queue_group {
            write!(buffer, " {}", qg)?;
        }
        write!(buffer, " {}\r\n", subscription_id)?;

        Ok(buffer)
    }
}

impl Display for SubscribeMessage {
    fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
        match self.queue_group {
            None => write!(f, "SUB {} {}\r\n", self.subject, self.subscription_id),
            Some(ref q) => write!(f, "SUB {} {} {}\r\n", self.subject, q, self.subscription_id),
        }
    }
}

impl FromStr for SubscribeMessage {
    type Err = NatsParseError;

    fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
        let res = parser::parse_sub_header(s);
        match res {
            Some(r) => Ok(SubscribeMessage {
                subscription_id: r.sid,
                queue_group: r.queue_group,
                subject: r.subject,
            }),
            None => Err(NatsParseError {
                msg: "Failed to parse Subscribe message".to_string(),
            }),
        }
    }
}

/// A struct that represents an unsubscribe message. This message conforms
/// to the following format from the NATS protocol definition:
/// ```text
/// UNSUB <sid> [max_msgs]
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct UnsubscribeMessage {
    pub subscription_id: usize,
    pub max_messages: Option<usize>,
}

impl UnsubscribeMessage {
    /// Constructor to create a new unsub message
    pub fn new(subscription_id: usize, max_messages: Option<usize>) -> UnsubscribeMessage {
        UnsubscribeMessage {
            subscription_id,
            max_messages,
        }
    }

    /// Efficient single-allocation conversion to a byte vector suitable for network transmission
    pub fn as_vec(
        subscription_id: usize,
        max_messages: Option<usize>,
    ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        let mut buffer = Vec::with_capacity(16);
        write!(buffer, "UNSUB {}", subscription_id)?;
        if let Some(m) = max_messages {
            write!(buffer, " {}", m)?;
        }
        write!(buffer, "\r\n")?;
        Ok(buffer)
    }
}

impl Display for UnsubscribeMessage {
    fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
        match self.max_messages {
            None => write!(f, "UNSUB {}\r\n", self.subscription_id),
            Some(n) => write!(f, "UNSUB {} {}\r\n", self.subscription_id, n),
        }
    }
}

impl FromStr for UnsubscribeMessage {
    type Err = NatsParseError;

    fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
        let res = parser::parse_unsub_header(s);
        match res {
            Some(r) => Ok(UnsubscribeMessage {
                subscription_id: r.sid,
                max_messages: r.max_messages,
            }),
            None => Err(NatsParseError {
                msg: "Failed to parse Unsubscribe message".to_string(),
            }),
        }
    }
}

/// Represents a publish message. This message conforms to the following format from the
/// NATS protocol documentation:
/// ```text
/// PUB <subject> [reply-to] <#bytes>\r\n[payload]\r\n
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct PublishMessage {
    pub subject: String,
    pub reply_to: Option<String>,
    pub payload_size: usize,
    pub payload: Vec<u8>,
}

impl PublishMessage {
    /// Constructor to create a new publish message
    pub fn new(subject: String, reply_to: Option<String>, payload: Vec<u8>) -> PublishMessage {
        PublishMessage {
            subject,
            reply_to,
            payload_size: payload.len(),
            payload,
        }
    }

    /// Single-allocation conversion from source data to a byte vector suitable for transmission
    pub fn as_vec(
        subject: &str,
        reply_to: Option<&str>,
        payload: &[u8],
    ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        // don't need exact capacity, can get close
        let capacity = 64 + payload.len();
        let mut buffer = Vec::with_capacity(capacity);
        write!(buffer, "PUB ")?;
        write!(buffer, "{}", subject)?;
        if let Some(rt) = reply_to {
            write!(buffer, " {}", rt)?;
        }
        write!(buffer, " {}", payload.len())?; // does not allocate
        write!(buffer, "\r\n")?;
        buffer.extend_from_slice(payload);
        write!(buffer, "\r\n")?;
        Ok(buffer)
    }
}

impl Display for PublishMessage {
    fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
        match self.reply_to {
            None => write!(
                f,
                "PUB {} {}\r\n{}\r\n",
                self.subject,
                self.payload_size,
                vec_to_str(&self.payload)
            ),
            Some(ref rt) => write!(
                f,
                "PUB {} {} {}\r\n{}\r\n",
                self.subject,
                rt,
                self.payload_size,
                vec_to_str(&self.payload)
            ),
        }
    }
}

impl FromStr for PublishMessage {
    type Err = NatsParseError;

    fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
        let split = parser::split_header_and_payload(s);
        match split {
            None => Err(NatsParseError {
                msg: "Failed to parse Publish message - possibly not a 2-line message".to_string(),
            }),
            Some(split) => {
                let res = parser::parse_pub_header(&split.0);
                match res {
                    Some(r) => Ok(PublishMessage {
                        subject: r.subject,
                        reply_to: r.reply_to,
                        payload_size: r.message_len,
                        payload: split.1,
                    }),
                    None => Err(NatsParseError {
                        msg: "Failed to parse Publish message".to_string(),
                    }),
                }
            }
        }
    }
}

fn vec_to_str(bytes: &Vec<u8>) -> String {
    let s = String::from_utf8(bytes.as_bytes().to_owned());
    match s {
        Ok(s) => s,
        Err(_) => "<<BAD PAYLOAD>>".to_string(),
    }
}

/// Indicates an error occurred during parsing of a NATS protocol message. Do not use this
/// type directly, instead use the error trait.
#[derive(Debug)]
pub struct NatsParseError {
    msg: String,
}

impl Error for NatsParseError {
    fn description(&self) -> &str {
        &self.msg
    }
}

impl Display for NatsParseError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}

mod parser;

#[cfg(test)]
mod tests {
    use super::{
        ConnectionInformation, DeliveredMessage, ProtocolMessage, PublishMessage,
        ServerInformation, SubscribeMessage, UnsubscribeMessage,
    };
    use std::str::FromStr;

    #[test]
    fn sub_roundtrip() {
        let msg = "SUB FOO group.test 99\r\n";
        let sub = SubscribeMessage::from_str(msg).unwrap();
        assert_eq!(sub.subject, "FOO");
        assert_eq!(sub.subscription_id, 99);
        assert_eq!(sub.queue_group, Some("group.test".to_string()));
        let out = format!("{}", sub);
        assert_eq!(out, msg);
    }

    #[test]
    fn sub_roundtrip_irreg_whitespace() {
        let msg = "SUB   \t  FOO   \t group.test   \t 99\r\n";
        let sub = SubscribeMessage::from_str(msg).unwrap();
        assert_eq!(sub.subject, "FOO");
        assert_eq!(sub.subscription_id, 99);
        assert_eq!(sub.queue_group, Some("group.test".to_string()));
        let out = format!("{}", sub);
        assert_eq!(out, "SUB FOO group.test 99\r\n");
    }

    #[test]
    fn unsub_roundtrip() {
        let msg = "UNSUB 21 40\r\n";
        let unsub = UnsubscribeMessage::from_str(msg).unwrap();
        assert_eq!(unsub.subscription_id, 21);
        assert_eq!(unsub.max_messages, Some(40));
        let out = format!("{}", unsub);
        assert_eq!(out, msg);
    }

    #[test]
    fn pub_roundtrip() {
        let msg = "PUB FOO 11\r\nHello NATS!\r\n";
        let pubm = PublishMessage::from_str(msg).unwrap();
        assert_eq!(pubm.payload_size, 11);
        assert_eq!(pubm.subject, "FOO");
        assert_eq!(pubm.reply_to, None);
        assert_eq!(pubm.payload, b"Hello NATS!");
        let out = format!("{}", pubm);
        assert_eq!(out, msg);
    }

    #[test]
    fn pub_roundtrip_irreg_whitespace() {
        let msg = "PUB     FOO  \t\t   11\r\nHello NATS!\r\n";
        let pubm = PublishMessage::from_str(msg).unwrap();
        assert_eq!(pubm.payload_size, 11);
        assert_eq!(pubm.subject, "FOO");
        assert_eq!(pubm.reply_to, None);
        assert_eq!(pubm.payload, b"Hello NATS!");
        let out = format!("{}", pubm);
        assert_eq!(out, "PUB FOO 11\r\nHello NATS!\r\n");
    }

    #[test]
    fn msg_roundtrip() {
        let msg = "MSG FOO.BAR 9 INBOX.34 11\r\nHello World\r\n";
        let mmsg = DeliveredMessage::from_str(msg).unwrap();
        assert_eq!(mmsg.reply_to, Some("INBOX.34".to_string()));
        assert_eq!(mmsg.payload_size, 11);
        assert_eq!(mmsg.subscription_id, 9);
        assert_eq!(mmsg.subject, "FOO.BAR");
        assert_eq!(mmsg.payload, b"Hello World");
        let out = format!("{}", mmsg);
        assert_eq!(out, msg);
    }

    #[test]
    fn msg_roundtrip_irreg_whitespace() {
        let msg = "MSG \t  \t  FOO.BAR   \t 9   \t  INBOX.34 11\r\nHello World\r\n";
        let mmsg = DeliveredMessage::from_str(msg).unwrap();
        assert_eq!(mmsg.reply_to, Some("INBOX.34".to_string()));
        assert_eq!(mmsg.payload_size, 11);
        assert_eq!(mmsg.subscription_id, 9);
        assert_eq!(mmsg.subject, "FOO.BAR");
        assert_eq!(mmsg.payload, b"Hello World");
        let out = format!("{}", mmsg);
        assert_eq!(out, "MSG FOO.BAR 9 INBOX.34 11\r\nHello World\r\n");
    }

    #[test]
    fn serverinfo_roundtrip() {
        let msg = r#"INFO {"server_id":"1ec445b504f4edfb4cf7927c707dd717",
        "version":"0.6.6","go":"go1.4.2","host":"0.0.0.0",
        "port":4222,"auth_required":false,"tls_required":false,
        "max_payload":1048576}"#;

        let si = ServerInformation::from_str(msg);
        assert!(si.is_ok());
        if let Ok(info) = si {
            assert_eq!(info.connect_urls, None);
            assert_eq!(info.server_id, "1ec445b504f4edfb4cf7927c707dd717");
            assert_eq!(info.go, "go1.4.2");
            assert_eq!(info.version, "0.6.6");
            assert_eq!(info.max_payload, 1048576);
            assert_eq!(info.tls_required, false);
            assert_eq!(info.port, 4222);
            assert_eq!(info.host, "0.0.0.0");
        }
    }

    #[test]
    fn connect_roundtrip() {
        let msg = r#"CONNECT {"verbose":false,"pedantic":false,"tls_required":false,"lang":"go","name":"testing","version":"1.2.2","protocol":1}"#;
        let ci = ConnectionInformation::from_str(msg);
        println!("{:?}", ci);
        assert!(ci.is_ok());
        if let Ok(info) = ci {
            assert_eq!(info.name, "testing");
            assert_eq!(info.pedantic, false);
            assert_eq!(info.tls_required, false);

            let out = format!("{}", info);
            assert_eq!(out, format!("{}\r\n", msg));
        }
    }

    #[test]
    fn enum_round() {
        let publish = ProtocolMessage::Publish(PublishMessage {
            subject: "workdispatch".to_string(),
            reply_to: None,
            payload_size: 11,
            payload: b"Hello World".to_vec(),
        });
        let out = format!("{}", publish);
        let pub2 = ProtocolMessage::from_str(&out).unwrap();
        assert_eq!(publish, pub2);
        assert_eq!(out, "PUB workdispatch 11\r\nHello World\r\n");
    }

    #[test]
    fn pubmessage_bytes_roundtrip() {
        let vec = PublishMessage::as_vec("workdispatch", None, b"Hello World").unwrap();
        let outstring = String::from_utf8(vec).unwrap();
        assert_eq!(
            outstring,
            format!(
                "PUB workdispatch {}\r\nHello World\r\n",
                b"Hello World".len()
            )
        );
    }

    #[test]
    fn submessage_bytes_roundtrip() {
        let vec = SubscribeMessage::as_vec("workdispatch", Some("myservice"), 99).unwrap();
        let outstring = String::from_utf8(vec).unwrap();
        assert_eq!(outstring, format!("SUB workdispatch myservice 99\r\n"));
    }

    #[test]
    fn unsubmessage_bytes_roundtrip() {
        let vec = UnsubscribeMessage::as_vec(84, Some(1)).unwrap();
        let outstring = String::from_utf8(vec).unwrap();
        assert_eq!(outstring, format!("UNSUB 84 1\r\n"));
    }
}