Skip to content

Commit

Permalink
listing bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
0xhenrique committed Apr 27, 2024
1 parent 1712071 commit 8ae74b5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BOOKMARK_PATH="assets/"
OWNER=
27 changes: 26 additions & 1 deletion assets/lum-marker.json
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
title: "linux ubiquitous marker!"
{
"owner": "admin",
"created_at": 1714182655,
"bookmarks": [
{
"title": "Hyouka - Saint Pepsi",
"link": "https://www.youtube.com/watch?v=SpQCuPTWrrI"
},
{
"title": "【original anime MV】SHINKIRO【hololive/宝鐘マリン・Gawr Gura】",
"link": "https://www.youtube.com/watch?v=9ehwhQJ50gs"
},
{
"title": "【MV】Lucky☆Orb feat. Hatsune Miku by emon(Tes.) / ラッキー☆オーブ feat. 初音ミク by emon(Tes.) 【MIKU EXPO 5th】",
"link": "https://www.youtube.com/watch?v=AufydOsiD6M"
},
{
"title": "[Ochame Kinou] hololive Fukkireta Chorus [24 VTubers]",
"link": "https://www.youtube.com/watch?v=9kQ2GtvDV3s"
},
{
"title": "【MV】White Love/SPEED (Cover)【FUWAMOCO】",
"link": "https://www.youtube.com/watch?v=D8YflSQi1Vk"
}
]
}
56 changes: 47 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use serde::Serialize;
use serde::{Serialize, Deserialize};
use std::io;
use std::io::prelude::*;
use std::fs::File;
use std::num::ParseIntError;
use std::time::{SystemTime, UNIX_EPOCH};
use std::env;

#[derive(Serialize)]
#[derive(Serialize, Deserialize)]
struct Bookmark {
title: String,
link: String,
}

#[derive(Serialize)]
#[derive(Serialize, Deserialize)]
struct BookmarkData {
owner: String,
created_at: u64,
bookmarks: Vec<Bookmark>,
}

fn create_json_file() -> std::io::Result<()> {
let computer_name = env::var("COMPUTERNAME").unwrap_or_else(|_| "admin".to_string());
fn generate_lum() -> std::io::Result<()> {
let computer_name = env::var("OWNER").unwrap_or_else(|_| "admin".to_string());

let created_at = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -39,7 +39,7 @@ fn create_json_file() -> std::io::Result<()> {
};

let json_data = serde_json::to_string_pretty(&bookmark_data)?;
let mut file = File::create("bookmark.json")?;
let mut file = File::create("assets/lum-marker.json")?;
file.write_all(json_data.as_bytes())?;

Ok(())
Expand Down Expand Up @@ -67,27 +67,65 @@ fn handle_options_from_user_input() {
3 => delete_bookmark(),
4 => purge_bookmarks(),
5 => quit(),
// We don't actually need this 6th here - just a convenience
6 => {
match generate_lum() {
Ok(_) => (),
Err(e) => println!("Error due to: {}", e),
}
},
_ => handle_options_from_user_input(),
}
}

// The whole "input" logic will soon be moved to a specific directory/file so I can have a better
// controll on this section -> src/input.rs maybe
/*
* @TODO: The whole "input" logic will soon be moved to a specific directory/file so I can have a better
* controll on this section -> src/
*/
fn print_options() {
println!("What is the operation?");
println!("1 - Add new bookmark;");
println!("2 - View bookmarks;");
println!("3 - Delete a bookmark;");
println!("4 - Purge bookmarks;");
println!("5 - Quit;");
println!("6 - Generate Lum;");
}

fn add_new_bookmark() {
println!("Adding a new bookmark now...");
}

fn view_bookmarks() {
println!("Viewing bookmarks now...");
let mut file = match File::open("assets/lum-marker.json") {
Ok(file) => file,
Err(error) => {
println!("Failed to open bookmark file due to: {}", error);
return;
}
};

let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => {
let bookmark_data: BookmarkData = match serde_json::from_str(&contents) {
Ok(data) => data,
Err(error) => {
println!("Failed to parse bookmark data due to: {}", error);
return;
}
};

println!("Bookmarks for {}: ", bookmark_data.owner);
for (index, bookmark) in bookmark_data.bookmarks.iter().enumerate() {
println!("{}. {}\n - {}\n", index + 1, bookmark.title, bookmark.link);
}
}
Err(error) => {
println!("Failed to read bookmark file due to: {}", error);
return;
}
}
}

fn delete_bookmark() {
Expand Down

0 comments on commit 8ae74b5

Please sign in to comment.