buss_protocol/settings/
mod.rs

1use crate::ToBytes;
2
3/// Settings are values passed during both request and response
4/// that are supposed to alter the default behavior of the client/server.
5/// They can also carry extra information for the protocol.
6/// The settings type/flag are of type u8 (1 byte).
7/// This means there are 256 possible settings.
8/// Values from 0 to 254(0xfe) are reserved for standard settings values.
9/// For your custom settings, value 0xff(255) indicates custom settings.
10#[repr(u8)]
11#[derive(Copy, Clone)]
12pub enum BussSettings {
13    /// The number of bytes contained in body content
14    BodyLength = 0,
15    /// The host domain name that this request was requested for
16    Host,
17    /// The custom tag
18    Custom = 0xff,
19}
20
21/// Provides easy conversion of various settings type into binary data
22pub trait BaseSettings {
23    /// Get the BussSettings type this setting represents
24    fn get_type(&self) -> BussSettings;
25    /// Get the body in bytes of this settings
26    fn get_body(&self) -> Vec<u8>;
27}
28
29/// User defined settings in byte form, ready to be passed to an encoder/decoder
30pub struct CustomSettings {
31    /// The custom type
32    custom_tag: u8,
33    /// The bytes of the custom settings
34    bytes: Vec<u8>,
35}
36
37impl CustomSettings {
38    /// New settings in byte form
39    pub fn new(custom_tag: u8, bytes: Vec<u8>) -> Self {
40        CustomSettings { custom_tag, bytes }
41    }
42}
43
44impl BaseSettings for CustomSettings {
45    fn get_type(&self) -> BussSettings {
46        BussSettings::Custom
47    }
48    fn get_body(&self) -> Vec<u8> {
49        let mut bytes: Vec<u8> = Vec::new();
50        bytes.push(self.get_type() as u8);
51        bytes.push(self.custom_tag);
52        bytes.extend(self.bytes.iter());
53        bytes
54    }
55}
56
57/// Holds the list of settings to be written to the byte stream
58pub struct Settings {
59    /// List of settings
60    entries: Vec<Box<dyn BaseSettings>>,
61}
62
63impl Settings {
64    /// Creates a new settings pool
65    pub fn new() -> Self {
66        Self {
67            entries: Vec::new(),
68        }
69    }
70    /// add a settings
71    pub fn add(&mut self, entry: Box<dyn BaseSettings>) {
72        self.entries.push(entry);
73    }
74
75    pub fn iter(&self) -> std::slice::Iter<'_, Box<dyn BaseSettings>> {
76        self.entries.iter()
77    }
78}
79
80impl ToBytes for Settings {
81    fn to_bytes(self) -> Vec<u8> {
82        let mut bytes = Vec::new();
83        // Write length
84        let length = self.entries.len() as u16;
85        bytes.extend(length.to_be_bytes());
86        for s in self.entries {
87            bytes.push(s.get_type() as u8);
88            bytes.append(&mut s.get_body());
89        }
90
91        bytes
92    }
93}
94
95impl Default for Settings {
96    fn default() -> Self {
97        Self::new()
98    }
99}
100
101/// BodyLength header/settings
102/// Represents the number of bytes in the body of respose/request
103pub struct BodyLength {
104    /// Number of bodies
105    length: usize,
106}
107
108impl BodyLength {
109    /// New body length
110    pub fn new(size: usize) -> Self {
111        BodyLength { length: size }
112    }
113    /// Returns the length of body
114    pub fn get_length(&self) -> usize {
115        self.length
116    }
117}
118
119impl BaseSettings for BodyLength {
120    fn get_type(&self) -> BussSettings {
121        BussSettings::BodyLength
122    }
123    fn get_body(&self) -> Vec<u8> {
124        self.length.to_be_bytes().to_vec()
125    }
126}
127
128/// Host
129pub struct Host {
130    /// The host domain name
131    host: String,
132}
133
134impl Host {
135    pub fn new(host: &str) -> Self {
136        Host {
137            host: String::from(host),
138        }
139    }
140}
141impl BaseSettings for Host {
142    fn get_type(&self) -> BussSettings {
143        BussSettings::Host
144    }
145    // TODO This should consume self to be efficient
146    fn get_body(&self) -> Vec<u8> {
147        let mut bytes = Vec::new();
148        bytes.extend(self.host.len().to_be_bytes());
149        bytes.append(&mut self.host.clone().into_bytes());
150        bytes
151    }
152}