Skip to content

Commit

Permalink
Add actions to bookmarks (#66)
Browse files Browse the repository at this point in the history
* Add actions

* Fix tests
  • Loading branch information
quambene authored Dec 4, 2023
1 parent 83437b7 commit fcdd6be
Show file tree
Hide file tree
Showing 16 changed files with 565 additions and 218 deletions.
127 changes: 100 additions & 27 deletions src/bookmark_reader/chromium.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{ReadBookmark, ReaderName};
use crate::{bookmarks::Source, utils, SourceBookmark, SourceBookmarks, SourceType};
use crate::{
bookmarks::{Source, SourceBookmarkBuilder},
utils, SourceBookmarks, SourceType,
};
use anyhow::anyhow;
use log::{debug, trace};
use serde_json::{Map, Value};
Expand All @@ -19,8 +22,9 @@ impl Chromium {
if type_value == "url" {
if let Some(Value::String(url_value)) = obj.get("url") {
if url_value.contains("http") {
let source_bookmark =
SourceBookmark::new(url_value.to_owned(), source.name.to_owned());
let source_bookmark = SourceBookmarkBuilder::new(url_value)
.add_source(&source.name)
.build();
source_bookmarks.insert(source_bookmark);
}
}
Expand Down Expand Up @@ -221,10 +225,7 @@ impl ReadBookmark for ChromiumNoExtensionBookmarkReader {
#[cfg(test)]
mod tests {
use super::*;
use std::{
collections::{HashMap, HashSet},
path::Path,
};
use std::{collections::HashMap, path::Path};

#[test]
fn test_parse_all() {
Expand All @@ -240,12 +241,41 @@ mod tests {
let res = bookmark_reader.parse(&bookmarks, &source, &mut source_bookmarks);
assert!(res.is_ok(), "{}", res.unwrap_err());

assert_eq!(source_bookmarks.inner(), HashMap::from_iter([
("https://www.deepl.com/translator".to_owned(), HashSet::from_iter([SourceType::Chromium])),
("https://www.quantamagazine.org/how-mathematical-curves-power-cryptography-20220919/".to_owned(), HashSet::from_iter([SourceType::Chromium])),
("https://en.wikipedia.org/wiki/Design_Patterns".to_owned(), HashSet::from_iter([SourceType::Chromium])),
("https://doc.rust-lang.org/book/title-page.html".to_owned(), HashSet::from_iter([SourceType::Chromium])),
]));
let url1 = "https://www.deepl.com/translator";
let url2 =
"https://www.quantamagazine.org/how-mathematical-curves-power-cryptography-20220919/";
let url3 = "https://en.wikipedia.org/wiki/Design_Patterns";
let url4 = "https://doc.rust-lang.org/book/title-page.html";

assert_eq!(
source_bookmarks.inner(),
HashMap::from_iter([
(
url1.to_owned(),
SourceBookmarkBuilder::new(url1)
.add_source(&SourceType::Chromium)
.build()
),
(
url2.to_owned(),
SourceBookmarkBuilder::new(url2)
.add_source(&SourceType::Chromium)
.build()
),
(
url3.to_owned(),
SourceBookmarkBuilder::new(url3)
.add_source(&SourceType::Chromium)
.build()
),
(
url4.to_owned(),
SourceBookmarkBuilder::new(url4)
.add_source(&SourceType::Chromium)
.build()
)
])
);
}

#[test]
Expand All @@ -262,12 +292,41 @@ mod tests {
let res = bookmark_reader.parse(&bookmarks, &source, &mut source_bookmarks);
assert!(res.is_ok(), "{}", res.unwrap_err());

assert_eq!(source_bookmarks.inner(), HashMap::from_iter([
("https://www.deepl.com/translator".to_owned(), HashSet::from_iter([SourceType::Chromium])),
("https://www.quantamagazine.org/how-mathematical-curves-power-cryptography-20220919/".to_owned(), HashSet::from_iter([SourceType::Chromium])),
("https://en.wikipedia.org/wiki/Design_Patterns".to_owned(), HashSet::from_iter([SourceType::Chromium])),
("https://doc.rust-lang.org/book/title-page.html".to_owned(), HashSet::from_iter([SourceType::Chromium])),
]));
let url1 = "https://www.deepl.com/translator";
let url2 =
"https://www.quantamagazine.org/how-mathematical-curves-power-cryptography-20220919/";
let url3 = "https://en.wikipedia.org/wiki/Design_Patterns";
let url4 = "https://doc.rust-lang.org/book/title-page.html";

assert_eq!(
source_bookmarks.inner(),
HashMap::from_iter([
(
url1.to_owned(),
SourceBookmarkBuilder::new(url1)
.add_source(&SourceType::Chromium)
.build()
),
(
url2.to_owned(),
SourceBookmarkBuilder::new(url2)
.add_source(&SourceType::Chromium)
.build()
),
(
url3.to_owned(),
SourceBookmarkBuilder::new(url3)
.add_source(&SourceType::Chromium)
.build()
),
(
url4.to_owned(),
SourceBookmarkBuilder::new(url4)
.add_source(&SourceType::Chromium)
.build()
)
])
);
}

#[test]
Expand All @@ -284,16 +343,23 @@ mod tests {
let res = bookmark_reader.parse(&bookmarks, &source, &mut source_bookmarks);
assert!(res.is_ok(), "{}", res.unwrap_err());

let url1 = "https://en.wikipedia.org/wiki/Design_Patterns";
let url2 = "https://doc.rust-lang.org/book/title-page.html";

assert_eq!(
source_bookmarks.inner(),
HashMap::from_iter([
(
"https://en.wikipedia.org/wiki/Design_Patterns".to_owned(),
HashSet::from_iter([SourceType::Chromium])
url1.to_owned(),
SourceBookmarkBuilder::new(url1)
.add_source(&SourceType::Chromium)
.build()
),
(
"https://doc.rust-lang.org/book/title-page.html".to_owned(),
HashSet::from_iter([SourceType::Chromium])
url2.to_owned(),
SourceBookmarkBuilder::new(url2)
.add_source(&SourceType::Chromium)
.build()
),
])
);
Expand All @@ -313,16 +379,23 @@ mod tests {
let res = bookmark_reader.parse(&bookmarks, &source, &mut source_bookmarks);
assert!(res.is_ok(), "{}", res.unwrap_err());

let url1 = "https://en.wikipedia.org/wiki/Design_Patterns";
let url2 = "https://doc.rust-lang.org/book/title-page.html";

assert_eq!(
source_bookmarks.inner(),
HashMap::from_iter([
(
"https://en.wikipedia.org/wiki/Design_Patterns".to_owned(),
HashSet::from_iter([SourceType::Chromium])
url1.to_owned(),
SourceBookmarkBuilder::new(url1)
.add_source(&SourceType::Chromium)
.build()
),
(
"https://doc.rust-lang.org/book/title-page.html".to_owned(),
HashSet::from_iter([SourceType::Chromium])
url2.to_owned(),
SourceBookmarkBuilder::new(url2)
.add_source(&SourceType::Chromium)
.build()
),
])
);
Expand Down
124 changes: 97 additions & 27 deletions src/bookmark_reader/firefox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{ReadBookmark, ReaderName};
use crate::{utils, Source, SourceBookmark, SourceBookmarks, SourceType};
use crate::{bookmarks::SourceBookmarkBuilder, utils, Source, SourceBookmarks, SourceType};
use anyhow::anyhow;
use log::{debug, trace};
use lz4::block;
Expand All @@ -25,8 +25,9 @@ impl Firefox {
if type_value == "text/x-moz-place" {
if let Some(Value::String(uri_value)) = obj.get("uri") {
if uri_value.contains("http") {
let source_bookmark =
SourceBookmark::new(uri_value.to_owned(), source.name.clone());
let source_bookmark = SourceBookmarkBuilder::new(uri_value)
.add_source(&source.name)
.build();
source_bookmarks.insert(source_bookmark);
}
}
Expand Down Expand Up @@ -316,10 +317,7 @@ impl ReadBookmark for FirefoxCompressedBookmarkReader {
mod tests {
use super::*;
use crate::{test_utils, utils};
use std::{
collections::{HashMap, HashSet},
io::Cursor,
};
use std::{collections::HashMap, io::Cursor};

#[test]
fn test_read() {
Expand Down Expand Up @@ -370,12 +368,41 @@ mod tests {
let res = bookmark_reader.parse(&raw_bookmarks, &source, &mut source_bookmarks);
assert!(res.is_ok(), "{}", res.unwrap_err());

assert_eq!(source_bookmarks.inner(), HashMap::from_iter([
("https://www.mozilla.org/en-US/firefox/central/".to_owned(), HashSet::from_iter([SourceType::Firefox])),
("https://www.quantamagazine.org/how-mathematical-curves-power-cryptography-20220919/".to_owned(), HashSet::from_iter([SourceType::Firefox])),
("https://en.wikipedia.org/wiki/Design_Patterns".to_owned(), HashSet::from_iter([SourceType::Firefox])),
("https://doc.rust-lang.org/book/title-page.html".to_owned(), HashSet::from_iter([SourceType::Firefox])),
]));
let url1 = "https://www.mozilla.org/en-US/firefox/central/";
let url2 =
"https://www.quantamagazine.org/how-mathematical-curves-power-cryptography-20220919/";
let url3 = "https://en.wikipedia.org/wiki/Design_Patterns";
let url4 = "https://doc.rust-lang.org/book/title-page.html";

assert_eq!(
source_bookmarks.inner(),
HashMap::from_iter([
(
url1.to_owned(),
SourceBookmarkBuilder::new(url1)
.add_source(&SourceType::Firefox)
.build()
),
(
url2.to_owned(),
SourceBookmarkBuilder::new(url2)
.add_source(&SourceType::Firefox)
.build()
),
(
url3.to_owned(),
SourceBookmarkBuilder::new(url3)
.add_source(&SourceType::Firefox)
.build()
),
(
url4.to_owned(),
SourceBookmarkBuilder::new(url4)
.add_source(&SourceType::Firefox)
.build()
)
])
);
}

#[test]
Expand All @@ -393,12 +420,41 @@ mod tests {
let res = bookmark_reader.parse(&raw_bookmarks, &source, &mut source_bookmarks);
assert!(res.is_ok(), "{}", res.unwrap_err());

assert_eq!(source_bookmarks.inner(), HashMap::from_iter([
("https://www.mozilla.org/en-US/firefox/central/".to_owned(), HashSet::from_iter([SourceType::Firefox])),
("https://www.quantamagazine.org/how-mathematical-curves-power-cryptography-20220919/".to_owned(), HashSet::from_iter([SourceType::Firefox])),
("https://en.wikipedia.org/wiki/Design_Patterns".to_owned(), HashSet::from_iter([SourceType::Firefox])),
("https://doc.rust-lang.org/book/title-page.html".to_owned(), HashSet::from_iter([SourceType::Firefox])),
]));
let url1 = "https://www.mozilla.org/en-US/firefox/central/";
let url2 =
"https://www.quantamagazine.org/how-mathematical-curves-power-cryptography-20220919/";
let url3 = "https://en.wikipedia.org/wiki/Design_Patterns";
let url4 = "https://doc.rust-lang.org/book/title-page.html";

assert_eq!(
source_bookmarks.inner(),
HashMap::from_iter([
(
url1.to_owned(),
SourceBookmarkBuilder::new(url1)
.add_source(&SourceType::Firefox)
.build()
),
(
url2.to_owned(),
SourceBookmarkBuilder::new(url2)
.add_source(&SourceType::Firefox)
.build()
),
(
url3.to_owned(),
SourceBookmarkBuilder::new(url3)
.add_source(&SourceType::Firefox)
.build()
),
(
url4.to_owned(),
SourceBookmarkBuilder::new(url4)
.add_source(&SourceType::Firefox)
.build()
)
])
);
}

#[test]
Expand All @@ -419,16 +475,23 @@ mod tests {
let res = bookmark_reader.parse(&raw_bookmarks, &source, &mut source_bookmarks);
assert!(res.is_ok(), "{}", res.unwrap_err());

let url1 = "https://en.wikipedia.org/wiki/Design_Patterns";
let url2 = "https://doc.rust-lang.org/book/title-page.html";

assert_eq!(
source_bookmarks.inner(),
HashMap::from_iter([
(
"https://en.wikipedia.org/wiki/Design_Patterns".to_owned(),
HashSet::from_iter([SourceType::Firefox])
url1.to_owned(),
SourceBookmarkBuilder::new(url1)
.add_source(&SourceType::Firefox)
.build()
),
(
"https://doc.rust-lang.org/book/title-page.html".to_owned(),
HashSet::from_iter([SourceType::Firefox])
url2.to_owned(),
SourceBookmarkBuilder::new(url2)
.add_source(&SourceType::Firefox)
.build()
),
])
);
Expand All @@ -453,16 +516,23 @@ mod tests {
let res = bookmark_reader.parse(&raw_bookmarks, &source, &mut source_bookmarks);
assert!(res.is_ok(), "{}", res.unwrap_err());

let url1 = "https://en.wikipedia.org/wiki/Design_Patterns";
let url2 = "https://doc.rust-lang.org/book/title-page.html";

assert_eq!(
source_bookmarks.inner(),
HashMap::from_iter([
(
"https://en.wikipedia.org/wiki/Design_Patterns".to_owned(),
HashSet::from_iter([SourceType::Firefox])
url1.to_owned(),
SourceBookmarkBuilder::new(url1)
.add_source(&SourceType::Firefox)
.build()
),
(
"https://doc.rust-lang.org/book/title-page.html".to_owned(),
HashSet::from_iter([SourceType::Firefox])
url2.to_owned(),
SourceBookmarkBuilder::new(url2)
.add_source(&SourceType::Firefox)
.build()
),
])
);
Expand Down
Loading

0 comments on commit fcdd6be

Please sign in to comment.