From fdf3687d54126a914f48fdbdf6713a75fd1ec918 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Fri, 31 Mar 2023 13:05:18 +0900 Subject: [PATCH] Clean up private helper methods --- src/library/beets.rs | 50 +++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/library/beets.rs b/src/library/beets.rs index c16b25a..72d0fcc 100644 --- a/src/library/beets.rs +++ b/src/library/beets.rs @@ -78,15 +78,39 @@ pub struct Beets { executor: Box, } +impl Beets { + pub fn new(executor: Box) -> Beets { + Beets { executor } + } +} + +trait LibraryPrivate { + const CMD_LIST: &'static str; + const SEPARATOR: &'static str; + const LIST_FORMAT: &'static str; + + fn list_cmd_and_args(query: &Query) -> Vec; + fn list_to_albums(list_output: Vec) -> Result, Error>; +} + +impl Library for Beets { + fn list(&mut self, query: &Query) -> Result, Error> { + let cmd = Self::list_cmd_and_args(query); + let output = self.executor.exec(cmd)?; + Self::list_to_albums(output) + } +} + macro_rules! separator { () => { "-*^-" }; } -impl Beets { - const SEPARATOR: &str = separator!(); - const LIST_FORMAT: &str = concat!( +impl LibraryPrivate for Beets { + const CMD_LIST: &'static str = "ls"; + const SEPARATOR: &'static str = separator!(); + const LIST_FORMAT: &'static str = concat!( "--format=", "$albumartist", separator!(), @@ -101,8 +125,11 @@ impl Beets { "$artist" ); - pub fn new(executor: Box) -> Beets { - Beets { executor } + fn list_cmd_and_args(query: &Query) -> Vec { + let mut cmd: Vec = vec![String::from(Self::CMD_LIST)]; + cmd.push(Self::LIST_FORMAT.to_string()); + cmd.append(&mut query.to_args()); + cmd } fn list_to_albums(list_output: Vec) -> Result, Error> { @@ -147,22 +174,11 @@ impl Beets { }); } } + Ok(albums) } } -impl Library for Beets { - fn list(&mut self, query: &Query) -> Result, Error> { - let mut arguments: Vec = vec![String::from("ls")]; - arguments.push(Self::LIST_FORMAT.to_string()); - arguments.append(&mut query.to_args()); - - let output = self.executor.exec(arguments)?; - - Self::list_to_albums(output) - } -} - pub struct SystemExecutor { bin: String, }