diff --git a/vhost-device-sound/src/args.rs b/vhost-device-sound/src/args.rs new file mode 100644 index 0000000..7cd51d5 --- /dev/null +++ b/vhost-device-sound/src/args.rs @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause +//! An arguments type for the binary interface of this library. + +use std::path::PathBuf; + +use clap::{Parser, ValueEnum}; + +#[derive(Parser, Debug)] +#[clap(version, about, long_about = None)] +pub struct SoundArgs { + /// vhost-user Unix domain socket path. + #[clap(long)] + pub socket: PathBuf, + /// audio backend to be used + #[clap(long)] + #[clap(value_enum)] + pub backend: BackendType, +} + +#[derive(ValueEnum, Clone, Copy, Default, Debug, Eq, PartialEq)] +pub enum BackendType { + #[default] + Null, + #[cfg(all(feature = "pw-backend", target_env = "gnu"))] + Pipewire, + #[cfg(all(feature = "alsa-backend", target_env = "gnu"))] + Alsa, +} diff --git a/vhost-device-sound/src/lib.rs b/vhost-device-sound/src/lib.rs index 9235b36..ea0ec75 100644 --- a/vhost-device-sound/src/lib.rs +++ b/vhost-device-sound/src/lib.rs @@ -37,6 +37,7 @@ pub fn init_logger() { let _ = env_logger::builder().is_test(true).try_init(); } +pub mod args; pub mod audio_backends; pub mod device; pub mod stream; @@ -50,7 +51,7 @@ use std::{ sync::Arc, }; -use clap::ValueEnum; +pub use args::BackendType; pub use stream::Stream; use thiserror::Error as ThisError; use vhost_user_backend::{VhostUserDaemon, VringRwLock, VringT}; @@ -191,16 +192,6 @@ impl From for Error { } } -#[derive(ValueEnum, Clone, Copy, Default, Debug, Eq, PartialEq)] -pub enum BackendType { - #[default] - Null, - #[cfg(all(feature = "pw-backend", target_env = "gnu"))] - Pipewire, - #[cfg(all(feature = "alsa-backend", target_env = "gnu"))] - Alsa, -} - #[derive(Debug, PartialEq, Eq)] pub struct InvalidControlMessage(u32); @@ -263,6 +254,14 @@ pub struct SoundConfig { audio_backend: BackendType, } +impl From for SoundConfig { + fn from(cmd_args: args::SoundArgs) -> Self { + let args::SoundArgs { socket, backend } = cmd_args; + + Self::new(socket, false, backend) + } +} + impl SoundConfig { /// Create a new instance of the SoundConfig struct, containing the /// parameters to be fed into the sound-backend server. diff --git a/vhost-device-sound/src/main.rs b/vhost-device-sound/src/main.rs index 28d48bf..283f802 100644 --- a/vhost-device-sound/src/main.rs +++ b/vhost-device-sound/src/main.rs @@ -1,35 +1,14 @@ // Manos Pitsidianakis // Stefano Garzarella // SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause -use std::{convert::TryFrom, path::PathBuf}; use clap::Parser; -use vhost_device_sound::{start_backend_server, BackendType, Error, Result, SoundConfig}; - -#[derive(Parser, Debug)] -#[clap(version, about, long_about = None)] -struct SoundArgs { - /// vhost-user Unix domain socket path. - #[clap(long)] - socket: PathBuf, - /// audio backend to be used - #[clap(long)] - #[clap(value_enum)] - backend: BackendType, -} - -impl TryFrom for SoundConfig { - type Error = Error; - - fn try_from(cmd_args: SoundArgs) -> Result { - Ok(SoundConfig::new(cmd_args.socket, false, cmd_args.backend)) - } -} +use vhost_device_sound::{args::SoundArgs, start_backend_server, SoundConfig}; fn main() { env_logger::init(); - let config = SoundConfig::try_from(SoundArgs::parse()).unwrap(); + let config = SoundConfig::from(SoundArgs::parse()); loop { start_backend_server(config.clone()); @@ -38,19 +17,14 @@ fn main() { #[cfg(test)] mod tests { + use std::path::{Path, PathBuf}; + + use clap::Parser; use rstest::*; + use vhost_device_sound::BackendType; use super::*; - impl SoundArgs { - fn from_args(socket: PathBuf) -> Self { - SoundArgs { - socket, - backend: BackendType::default(), - } - } - } - fn init_logger() { std::env::set_var("RUST_LOG", "trace"); let _ = env_logger::builder().is_test(true).try_init(); @@ -59,15 +33,15 @@ mod tests { #[test] fn test_sound_config_setup() { init_logger(); - let args = SoundArgs::from_args(PathBuf::from("/tmp/vhost-sound.socket")); + let args = SoundArgs { + socket: PathBuf::from("/tmp/vhost-sound.socket"), + backend: BackendType::default(), + }; + let config = SoundConfig::from(args); - let config = SoundConfig::try_from(args); - assert!(config.is_ok()); - - let config = config.unwrap(); assert_eq!( config.get_socket_path(), - PathBuf::from("/tmp/vhost-sound.socket") + Path::new("/tmp/vhost-sound.socket") ); } @@ -90,10 +64,7 @@ mod tests { backend_name, ]); - let config = SoundConfig::try_from(args); - assert!(config.is_ok()); - - let config = config.unwrap(); + let config = SoundConfig::from(args); assert_eq!(config.get_audio_backend(), backend); } }