sardine/blobs/
basic_blob.rs

1use std::io::Read;
2use std::io::Write;
3
4use blobs::Blob;
5use messages::Message;
6use srd_errors::SrdError;
7use Result;
8
9#[derive(Debug, PartialEq, Eq, Clone)]
10pub struct BasicBlob {
11    username: String,
12    password: String,
13}
14
15impl BasicBlob {
16    pub fn new(username: &str, password: &str) -> BasicBlob {
17        BasicBlob {
18            username: username.to_string(),
19            password: password.to_string(),
20        }
21    }
22}
23impl Blob for BasicBlob {
24    fn blob_type() -> &'static str {
25        "Basic"
26    }
27}
28
29impl Message for BasicBlob {
30    fn read_from<R: Read>(reader: &mut R) -> Result<Self>
31    where
32        Self: Sized,
33    {
34        let mut str_buffer = Vec::new();
35        reader.read_to_end(&mut str_buffer)?;
36        let full_str = String::from_utf8_lossy(str_buffer.as_slice()).to_string();
37
38        let v: Vec<&str> = full_str.split(':').collect();
39
40        if v.len() != 2 {
41            return Err(SrdError::BlobFormatError);
42        }
43
44        Ok(BasicBlob::new(v[0], v[1]))
45    }
46
47    fn write_to<W: Write>(&self, writer: &mut W) -> Result<()> {
48        let mut full_str = self.username.clone();
49        full_str.push_str(":");
50        full_str.push_str(&self.password);
51        writer.write_all(full_str.as_bytes())?;
52        Ok(())
53    }
54}