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
//! Encode items that implement formatting traits like `Debug` and `Display`.
//!
//! To encode an item that implements `Debug`:
//!
//! ```
//! extern crate bytes;
//! extern crate tokio_fmt_encoder;
//! extern crate tokio_io;
//!
//! fn main() {
//!     use bytes::BytesMut;
//!     use std::fmt::Formatter;
//!     use tokio_fmt_encoder::DebugEncoder;
//!     use tokio_io::codec::Encoder;
//!
//!     let to_encode = Some(10);
//!     let mut buffer = BytesMut::with_capacity(64);
//!     let mut encoder: DebugEncoder<Option<usize>> = Default::default();
//!     encoder.encode(to_encode, &mut buffer).unwrap();
//!     assert_eq!(&buffer.take(), &"Some(10)\n");
//! }
//! ```
//!
//! To encode an item that implements `Display`:
//!
//! ```
//! extern crate bytes;
//! extern crate tokio_fmt_encoder;
//! extern crate tokio_io;
//!
//! fn main() {
//!     use bytes::BytesMut;
//!     use std::fmt::Formatter;
//!     use tokio_fmt_encoder::DisplayEncoder;
//!     use tokio_io::codec::Encoder;
//!
//!     let to_encode = String::from("hello");
//!     let mut buffer = BytesMut::with_capacity(64);
//!     let mut encoder: DisplayEncoder<String> = Default::default();
//!     encoder.encode(to_encode, &mut buffer).unwrap();
//!     assert_eq!(&buffer.take(), &"hello\n");
//! }
//! ```
//!
#![deny(warnings)]
#![allow(deprecated)]
extern crate bytes;
extern crate tokio_io;

use bytes::BytesMut;
use std::fmt::{Debug, Display, Write};
use std::marker::PhantomData;
use tokio_io::codec::Encoder;


#[derive(Debug)]
pub enum Error {
    Fmt(std::fmt::Error),
    Io(std::io::Error),
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::Io(e)
    }
}

/// Encode items that implement `Debug`, separated by newlines.
pub struct DebugEncoder<I> {
    _i: PhantomData<I>, // TODO can this be removed?
}

impl<I> Default for DebugEncoder<I> {
    fn default() -> Self {
        Self { _i: PhantomData }
    }
}

impl<I: Debug> Encoder for DebugEncoder<I> {
    type Item = I;
    type Error = Error;

    fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
        writeln!(dst, "{:?}", item).map_err(Error::Fmt)
    }
}

/// Encode items that implement `Display`, separated by newlines.
pub struct DisplayEncoder<I> {
    _i: PhantomData<I>, // TODO can this be removed?
}

impl<I> Default for DisplayEncoder<I> {
    fn default() -> Self {
        Self { _i: PhantomData }
    }
}

impl<I: Display> Encoder for DisplayEncoder<I> {
    type Item = I;
    type Error = Error;

    fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
        writeln!(dst, "{}", item).map_err(Error::Fmt)
    }
}