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
use fd;
use std::io;
use Argdata;
use ReadError;
use StrValue;
use Value;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Str<'d> {
	value: &'d str,
}

/// Create an argdata value representing a string.
pub fn str<'d>(value: &'d str) -> Str<'d> {
	Str { value }
}

impl<'d> Str<'d> {
	pub fn str(&self) -> &'d str {
		self.value
	}
}

impl<'d> Argdata<'d> for Str<'d> {
	fn read<'a>(&'a self) -> Result<Value<'a, 'd>, ReadError>
	where
		'd: 'a,
	{
		Ok(Value::Str(StrValue::from_str(self.str())))
	}

	fn serialized_length(&self) -> usize {
		self.str().len() + 2
	}

	fn serialize(&self, writer: &mut io::Write, _: Option<&mut fd::FdMapping>) -> io::Result<()> {
		writer.write_all(&[8])?;
		writer.write_all(self.str().as_bytes())?;
		writer.write_all(&[0])?;
		Ok(())
	}
}

#[test]
fn str_serialize_test() {
	let s = str("blah");
	assert_eq!(s.serialized_length(), 6);
	let mut buf = Vec::new();
	s.serialize(&mut buf, None).unwrap();
	assert_eq!(&buf, b"\x08blah\x00");
}