From 7d6d6d295bb5d5f1e25e30fbc06de2cd4a16e22c Mon Sep 17 00:00:00 2001 From: Mark <> Date: Tue, 28 May 2024 10:15:13 +0200 Subject: [PATCH] update lib --- musicdb-lib/src/server/simple.rs | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 musicdb-lib/src/server/simple.rs diff --git a/musicdb-lib/src/server/simple.rs b/musicdb-lib/src/server/simple.rs new file mode 100644 index 0000000..436c3bf --- /dev/null +++ b/musicdb-lib/src/server/simple.rs @@ -0,0 +1,43 @@ +use std::{ + io::{BufRead, BufReader, Read, Write}, + sync::{mpsc, Arc, Mutex}, +}; + +use crate::data::database::Database; + +use super::Command; + +pub fn handle_main( + db: Arc>, + con: &mut BufReader, + command_sender: &mpsc::Sender, +) -> Result<(), std::io::Error> { + loop { + let mut command = String::new(); + con.read_line(&mut command)?; + let command = command.trim(); + let (command, args) = command.split_once(' ').unwrap_or((command, "")); + match command { + "goodbye" => return Ok(()), + "list-artists" => { + for (id, artist) in db.lock().unwrap().artists().iter() { + writeln!(con.get_mut(), "#{id}:{}", artist.name)?; + } + writeln!(con.get_mut(), "---")?; + } + "list-albums" => { + for (id, album) in db.lock().unwrap().albums().iter() { + writeln!(con.get_mut(), "#{id}:{}", album.name)?; + } + writeln!(con.get_mut(), "---")?; + } + "list-songs" => { + for (id, song) in db.lock().unwrap().songs().iter() { + writeln!(con.get_mut(), "#{id}:{}", song.title)?; + } + writeln!(con.get_mut(), "---")?; + } + _ => writeln!(con.get_mut(), "err: no such command")?, + } + } +}