~julienxx/castor

Validate URL before adding to bookmarks v1 APPLIED

alex wennerberg: 1
 Validate URL before adding to bookmarks

 2 files changed, 12 insertions(+), 1 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~julienxx/castor/patches/10826/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Validate URL before adding to bookmarks Export this patch

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

Throws an error dialog if URL is invalid before adding to bookmarks
---
 src/bookmarks.rs | 8 ++++++++
 src/main.rs      | 5 ++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/bookmarks.rs b/src/bookmarks.rs
index 3a5b8ea..6ae6a96 100644
--- a/src/bookmarks.rs
+++ b/src/bookmarks.rs
@@ -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();
diff --git a/src/main.rs b/src/main.rs
index 240a3d6..4fbff8f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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.");
        }
    }
}

-- 
2.20.1
Thanks for the patch!