/ src / message.rs
message.rs
 1  use std::vec::IntoIter;
 2  
 3  #[derive(Clone)]
 4  pub struct Message {
 5      pub parts: Parts,
 6      pub combined: String,
 7  }
 8  impl Message {
 9      pub fn new(content: String, start_shift: u8) -> Self {
10          let parts = Parts::new(content, start_shift);
11          let combined = parts.shift_to_string() + &parts.content;
12          Self {
13              parts,
14              combined,
15          }
16      }
17  
18      pub fn to_triples(&self) -> Vec<Triple> {
19          self.combined.chars()
20              .collect::<Vec<char>>()
21              .chunks(3)
22              .filter_map(|chunk| {
23                  Some(Triple::new(chunk[0], chunk[1], chunk[2]))
24              }
25          ).collect()
26      }
27  }
28  #[derive(Clone)]
29  pub struct Parts {
30      pub content: String,
31      pub start_shift: u8,
32  }
33  impl Parts {
34      fn new(content: String, start_shift: u8) -> Self {
35          let mut parts = Self {
36              content,
37              start_shift,
38          };
39          parts.standardize();
40          
41          parts
42      }
43  
44      fn standardize(&mut self) {
45          let delta = self.content.len() % 3;
46          match delta {
47              0 => (),
48              _ => { self.content += &"_".repeat(3 - delta) },
49          }
50      }
51  
52      fn shift_to_string(&self) -> String {
53          let string = self.start_shift.to_string(); 
54          "0".repeat(3 - string.len()) + &string
55      }
56  }
57  #[derive(Clone, Debug)]
58  pub struct Triple {
59      pub first: char,
60      pub second: char,
61      pub third: char
62  }
63  impl Triple {
64      fn new(first: char, second: char, third: char) -> Self {
65          Self {
66              first,
67              second,
68              third,
69          }
70      }
71      pub fn encrypt(mut self, shift: &mut u8, shift_delta: i8) -> Self {
72          self.first = crate::caesar_shift(&self.first, shift, shift_delta);
73          self.second = crate::caesar_shift(&self.second, shift, shift_delta);
74          self.second = crate::caesar_shift(&self.third, shift, shift_delta); 
75  
76          self
77      }
78  }
79  
80  impl IntoIterator for Triple {
81      type Item = char;
82      type IntoIter = IntoIter<char>;
83  
84      fn into_iter(self) -> Self::IntoIter {
85          vec![self.first, self.second, self.third].into_iter()
86      }
87  }
88  impl From<Triple> for Vec<f64> {
89      fn from(triple: Triple) -> Self {
90          vec![
91              triple.first as u8 as f64,
92              triple.second as u8 as f64,
93              triple.third as u8 as f64,
94          ]
95      }
96  }