diff --git a/Cargo.lock b/Cargo.lock index 69f4240..98fa292 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,7 +47,7 @@ dependencies = [ [[package]] name = "monokakido" -version = "0.3.1" +version = "0.3.2" dependencies = [ "miniserde", "miniz_oxide", diff --git a/Cargo.toml b/Cargo.toml index 4d07631..f7b0d95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monokakido" -version = "0.3.1" +version = "0.3.2" edition = "2021" description = "A Rust library for parsing and interpreting the Monokakido dictionary format." license = "MIT" diff --git a/src/bin/monokakido-cli.rs b/src/bin/monokakido-cli.rs index 7920c05..cf1a2b7 100644 --- a/src/bin/monokakido-cli.rs +++ b/src/bin/monokakido-cli.rs @@ -1,3 +1,5 @@ +use std::io::Write; + use monokakido::{Error, MonokakidoDict}; fn print_help() { @@ -46,6 +48,16 @@ fn list_audio(dict_name: &str, keyword: &str) -> Result<(), Error> { Ok(()) } +fn get_audio(dict_name: &str, id: &str) -> Result<(), Error> { + let id = id.strip_suffix(".aac").unwrap_or(id); + let mut dict = MonokakidoDict::open(dict_name)?; + let aac = dict.audio.as_mut().ok_or(Error::MissingAudio)?.get(id)?; + let mut stdout = std::io::stdout().lock(); + // TODO: for ergonomics/failsafe, check if stdout is a TTY + stdout.write_all(aac)?; + Ok(()) +} + fn list_dicts() -> Result<(), Error> { for dict in MonokakidoDict::list()? { println!("{}", dict?); @@ -63,6 +75,13 @@ fn main() { Err(Error::InvalidArg) } } + Some("get_audio") => { + if let (Some(dict_name), Some(id)) = (args.next(), args.next()) { + get_audio(&dict_name, &id) + } else { + Err(Error::InvalidArg) + } + } Some("list_items") => { if let (Some(dict_name), Some(keyword)) = (args.next(), args.next()) { list_items(&dict_name, &keyword) @@ -82,7 +101,7 @@ fn main() { print_help(); Ok(()) } - _ => Err(Error::InvalidArg), + _ => Err(Error::InvalidSubcommand), }; if let Err(e) = res { diff --git a/src/error.rs b/src/error.rs index 61809cf..992c87b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -25,6 +25,8 @@ pub enum Error { FmtError, IndexDoesntExist, XmlError, + MissingAudio, + InvalidSubcommand, } impl From for Error {