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
use crate::codecs::DataFormat;
use crate::codecs::Encodable;
use std::io::Result;
use std::io::Write;
const ID: u64 = 3;
const MEDIA_TYPE: &str = "text/plain";
pub struct PlainTextDataFormat;
impl DataFormat for PlainTextDataFormat {
#[inline]
fn id() -> u64 {
ID
}
#[inline]
fn media_type() -> &'static str {
MEDIA_TYPE
}
}
impl<T: AsRef<str>> Encodable<PlainTextDataFormat> for T {
#[inline]
fn encode_to(&self, _format: &PlainTextDataFormat, w: &mut impl Write) -> Result<usize> {
w.write(self.as_ref().as_bytes())
}
}
#[cfg(test)]
mod test {
use super::*;
use std::io::Cursor;
#[test]
fn check_plain_text_encoder() {
let mut vec = Vec::<u8>::new();
let mut cursor = Cursor::new(&mut vec);
let df = PlainTextDataFormat;
let msg = "They are who we thought they are";
msg.encode_to(&df, &mut cursor).unwrap();
assert_eq!(cursor.position() as usize, msg.len());
msg.to_string().encode_to(&df, &mut cursor).unwrap();
assert_eq!(cursor.position() as usize, 2 * msg.len());
}
#[test]
fn check_data_format() {
assert_eq!(PlainTextDataFormat::id(), ID);
assert_eq!(PlainTextDataFormat::media_type(), MEDIA_TYPE);
}
}