Skip to content

Commit

Permalink
Refactored to make code more safe
Browse files Browse the repository at this point in the history
  • Loading branch information
pomettini committed Jul 30, 2019
1 parent 4df0480 commit 8b07673
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
49 changes: 28 additions & 21 deletions src/bin/golden-frieza.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]

extern crate color_convert;
extern crate csv;
extern crate golden_frieza;
Expand Down Expand Up @@ -48,15 +50,21 @@ fn main() {
let mut window = Window::new(&ui, "Golden Frieza", 800, 600, WindowType::NoMenubar);

// Initialize color dictionaries
let colors: Rc<RefCell<Color>> = Rc::new(RefCell::new(Default::default()));
let colors: Rc<RefCell<Color>> = Rc::new(RefCell::new(golden_frieza::Color::default()));
// TODO: Show an alert if file is not found
let dictionary = Path::new("resources/colors.csv");
colors.borrow_mut().load_dictionary(&dictionary);
let dictionary_path = Path::new("resources/colors.csv");
colors.borrow_mut().load_dictionary(dictionary_path);

let dictionary = match DisplayColors::load_dictionary(Path::new("resources/display_colors.csv"))
{
Ok(dict) => dict,
Err(err) => {
window.modal_err(&ui, "Warning", err);
return;
}
};

let display_colors: Rc<RefCell<DisplayColors>> = Rc::new(RefCell::new(
// TODO: Show an alert if file is not found
DisplayColors::load_dictionary(Path::new("resources/display_colors.csv")).unwrap(),
));
let display_colors: Rc<RefCell<DisplayColors>> = Rc::new(RefCell::new(dictionary));

// Text labels and bars
let text_labels: Rc<RefCell<HashMap<String, Label>>> = Rc::new(RefCell::new(HashMap::new()));
Expand All @@ -73,7 +81,7 @@ fn main() {
let mut process_button = Button::new(&ui, "Process Data");

let mut website_hbox = HorizontalBox::new(&ui);
website_hbox.append(&ui, website_entry.clone(), LayoutStrategy::Stretchy);
website_hbox.append(&ui, website_entry, LayoutStrategy::Stretchy);
website_hbox.append(&ui, load_website_button.clone(), LayoutStrategy::Compact);
website_hbox.set_padded(&ui, true);

Expand Down Expand Up @@ -133,7 +141,7 @@ fn main() {

let color_area = Area::new(&ui, Box::new(color_canvas));
temp_vbox.append(&ui, color_area, LayoutStrategy::Compact);
output_vbox.append(&ui, temp_vbox.clone(), LayoutStrategy::Compact);
output_vbox.append(&ui, temp_vbox, LayoutStrategy::Compact);

window.set_child(&ui, horizontal_box);
window.show(&ui);
Expand Down Expand Up @@ -175,22 +183,21 @@ fn main() {

load_file_button.on_clicked(&ui, {
let ui = ui.clone();
let window = window.clone();
let mut entry = entry.clone();
let colors = colors.clone();
let window = window;
let mut entry = entry;
let colors = colors;
move |_| {
let path = match window.open_file(&ui) {
Some(p) => p,
None => {
window.modal_err(&ui, "Warning", "Please enter a valid file");
return;
}
let path = if let Some(path_) = window.open_file(&ui) {
path_
} else {
window.modal_err(&ui, "Warning", "Please enter a valid file");
return;
};

let document = match Document::from_file(&path) {
Ok(d) => d,
Err(_e) => {
window.modal_err(&ui, "Warning", "Please enter a valid text document");
Ok(doc) => doc,
Err(err) => {
window.modal_err(&ui, "Warning", err);
return;
}
};
Expand Down
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]

extern crate ammonia;
extern crate csv;
extern crate regex;
Expand Down Expand Up @@ -41,7 +43,7 @@ impl DisplayColors {
dictionary.insert(record.0, rgb);
}

Ok(DisplayColors { dictionary })
Ok(Self { dictionary })
}

pub fn blend_colors(&self, dictionary: HashMap<String, f32>) -> RGB {
Expand Down Expand Up @@ -75,26 +77,26 @@ pub struct Document {
}

impl Document {
pub fn from_text(text: &String) -> Document {
Document {
pub fn from_text(text: &str) -> Self {
Self {
content: text.to_string(),
}
}

pub fn from_file(path: &Path) -> Result<Document, &str> {
pub fn from_file(path: &Path) -> Result<Self, &str> {
let mut file = match File::open(&path) {
Ok(f) => f,
Err(_) => return Err("Cannot open the file"),
};
let mut contents = String::new();

match file.read_to_string(&mut contents) {
Ok(_) => Ok(Document { content: contents }),
Ok(_) => Ok(Self { content: contents }),
Err(_) => Err("Cannot parse the file"),
}
}

pub fn from_website(url: &str) -> Option<Document> {
pub fn from_website(url: &str) -> Option<Self> {
// TODO: Handle errors and exceptions

let mut request = reqwest::get(url).expect("URL not valid");
Expand Down Expand Up @@ -124,7 +126,7 @@ impl Document {

println!("RESULT: {:?}", &clean_text);

Some(Document {
Some(Self {
content: clean_text,
})
}
Expand Down
4 changes: 3 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]

use super::*;

macro_rules! INIT_COLOR_DICTIONARY {
($colors:ident, $dictionary:ident) => {
let mut $colors: Color = Default::default();
let mut $colors: Color = Color::default();
let $dictionary = Path::new("resources/test/colors.csv");
$colors.load_dictionary(&$dictionary);
};
Expand Down

0 comments on commit 8b07673

Please sign in to comment.