mirror of
https://github.com/rust-vmm/vhost-device.git
synced 2025-12-30 09:46:55 +00:00
sound: move CLI arg types to lib submodule
Currently, the main CLI struct type is defined in src/main.rs thus inaccessible when using the crate as a library. Move it to its own module under src/args.rs This will be useful for followup commits that will use src/args.rs directly to generate manual pages using clap_mangen Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
This commit is contained in:
parent
26e163736c
commit
de8ed1baea
28
vhost-device-sound/src/args.rs
Normal file
28
vhost-device-sound/src/args.rs
Normal file
@ -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,
|
||||
}
|
||||
@ -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<stream::Error> 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<args::SoundArgs> 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.
|
||||
|
||||
@ -1,35 +1,14 @@
|
||||
// Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
|
||||
// Stefano Garzarella <sgarzare@redhat.com>
|
||||
// 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<SoundArgs> for SoundConfig {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(cmd_args: SoundArgs) -> Result<Self> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user