Skip to content

Commit

Permalink
Update + format
Browse files Browse the repository at this point in the history
  • Loading branch information
kenf1 committed Dec 12, 2024
1 parent 1ae2502 commit d79947a
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 205 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ webscrape = ["reqwest/blocking", "select"]

[dependencies]
bardecoder = { version = "0.5.0", optional = true }
csv = { version = "1.3.0", optional = true }
csv = { version = "1.3.1", optional = true }
dotenv = { version = "0.15.0", optional = true }
image = { version = "0.24.9", optional = true }
polars = { version = "0.44.2", optional = true, features = ["lazy"] }
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
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 \
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ test_qr: ##Test qr features flag
cargo test --features "qr"

test_webscrape: ##Test webscrape features flag
cargo test --features "webscrape"
cargo test --features "webscrape"

update_toml: ##Update dependencies in Cargo.toml
cargo update --manifest-path Cargo.toml
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
max_width = 80
tab_spaces = 4
use_small_heuristics="Default"
40 changes: 24 additions & 16 deletions src/cryptlib.rs
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()
}
86 changes: 44 additions & 42 deletions src/imagefn.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
use image::{DynamicImage,GenericImageView,ImageFormat};
use reqwest::blocking::get;
use image::imageops::CatmullRom; //resize algorithm
use bardecoder;
use image::imageops::CatmullRom; //resize algorithm
use image::{DynamicImage, GenericImageView, ImageFormat};
use reqwest::blocking::get;

//import image from local path (allows error handling)
pub fn image_import(
image_path: &str
) -> Result<DynamicImage,Box<dyn std::error::Error>>{
image_path: &str,
) -> Result<DynamicImage, Box<dyn std::error::Error>> {
let temp_img = image::open(image_path)?;
Ok(temp_img)
}

//import image from url (allows error handling)
pub fn image_from_url(
url: &str
) -> Result<DynamicImage,Box<dyn std::error::Error>>{
let img_bytes = get(url)?
.bytes()?
.to_vec();
url: &str,
) -> Result<DynamicImage, Box<dyn std::error::Error>> {
let img_bytes = get(url)?.bytes()?.to_vec();

let image = image::load_from_memory(&img_bytes)?;
Ok(image)
}

//determine image dimensions & resize if any >= 600 pixels
pub fn image_dimensions(
image: DynamicImage,orig_cutoff: u32,new_cutoff: u32
) -> DynamicImage{
let (width,height) = image.dimensions();
image: DynamicImage,
orig_cutoff: u32,
new_cutoff: u32,
) -> DynamicImage {
let (width, height) = image.dimensions();
println!(
"Image dimensions are: {} pixels in width x {} pixels in height.",
width,height
width, height
);

if width >= orig_cutoff || height >= orig_cutoff {
Expand All @@ -39,27 +39,20 @@ pub fn image_dimensions(
orig_cutoff
);

let new_image = DynamicImage::resize(
&image,
new_cutoff,
new_cutoff,
CatmullRom
);
println!(
"Resized image dimensions: {:?}",
new_image.dimensions()
);
let new_image =
DynamicImage::resize(&image, new_cutoff, new_cutoff, CatmullRom);
println!("Resized image dimensions: {:?}", new_image.dimensions());

new_image
}else{
} else {
image
}
}

//decode image
pub fn image_decode(
image: DynamicImage
) -> Result<String,Box<dyn std::error::Error>>{
image: DynamicImage,
) -> Result<String, Box<dyn std::error::Error>> {
let decoder = bardecoder::default_decoder();

let decoded_items: Vec<String> = decoder
Expand All @@ -72,50 +65,59 @@ pub fn image_decode(

//wrapper function for local image
pub fn from_local(
image_path: &str,orig_cutoff: u32,new_cutoff: u32
) -> Result<String,Box<dyn std::error::Error>>{
image_path: &str,
orig_cutoff: u32,
new_cutoff: u32,
) -> Result<String, Box<dyn std::error::Error>> {
let qrcode = image_import(image_path)?;

//resize if nec
let tidy = image_dimensions(qrcode,orig_cutoff,new_cutoff);
let tidy = image_dimensions(qrcode, orig_cutoff, new_cutoff);

let res = image_decode(tidy)?;
Ok(res)
}

//wrapper function for remote image (same structure as from_local)
pub fn from_remote(
url: &str, orig_cutoff: u32,new_cutoff: u32
) -> Result<String, Box<dyn std::error::Error>>{
url: &str,
orig_cutoff: u32,
new_cutoff: u32,
) -> Result<String, Box<dyn std::error::Error>> {
let qrcode = image_from_url(url)?;

//resize if nec
let tidy = image_dimensions(qrcode,orig_cutoff,new_cutoff);
let tidy = image_dimensions(qrcode, orig_cutoff, new_cutoff);

let res = image_decode(tidy)?;
Ok(res)
}

//save image from url (keep original dimensions + aspect ratio)
pub fn save_img(
url: &str,path: &str,extension: ImageFormat
) -> Result<(),Box<dyn std::error::Error>>{
url: &str,
path: &str,
extension: ImageFormat,
) -> Result<(), Box<dyn std::error::Error>> {
let image = image_from_url(url)?;

image.save_with_format(path,extension)?;
image.save_with_format(path, extension)?;
Ok(())
}

//save image from url (resize w/ orig aspect ratio)
pub fn save_img_resized(
url: &str,orig_cutoff: u32,new_cutoff: u32,
path: &str,extension: ImageFormat
) -> Result<(),Box<dyn std::error::Error>>{
url: &str,
orig_cutoff: u32,
new_cutoff: u32,
path: &str,
extension: ImageFormat,
) -> Result<(), Box<dyn std::error::Error>> {
let image = image_from_url(url)?;

//resize image
let resized = image_dimensions(image,orig_cutoff,new_cutoff);
let resized = image_dimensions(image, orig_cutoff, new_cutoff);

resized.save_with_format(path,extension)?;
resized.save_with_format(path, extension)?;
Ok(())
}
}
Loading

0 comments on commit d79947a

Please sign in to comment.