From ef3db5494520e753b2f0f857dfed4f6c3ba76b18 Mon Sep 17 00:00:00 2001 From: RedStone576 Date: Wed, 25 Mar 2026 00:37:16 +0700 Subject: [PATCH] balls --- .gitignore | 1 + Cargo.lock | 7 +++ Cargo.toml | 6 ++ src/main.rs | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..71e512d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "notes-rust" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8edec7c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "notes-rust" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..cc2ae0a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,177 @@ +use std::io::{self, Write}; + +struct Notes { + id: u32, + title: String, + content: Vec, +} + +impl Notes { + fn new(id: u32, title: String, content: Vec) -> Notes { + Notes { + id, + title, + content + } + } +} + +struct NotesCollection { + notes: Vec, + next_id: u32, +} + +impl NotesCollection { + fn new() -> NotesCollection { + NotesCollection { + notes: Vec::new(), + next_id: 1, + } + } + + fn add(&mut self, title: String, content: Vec) -> u32 { + let note = Notes::new(self.next_id, title, content); + let id = note.id; + + self.notes.push(note); + self.next_id += 1; + + id + } + + fn remove(&mut self, id: u32) -> bool { + if let Some(pos) = self.notes.iter().position(|n| n.id == id) { + self.notes.remove(pos); + + true + } else { + false + } + } + + fn list(&self) -> Vec<(u32, &String)> { + self.notes.iter().map(|n| (n.id, &n.title)).collect() + } + + fn count(&self) -> usize { + self.notes.len() + } +} + +pub fn get_input(prompt: &str) -> String { + print!("{prompt}"); + io::stdout().flush().expect("Failed to flush"); + + let mut input = String::new(); + + match io::stdin().read_line(&mut input) { + Ok(_) => {} + Err(e) => eprintln!("Error reading input: {e}"), + } + + input.trim().to_string() +} + +fn mode_add(collection: &mut NotesCollection) { + let title: String; + + loop { + let temp_title = get_input("Set title: "); + + if temp_title.is_empty() { + println!("Pls no empty title"); + } else { + title = temp_title.to_string(); + break; + } + } + + let mut content: Vec = Vec::new(); + + loop { + let item = get_input("Enter item (- to exit): "); + + if item.is_empty() { + println!("Pls no empty item"); + + continue; + } + + if item == "-" { + break; + } + + content.push(item); + } + + let id = collection.add(title, content); + + println!("Note #{} saved!", id); +} + +fn mode_delete(collection: &mut NotesCollection) { + if collection.count() == 0 { + println!("Nothing to delete bruh"); + + return; + } + + for (id, title) in collection.list() { + println!("[{}] {}", id, title); + } + + let input = get_input("Enter note ID to delete (- to cancel): "); + + if input == "-" { + return; + } + + match input.trim().parse::() { + Ok(id) => { + if collection.remove(id) { + println!("Note #{} deleted!", id); + } else { + println!("No note with the ID {}!!!", id); + } + } + Err(_) => println!("Invalid ID!"), + } +} + +fn mode_view(collection: &NotesCollection) { + if collection.count() == 0 { + println!("Nothing here bruh"); + + return; + } + + for note in &collection.notes { + println!("{}", note.title); + + for item in ¬e.content { + println!(" - {}", item); + } + + println!(); + } +} + +fn main() { + let mut notes: NotesCollection = NotesCollection::new(); + + loop { + let mode = get_input("Select mode (v | a | d): "); // view, add, delete + // println!("{mode}"); + + match mode.as_str() { + "a" => mode_add(&mut notes), + "d" => mode_delete(&mut notes), + "v" => mode_view(¬es), + "q" => break, + _ => { + println!("not a valid input"); + continue + } + } + } +}