Skip to content

Commit 9eca3bf

Browse files
committed
💡 Add documentation
1 parent a229ef5 commit 9eca3bf

File tree

4 files changed

+70
-39
lines changed

4 files changed

+70
-39
lines changed

src/commands.rs

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,23 @@
11
use super::config::Config;
2-
use super::data::get_data_file_path;
32
use super::data::{Bookmark, Data};
4-
use super::parser::*;
5-
use chrono::prelude::Local;
6-
use regex::Regex;
7-
use std::fs;
8-
use substring::Substring;
9-
10-
fn generate_name(link: &String, config: Config) -> String {
11-
let mut name = config.name.unwrap_or("".to_owned());
123

13-
// If name is not provided, use the domain name
14-
// If provided, replace ' ' with '_'
15-
if name == "" {
16-
let m = get_name(&link);
17-
name = link.substring(m.start(), m.end()).to_owned();
18-
} else {
19-
name = name.replace(' ', "_");
20-
}
21-
22-
return name;
23-
}
24-
25-
fn generate_bookmark(id:u32,link: String, name: String) -> Bookmark {
26-
Bookmark {
27-
is_file: !is_url(&link),
28-
link: link,
29-
name: name,
30-
date: Local::now().to_string(),
31-
id: id,
32-
}
33-
}
4+
use regex::Regex;
345

6+
/// Insert commands
7+
/// Adds the bookmark to the data list and increments the last id
358
pub fn insert(input: String, mut data: Data, config: Config) {
36-
let name = generate_name(&input, config);
9+
let name = Bookmark::generate_name(&input, config.name);
3710

38-
let bookmark = generate_bookmark(data.last_id+1, input, name);
11+
let bookmark = Bookmark::generate_bookmark(data.last_id + 1, input, name);
3912
data.bookmarks.push(bookmark);
4013
data.last_id += 1;
4114

42-
fs::write(
43-
get_data_file_path(),
44-
serde_json::to_string_pretty(&data).unwrap(),
45-
)
46-
.unwrap();
15+
data.write_to_file();
4716
}
4817

18+
/// List command
19+
/// List all the bookmarks if no name flag was provided
20+
/// List bookmarks that match the regex provided in name flag
4921
pub fn list(data: Data, config: Config) {
5022
let name = config.name.unwrap_or("".to_owned());
5123

@@ -67,6 +39,7 @@ pub fn list(data: Data, config: Config) {
6739
print_bookmark(&search_results);
6840
}
6941

42+
/// Print all bookmarks in the vector
7043
fn print_bookmark(input: &Vec<Bookmark>) {
7144
for i in input {
7245
println!("{}", i);

src/data.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use home::home_dir;
22
use serde::{Deserialize, Serialize};
33
use std::fmt;
44
use std::fs;
5+
use substring::Substring;
6+
use chrono::prelude::Local;
7+
8+
use super::parser::{is_url,get_domain_name};
59

610
#[derive(Serialize, Deserialize, Debug)]
711
pub struct Bookmark {
@@ -12,6 +16,37 @@ pub struct Bookmark {
1216
pub id: u32,
1317
}
1418

19+
impl Bookmark {
20+
/// Generate a suitable name for Bookmark
21+
/// If name is empty or not provided, link is parsed to get the domain name.
22+
/// If name contains spaces, it is converted to underscores
23+
pub fn generate_name(link: &String, name: Option<String>) -> String {
24+
let mut name = name.unwrap_or("".to_owned());
25+
26+
// If name is not provided, use the domain name
27+
// If provided, replace ' ' with '_'
28+
if name == "" {
29+
let m = get_domain_name(&link);
30+
name = link.substring(m.start(), m.end()).to_owned();
31+
} else {
32+
name = name.replace(' ', "_");
33+
}
34+
35+
return name;
36+
}
37+
38+
/// Return bookmark with values
39+
pub fn generate_bookmark(id: u32, link: String, name: String) -> Bookmark {
40+
Bookmark {
41+
is_file: !is_url(&link),
42+
link: link,
43+
name: name,
44+
date: Local::now().to_string(),
45+
id: id,
46+
}
47+
}
48+
}
49+
1550
impl fmt::Display for Bookmark {
1651
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1752
write!(f, "{} {} {}", self.id, self.name, self.link)
@@ -24,6 +59,19 @@ pub struct Data {
2459
pub last_id: u32,
2560
}
2661

62+
impl Data {
63+
/// Prettify the json and write to file
64+
pub fn write_to_file(&self) {
65+
fs::write(
66+
get_data_file_path(),
67+
serde_json::to_string_pretty(&self).unwrap(),
68+
)
69+
.unwrap();
70+
}
71+
}
72+
73+
/// Create data directory and data file.data
74+
/// Write a barebones JSON to the data file
2775
pub fn create_data_file() {
2876
let data_dir = home_dir().unwrap().join(".local/share/rbmenu/");
2977
let data_file = data_dir.join("bookmark.json");
@@ -36,11 +84,15 @@ pub fn create_data_file() {
3684
fs::File::create(&data_file).unwrap();
3785
}
3886

39-
let data = Data { bookmarks: vec![] ,last_id: 0};
87+
let data = Data {
88+
bookmarks: vec![],
89+
last_id: 0,
90+
};
4091

4192
fs::write(data_file, serde_json::to_string_pretty(&data).unwrap()).unwrap();
4293
}
4394

95+
/// Read and parse data file into Data struct
4496
pub fn read_data_file() -> Data {
4597
let data_file = get_data_file_path();
4698

@@ -52,6 +104,7 @@ pub fn read_data_file() -> Data {
52104
serde_json::from_str(&content).unwrap()
53105
}
54106

107+
/// Return data file path
55108
pub fn get_data_file_path() -> std::path::PathBuf {
56109
home_dir()
57110
.unwrap()

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use data::read_data_file;
1010
use std::io::stdin;
1111
use structopt::StructOpt;
1212

13+
/// Call command functions based on given options
1314
pub fn run() {
1415
let opts = Config::from_args();
1516
let data = read_data_file();

src/parser.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use regex::Regex;
22

3+
/// Use regex to check if the given str is a URL
4+
/// input : link to check against
35
pub fn is_url(input: &str) -> bool {
46
let re = Regex::new( r"^(http://www\.|https://www.|http://|https://)?[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$").unwrap();
57
re.is_match(&input)
68
}
79

8-
pub fn get_name(input: &str) -> regex::Match {
10+
/// Use regex to get the domain name and subdomains from the given URL
11+
/// input : link to get the domain and subdomains
12+
pub fn get_domain_name(input: &str) -> regex::Match {
913
let re = Regex::new(r".*://(?:www.)?([^/]+)").unwrap();
1014
re.captures(&input).unwrap().get(1).unwrap()
1115
}

0 commit comments

Comments
 (0)