This commit is contained in:
Joshua Perry 2024-11-04 03:16:40 +00:00
parent f7f0ff88e0
commit 1114207f47
4 changed files with 38 additions and 44 deletions

View File

@ -8,19 +8,17 @@ pub fn encrypt_message(message: &mut Message, circle_length: &usize) -> Vec<Circ
)
}
fn caesar_shift(char: &char, shift: &u8) -> char {
shift.wrapping_add(*char as u8) as char
pub fn caesar_shift(char: &char, shift: &mut u8, shift_delta: i8) -> char {
let char = shift.wrapping_add(*char as u8);
*shift = shift.wrapping_add_signed(shift_delta);
char as char
}
pub type Circle = Vec<message::Triple>;
pub fn circle_to_points(circle: &Circle) -> Vec<f64> {
circle.iter().flat_map(|triple| {
vec![
triple.first as u8 as f64,
triple.second as u8 as f64,
triple.third as u8 as f64
]
Vec::<f64>::from(triple.clone())
}).collect()
}
@ -33,26 +31,15 @@ fn triples_into_circles(triples: &Vec<message::Triple>, circle_length: &usize) -
fn encode_circles(circles: &mut Vec<Circle>, shift: &mut u8) -> Vec<Circle> {
circles.iter_mut().enumerate().map(|(count, circle)| {
let shift_delta = match count {
count if count % 2 == 0 => count.add(1) as i8,
_ => -(count.add(1) as i8)
};
let shift_delta = get_shift_delta(&count);
let circle_iter = circle.iter_mut();
let new_circle = match count {
0 => circle_iter.skip(1),
_ => circle_iter.skip(0),
}.map(|triple| {
triple.first = caesar_shift(&triple.first, shift);
*shift = shift.wrapping_add_signed(shift_delta);
triple.second = caesar_shift(&triple.second, shift);
*shift = shift.wrapping_add_signed(shift_delta);
triple.second = caesar_shift(&triple.third, shift);
*shift = shift.wrapping_add_signed(shift_delta);
triple.clone()
}).collect::<Circle>();
triple.clone().encrypt(shift, shift_delta)
}).collect();
let new_circle = match count {
0 => {
@ -62,8 +49,14 @@ fn encode_circles(circles: &mut Vec<Circle>, shift: &mut u8) -> Vec<Circle> {
},
_ => new_circle,
};
new_circle
}).collect()
}
fn get_shift_delta(count: &usize) -> i8 {
match count {
count if count % 2 == 0 => count.add(1) as i8,
_ => -(count.add(1) as i8)
}
}

View File

@ -12,19 +12,11 @@ pub fn gui() -> anyhow::Result<()> {
Ok(())
}
#[derive(Default)]
struct Gui {
message: Option<crate::Message>,
circle_length: Option<usize>,
}
impl Default for Gui {
fn default() -> Self {
Self{
message: None,
circle_length: None,
}
}
}
impl eframe::App for Gui {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {

View File

@ -41,6 +41,7 @@ pub enum OutputMode {
}
pub use cli::display_plot;
pub use encoding::caesar_shift;
pub use encoding::Circle;
pub use encoding::circle_to_points;
pub use encoding::encrypt_message;

View File

@ -66,16 +66,15 @@ impl Triple {
third,
}
}
pub fn encrypt(mut self, shift: &mut u8, shift_delta: i8) -> Self {
self.first = crate::caesar_shift(&self.first, shift, shift_delta);
self.second = crate::caesar_shift(&self.second, shift, shift_delta);
self.second = crate::caesar_shift(&self.third, shift, shift_delta);
pub fn filler() -> Self {
let filler = 0 as char;
Self {
first: filler,
second: filler,
third: filler,
}
self
}
}
impl IntoIterator for Triple {
type Item = char;
type IntoIter = IntoIter<char>;
@ -84,3 +83,12 @@ impl IntoIterator for Triple {
vec![self.first, self.second, self.third].into_iter()
}
}
impl From<Triple> for Vec<f64> {
fn from(triple: Triple) -> Self {
vec![
triple.first as u8 as f64,
triple.second as u8 as f64,
triple.third as u8 as f64,
]
}
}