byte_size/builder.rs
1use std::collections::{HashSet};
2use bimap::BiHashMap;
3use crate::engine::Engine;
4
5///Compress/decompress with specific options
6///
7/// Use the builder class to compress and decompress using specific options.
8///
9/// If you're not sure about the options, either use [Builder::default] or use the convenience functions [Engine::compress] and [Engine::decompress]
10pub struct Builder {
11 custom: Vec<& 'static str>,
12 custom_spaces: bool,
13}
14
15impl Default for Builder {
16 fn default() -> Self {
17 Self {
18 custom: vec!["http://", "https://", ".com", "\n\r\n", "\r\n\r", "C:\\", ".co.uk"],
19 custom_spaces: false,
20 }
21 }
22}
23
24impl Builder {
25
26 ///Create a builder with an empty custom list
27 pub fn empty() -> Self {
28 Self {
29 custom: Vec::new(),
30 custom_spaces: false,
31 }
32 }
33
34
35
36 ///Move in a new list of custom words
37 ///
38 /// sss supports the use of 32 custom strings which are encoded as two bytes. This list can be replaced completely with this [Builder::set_custom] function,
39 /// or it can be modified with the [Builder::push_custom] or [Builder::clear_custom] functions.
40 ///
41 /// Note: The protocol only supports 32 custom strings, so only the first 32 strings will be used in the custom vector. Adding more than 32 is not an error, but these extra strings will not be used.
42 pub fn set_custom(& mut self, list: Vec<& 'static str>) -> & mut Self {
43 self.custom = list;
44 self
45 }
46
47 ///Determines whether the custom words will automatically support space prefixes.
48 ///
49 /// If true, the maximum number of possible custom strings in the table is halved from 32 to 16.
50 pub fn set_custom_spaces(& mut self, spaces: bool) -> & mut Self {
51 self.custom_spaces = spaces;
52 self
53 }
54
55 ///Appends a single string to the custom list. See the [Builder::set_custom] for more information on custom strings.
56 pub fn push_custom(& mut self, custom: & 'static str) -> & mut Self {
57 self.custom.push(custom);
58 self
59 }
60
61 ///Clears the custom list. See the [Builder::set_custom] for more information on custom strings.
62 pub fn clear_custom(& mut self) -> & mut Self {
63 self.custom.clear();
64 self
65 }
66
67 ///Returns the current length of the custom string list
68 pub fn len_custom(& self) -> usize {
69 self.custom.len()
70 }
71
72 ///Converts the builder into an [Engine]
73 pub fn engine(&self) -> Engine {
74
75 let max_len = if self.custom_spaces { 16 } else { 32 };
76
77 let v = if self.custom.len() <= max_len { self.custom.clone() } else { (&self.custom[0..max_len]).to_vec() };
78
79 let mut map = BiHashMap::new();
80 let mut lengths = HashSet::new();
81
82 for (i, string) in v.iter().enumerate() {
83 map.insert(string.as_bytes(), i);
84 lengths.insert(string.len());
85
86 }
87
88 let mut lengths: Vec<_> = lengths.iter().map(|x| *x).collect();
89 lengths.sort();
90 lengths.reverse();
91
92 Engine {
93 custom_spaces: self.custom_spaces,
94 custom_map: map,
95 lengths,
96 }
97 }
98
99}