Skip to content

Commit

Permalink
functionality add new bookmark
Browse files Browse the repository at this point in the history
  • Loading branch information
0xhenrique committed Apr 27, 2024
1 parent 8ae74b5 commit e8acf8f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
27 changes: 1 addition & 26 deletions assets/lum-marker.json
Original file line number Diff line number Diff line change
@@ -1,26 +1 @@
{
"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"
}
]
}
{"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"},{"title":"Re:Zero ED / Ending 2 Full『Emilia (Rie Takahashi) - Stay Alive』【ENG Sub】","link":"https://www.youtube.com/watch?v=z-NuxWkYtlI"},{"title":"arrays and vectors in rust (btw)","link":"https://www.cs.brandeis.edu/~cs146a/rust/doc-02-21-2015/book/arrays-vectors-and-slices.html"},{"title":"lambda expressions","link":"https://www.gnu.org/software/emacs/manual/html_node/eintr/lambda.html"}]}
64 changes: 61 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Serialize, Deserialize};
use std::io;
use std::io::prelude::*;
use std::fs::File;
use std::fs::{File, OpenOptions};
use std::num::ParseIntError;
use std::time::{SystemTime, UNIX_EPOCH};
use std::env;
Expand Down Expand Up @@ -80,7 +80,7 @@ fn handle_options_from_user_input() {

/*
* @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/
* controll on this section
*/
fn print_options() {
println!("What is the operation?");
Expand All @@ -93,7 +93,64 @@ fn print_options() {
}

fn add_new_bookmark() {
println!("Adding a new bookmark now...");
let mut file = match File::open("assets/lum-marker.json") {
Ok(file) => file,
Err(error) => {
println!("Error due to: {}", error);
return;
}
};

let mut contents = String::new();
if let Err(error) = file.read_to_string(&mut contents) {
println!("Failed due to: {}", error);
return;
};

let mut 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!("Enter the title for the new bookmark:");
let mut title = String::new();
io::stdin().read_line(&mut title).expect("Failed to read input.");
let title = title.trim().to_string();

println!("Enter the link for the new bookmark:");
let mut link = String::new();
io::stdin().read_line(&mut link).expect("Failed to read input.");
let link = link.trim().to_string();

let new_bookmark = Bookmark { title, link };

bookmark_data.bookmarks.push(new_bookmark);

let new_contents = match serde_json::to_string(&bookmark_data) {
Ok(data) => data,
Err(error) => {
println!("Failed to serialize bookmark data due to: {}", error);
return;
}
};

let mut file = match OpenOptions::new().write(true).truncate(true).open("assets/lum-marker.json") {
Ok(file) => file,
Err(error) => {
println!("Failed to open bookmark file for writing due to: {}", error);
return;
}
};

if let Err(error) = file.write_all(new_contents.as_bytes()) {
println!("Failed to write updated bookmark data to file due to: {}", error);
return;
}

println!("New bookmark added successfully.");
}

fn view_bookmarks() {
Expand Down Expand Up @@ -126,6 +183,7 @@ fn view_bookmarks() {
return;
}
}
handle_options_from_user_input();
}

fn delete_bookmark() {
Expand Down

0 comments on commit e8acf8f

Please sign in to comment.