-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
174 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM rust:1.82-slim | ||
FROM rust:1.83-slim | ||
|
||
RUN apt-get update && apt-get install -y \ | ||
build-essential \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
max_width = 80 | ||
tab_spaces = 4 | ||
use_small_heuristics="Default" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
//encrypt & decrypt logic | ||
pub fn cryptfn( | ||
task: &str,input: &str,ref_dict: &str,shift: usize | ||
) -> Result<String, String>{ | ||
task: &str, | ||
input: &str, | ||
ref_dict: &str, | ||
shift: usize, | ||
) -> Result<String, String> { | ||
let mut charvec: Vec<char> = Vec::new(); | ||
|
||
for c in input.chars(){ | ||
match ref_dict.find(c){ | ||
for c in input.chars() { | ||
match ref_dict.find(c) { | ||
Some(v) => { | ||
let new_index = match task { | ||
//encrypt vs decrypt | ||
"encrypt" => (v + shift) % ref_dict.len(), | ||
"decrypt" => (v - shift + ref_dict.len()) % ref_dict.len(), //ensure non-negative index | ||
//ensure non-negative index | ||
"decrypt" => (v - shift + ref_dict.len()) % ref_dict.len(), | ||
_ => { | ||
//unaccepted input | ||
return Err(String::from("Options are: encrypt or decrypt")); | ||
return Err(String::from( | ||
"Options are: encrypt or decrypt", | ||
)); | ||
} | ||
}; | ||
|
||
//get char from new index -> append to vec | ||
if let Some(new_char) = ref_dict.chars().nth(new_index){ | ||
if let Some(new_char) = ref_dict.chars().nth(new_index) { | ||
charvec.push(new_char); | ||
} else { | ||
return Err(String::from("Error: reference dictionary is invalid.")); | ||
return Err(String::from( | ||
"Error: reference dictionary is invalid.", | ||
)); | ||
} | ||
}, | ||
} | ||
None => { | ||
//break when encounter char not in ref | ||
return Err(String::from("Error: entered string contains characters not found in reference dict.")); | ||
}, | ||
return Err(String::from( | ||
"Error: entered string contains characters not found in reference dict.", | ||
)); | ||
} | ||
} | ||
} | ||
|
||
Ok(vec_to_str(charvec)) | ||
} | ||
|
||
//convert Vec<char> -> String | ||
fn vec_to_str(input_vec: Vec<char>) -> String{ | ||
fn vec_to_str(input_vec: Vec<char>) -> String { | ||
//implicit return | ||
input_vec | ||
.iter() | ||
.collect() | ||
} | ||
input_vec.iter().collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.