~exprez135/castor

ref: 0.8.4 castor/src/bookmarks.rs -rw-r--r-- 879 bytes
193de0c2Julien Blanchard [gopher] Fix parsing of h type 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
extern crate dirs;

use std::fs;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::{Read, Write};

pub fn add(url: &str) {
    let mut file = bookmarks_file();
    let entry = format!("=> {}\n", url);
    file.write_all(entry.as_bytes())
        .expect("Unable to write file");
}

pub fn content() -> String {
    let mut file = bookmarks_file();
    let mut content = String::new();
    file.read_to_string(&mut content)
        .expect("Unable to read file");

    content
}

fn bookmarks_file() -> File {
    let mut bookmarks = dirs::data_local_dir().unwrap();
    bookmarks.push("castor");
    fs::create_dir_all(&bookmarks).unwrap();
    bookmarks.push("bookmarks");
    let file_path = bookmarks.into_os_string();

    OpenOptions::new()
        .create(true)
        .append(true)
        .read(true)
        .open(file_path)
        .expect("file not found")
}