web_extensions/bookmarks.rs
1//! Wrapper for the [`chrome.bookmarks` API](https://developer.chrome.com/docs/extensions/reference/bookmarks/).
2
3use crate::{util::*, Error};
4use serde::{Deserialize, Serialize};
5use web_extensions_sys as sys;
6
7/// <https://developer.chrome.com/docs/extensions/reference/bookmarks/#method-search>
8pub async fn search(query: &Query<'_>) -> Result<Vec<BookmarkTreeNode>, Error> {
9 let js_query = js_from_serde(query)?;
10 let js_value = sys::chrome().bookmarks().search(&js_query).await;
11 serde_from_js(js_value)
12}
13
14/// <https://developer.chrome.com/docs/extensions/reference/bookmarks/#type-search-query>
15#[derive(Debug, Clone, Serialize)]
16#[serde(rename_all = "camelCase")]
17pub struct Query<'a> {
18 /// A string of words and quoted phrases that are matched against bookmark URLs and titles.
19 pub query: Option<&'a str>,
20
21 /// The title of the bookmark; matches verbatim.
22 pub title: Option<&'a str>,
23
24 /// The URL of the bookmark; matches verbatim. Note that folders have no URL.
25 pub url: Option<&'a str>,
26}
27
28impl<'a> From<&'a str> for Query<'a> {
29 fn from(q: &'a str) -> Self {
30 Self {
31 query: Some(q),
32 title: None,
33 url: None,
34 }
35 }
36}
37
38/// <https://developer.chrome.com/docs/extensions/reference/bookmarks/#type-BookmarkTreeNode>
39///
40/// A node (either a bookmark or a folder) in the bookmark tree.
41/// Child nodes are ordered within their parent folder.
42
43#[derive(Debug, Clone, Deserialize)]
44#[serde(rename_all = "camelCase")]
45pub struct BookmarkTreeNode {
46 /// Unique identifier.
47 pub id: String,
48
49 /// An ordered list of children of this node.
50 pub children: Option<Vec<Self>>,
51
52 /// The 0-based position of this node within its parent folder.
53 pub index: Option<u32>,
54
55 /// A string which specifies the ID of the parent folder. This property is
56 /// not present in the root node.
57 pub parent_id: Option<String>,
58
59 /// Date and time of the creation of the bookmark.
60 ///
61 /// Unix time as milliseconds since the epoch.
62 pub date_added: Option<i64>,
63
64 /// When the contents of this folder last changed, in milliseconds since the epoch.
65 pub date_group_modified: Option<i64>,
66
67 /// The text displayed for the node in menus and lists of bookmarks.
68 pub title: String,
69
70 /// The URL for the bookmark. Empty if this node is a Folder.
71 pub url: Option<String>,
72}