djin_protocol/types/
string.rs1use crate::{hint, types, util, Parcel, Error, Settings};
2use std::io::prelude::*;
3use std;
4
5impl Parcel for std::string::String
7{
8 const TYPE_NAME: &'static str = "String";
9
10 fn read_field(read: &mut dyn Read,
11 settings: &Settings,
12 hints: &mut hint::Hints) -> Result<Self, Error> {
13 let bytes: Vec<u8> = util::read_list(read, settings, hints)?;
14
15 Ok(std::string::String::from_utf8(bytes)?)
16 }
17
18 fn write_field(&self, write: &mut dyn Write,
19 settings: &Settings,
20 hints: &mut hint::Hints) -> Result<(), Error> {
21 let bytes: Vec<u8> = self.bytes().collect();
22 util::write_list(&bytes, write, settings, hints)
23 }
24}
25
26#[derive(Clone, Debug, PartialEq)]
29pub struct String<S: types::Integer = u32>
30{
31 pub value: std::string::String,
32 _a: std::marker::PhantomData<S>,
33}
34
35impl<S: types::Integer> String<S>
36{
37 pub fn new(s: std::string::String) -> Self {
38 String {
39 value: s,
40 _a: std::marker::PhantomData,
41 }
42 }
43}
44
45impl<S: types::Integer> Parcel for String<S>
46{
47 const TYPE_NAME: &'static str = "djin_protocol::String<S>";
48
49 fn read_field(read: &mut dyn Read,
50 settings: &Settings,
51 hints: &mut hint::Hints) -> Result<Self, Error> {
52 let bytes = types::Vec::<S, u8>::read_field(read, settings, hints)?;
53
54 Ok(String::new(std::string::String::from_utf8(bytes.elements)?))
55 }
56
57 fn write_field(&self, write: &mut dyn Write,
58 settings: &Settings,
59 hints: &mut hint::Hints) -> Result<(), Error> {
60 let array: types::Vec<S, u8> = types::Vec::new(self.value.bytes().collect());
61 array.write_field(write, settings, hints)
62 }
63}
64