~exprez135/castor

74ca692f69dd5e7c405a00f310db99fb0cf63a13 — alex wennerberg 1 year, 4 months ago e68f7fd
Validate URL before adding to bookmarks

Fixes issue #20 (https://todo.sr.ht/~julienxx/Castor/20)

Throws an error dialog if URL is invalid before adding to bookmarks
2 files changed, 12 insertions(+), 1 deletions(-)

M src/bookmarks.rs
M src/main.rs
M src/bookmarks.rs => src/bookmarks.rs +8 -0
@@ 4,6 4,14 @@ use std::fs;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::{Read, Write};
use url::Url;

pub fn is_valid(url: &str) -> bool {
    match Url::parse(&url) {
        Ok(_) => true,
        _ => false,
    }
}

pub fn add(url: &str) {
    let mut file = bookmarks_file();

M src/main.rs => src/main.rs +4 -1
@@ 161,10 161,13 @@ fn add_bookmark(gui: &Arc<Gui>) {
    let current_url = url_bar.get_text();

    if let Some(url) = current_url {
        if url != "" {
        if bookmarks::is_valid(&url) {
            bookmarks::add(&url);
            dialog::info(&gui, "Bookmark added.");
        }
        else {
            dialog::error(&gui, "Invalid bookmark URL.");
        }
    }
}