Add existing command parsers to the main request parser

This commit is contained in:
Oystein Kristoffer Tveit 2024-12-01 19:16:44 +01:00
parent 08104b3537
commit 3e512092bd
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
11 changed files with 172 additions and 25 deletions

View File

@ -9,9 +9,12 @@ mod playback_options;
mod querying_mpd_status; mod querying_mpd_status;
mod queue; mod queue;
pub use querying_mpd_status::clearerror::ClearError; pub use audio_output_devices::*;
pub use querying_mpd_status::idle::Idle; pub use client_to_client::*;
pub use querying_mpd_status::status::Status; pub use controlling_playback::*;
pub use playback_options::*;
pub use querying_mpd_status::*;
pub use queue::*;
pub trait Command { pub trait Command {
type Response; type Response;

View File

@ -3,3 +3,9 @@ pub mod enableoutput;
pub mod outputs; pub mod outputs;
pub mod outputset; pub mod outputset;
pub mod toggleoutput; pub mod toggleoutput;
pub use disableoutput::DisableOutput;
pub use enableoutput::EnableOutput;
pub use outputs::Outputs;
pub use outputset::OutputSet;
pub use toggleoutput::ToggleOutput;

View File

@ -3,3 +3,9 @@ pub mod readmessages;
pub mod sendmessage; pub mod sendmessage;
pub mod subscribe; pub mod subscribe;
pub mod unsubscribe; pub mod unsubscribe;
pub use channels::Channels;
pub use readmessages::ReadMessages;
pub use sendmessage::SendMessage;
pub use subscribe::Subscribe;
pub use unsubscribe::Unsubscribe;

View File

@ -7,3 +7,13 @@ pub mod seek;
pub mod seekcur; pub mod seekcur;
pub mod seekid; pub mod seekid;
pub mod stop; pub mod stop;
pub use next::Next;
pub use pause::Pause;
pub use play::Play;
pub use playid::PlayId;
pub use previous::Previous;
pub use seek::Seek;
pub use seekcur::SeekCur;
pub use seekid::SeekId;
pub use stop::Stop;

View File

@ -10,3 +10,16 @@ pub mod replay_gain_status;
pub mod setvol; pub mod setvol;
pub mod single; pub mod single;
pub mod volume; pub mod volume;
pub use consume::Consume;
pub use crossfade::Crossfade;
pub use getvol::GetVol;
pub use mixrampdb::MixRampDb;
pub use mixrampdelay::MixRampDelay;
pub use random::Random;
pub use repeat::Repeat;
pub use replay_gain_mode::ReplayGainMode;
pub use replay_gain_status::ReplayGainStatus;
pub use setvol::SetVol;
pub use single::Single;
pub use volume::Volume;

View File

@ -1,12 +1,12 @@
use crate::{ use crate::{
commands::{Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError}, commands::{Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError},
request::Volume, request::VolumeValue,
}; };
pub struct GetVol; pub struct GetVol;
impl Command for GetVol { impl Command for GetVol {
type Response = Volume; type Response = VolumeValue;
const COMMAND: &'static str = "getvol"; const COMMAND: &'static str = "getvol";
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> { fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {

View File

@ -5,7 +5,7 @@ use crate::{
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
ResponseParserError, ResponseParserError,
}, },
request::Volume, request::VolumeValue,
}; };
pub struct SetVol; pub struct SetVol;
@ -16,9 +16,8 @@ impl Command for SetVol {
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> { fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
let volume = match parts.next() { let volume = match parts.next() {
Some(s) => { Some(s) => VolumeValue::from_str(s)
Volume::from_str(s).map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))? .map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
}
None => return Err(RequestParserError::UnexpectedEOF), None => return Err(RequestParserError::UnexpectedEOF),
}; };

View File

@ -1,11 +1,11 @@
use std::str::FromStr; use std::str::FromStr;
use crate::commands::{ use crate::{commands::{
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes, Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
ResponseParserError, ResponseParserError,
}; }, request::VolumeValue};
struct Volume; pub struct Volume;
impl Command for Volume { impl Command for Volume {
type Response = (); type Response = ();
@ -13,7 +13,7 @@ impl Command for Volume {
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> { fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
let change = match parts.next() { let change = match parts.next() {
Some(s) => crate::request::Volume::from_str(s) Some(s) => VolumeValue::from_str(s)
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?, .map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
None => return Err(RequestParserError::UnexpectedEOF), None => return Err(RequestParserError::UnexpectedEOF),
}; };

View File

@ -3,3 +3,9 @@ pub mod currentsong;
pub mod idle; pub mod idle;
pub mod stats; pub mod stats;
pub mod status; pub mod status;
pub use clearerror::ClearError;
pub use currentsong::CurrentSong;
pub use idle::Idle;
pub use stats::Stats;
pub use status::Status;

View File

@ -20,3 +20,26 @@ pub mod rangeid;
pub mod shuffle; pub mod shuffle;
pub mod swap; pub mod swap;
pub mod swapid; pub mod swapid;
pub use add::Add;
pub use addid::AddId;
pub use addtagid::AddTagId;
pub use clear::Clear;
pub use cleartagid::ClearTagId;
pub use delete::Delete;
pub use deleteid::DeleteId;
pub use move_::Move;
pub use moveid::MoveId;
pub use playlist::Playlist;
pub use playlistfind::PlaylistFind;
pub use playlistid::PlaylistId;
pub use playlistinfo::PlaylistInfo;
pub use playlistsearch::PlaylistSearch;
pub use plchanges::PlChanges;
pub use plchangesposid::PlChangesPosId;
pub use prio::Prio;
pub use prioid::PrioId;
pub use rangeid::RangeId;
pub use shuffle::Shuffle;
pub use swap::Swap;
pub use swapid::SwapId;

View File

@ -26,12 +26,12 @@ pub enum Request {
MixRampDelay(Seconds), MixRampDelay(Seconds),
Random(bool), Random(bool),
Repeat(bool), Repeat(bool),
SetVol(Volume), SetVol(VolumeValue),
GetVol, GetVol,
Single(BoolOrOneshot), Single(BoolOrOneshot),
ReplayGainMode(ReplayGainModeMode), ReplayGainMode(ReplayGainModeMode),
ReplayGainStatus, ReplayGainStatus,
Volume(Volume), Volume(VolumeValue),
// -- Playback Control Commands -- // // -- Playback Control Commands -- //
Next, Next,
@ -253,8 +253,8 @@ impl FromStr for ReplayGainModeMode {
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Volume(u32); pub struct VolumeValue(u32);
impl Volume { impl VolumeValue {
pub fn new(volume: u32) -> Result<Self, ()> { pub fn new(volume: u32) -> Result<Self, ()> {
match volume { match volume {
0..=100 => Ok(Self(volume)), 0..=100 => Ok(Self(volume)),
@ -262,16 +262,16 @@ impl Volume {
} }
} }
} }
impl From<Volume> for u32 { impl From<VolumeValue> for u32 {
fn from(val: Volume) -> Self { fn from(val: VolumeValue) -> Self {
val.0 val.0
} }
} }
impl FromStr for Volume { impl FromStr for VolumeValue {
type Err = (); type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let volume = s.parse().map_err(|_| ())?; let volume = s.parse().map_err(|_| ())?;
Volume::new(volume) VolumeValue::new(volume)
} }
} }
@ -353,10 +353,91 @@ impl Request {
} }
"command_list_end" => Err(RequestParserError::UnexpectedCommandListEnd(0)), "command_list_end" => Err(RequestParserError::UnexpectedCommandListEnd(0)),
Idle::COMMAND => Idle::parse_request(parts).map(|(req, _)| (req, rest)), /* querying mpd status */
Status::COMMAND => Status::parse_request(parts).map(|(req, _)| (req, rest)), ClearError::COMMAND => ClearError::parse_request(parts),
ClearError::COMMAND => ClearError::parse_request(parts).map(|(req, _)| (req, rest)), CurrentSong::COMMAND => CurrentSong::parse_request(parts),
_ => todo!(), Idle::COMMAND => Idle::parse_request(parts),
Status::COMMAND => Status::parse_request(parts),
Stats::COMMAND => Stats::parse_request(parts),
/* playback options */
Consume::COMMAND => Consume::parse_request(parts),
Crossfade::COMMAND => Crossfade::parse_request(parts),
MixRampDb::COMMAND => MixRampDb::parse_request(parts),
MixRampDelay::COMMAND => MixRampDelay::parse_request(parts),
Random::COMMAND => Random::parse_request(parts),
Repeat::COMMAND => Repeat::parse_request(parts),
SetVol::COMMAND => SetVol::parse_request(parts),
GetVol::COMMAND => GetVol::parse_request(parts),
Single::COMMAND => Single::parse_request(parts),
ReplayGainMode::COMMAND => ReplayGainMode::parse_request(parts),
ReplayGainStatus::COMMAND => ReplayGainStatus::parse_request(parts),
Volume::COMMAND => Volume::parse_request(parts),
/* playback control */
Next::COMMAND => Next::parse_request(parts),
Pause::COMMAND => Pause::parse_request(parts),
Play::COMMAND => Play::parse_request(parts),
PlayId::COMMAND => PlayId::parse_request(parts),
Previous::COMMAND => Previous::parse_request(parts),
Seek::COMMAND => Seek::parse_request(parts),
SeekId::COMMAND => SeekId::parse_request(parts),
SeekCur::COMMAND => SeekCur::parse_request(parts),
Stop::COMMAND => Stop::parse_request(parts),
/* queue */
Add::COMMAND => Add::parse_request(parts),
AddId::COMMAND => AddId::parse_request(parts),
Clear::COMMAND => Clear::parse_request(parts),
Delete::COMMAND => Delete::parse_request(parts),
DeleteId::COMMAND => DeleteId::parse_request(parts),
Move::COMMAND => Move::parse_request(parts),
MoveId::COMMAND => MoveId::parse_request(parts),
Playlist::COMMAND => Playlist::parse_request(parts),
PlaylistFind::COMMAND => PlaylistFind::parse_request(parts),
PlaylistId::COMMAND => PlaylistId::parse_request(parts),
PlaylistInfo::COMMAND => PlaylistInfo::parse_request(parts),
PlaylistSearch::COMMAND => PlaylistSearch::parse_request(parts),
PlChanges::COMMAND => PlChanges::parse_request(parts),
PlChangesPosId::COMMAND => PlChangesPosId::parse_request(parts),
Prio::COMMAND => Prio::parse_request(parts),
PrioId::COMMAND => PrioId::parse_request(parts),
RangeId::COMMAND => RangeId::parse_request(parts),
Shuffle::COMMAND => Shuffle::parse_request(parts),
Swap::COMMAND => Swap::parse_request(parts),
SwapId::COMMAND => SwapId::parse_request(parts),
AddTagId::COMMAND => AddTagId::parse_request(parts),
ClearTagId::COMMAND => ClearTagId::parse_request(parts),
/* stored playlists */
/* music database */
/* mounts and neighbors */
/* stickers */
/* connection settings */
/* partition commands */
/* audio output devices */
DisableOutput::COMMAND => DisableOutput::parse_request(parts),
EnableOutput::COMMAND => EnableOutput::parse_request(parts),
ToggleOutput::COMMAND => ToggleOutput::parse_request(parts),
Outputs::COMMAND => Outputs::parse_request(parts),
OutputSet::COMMAND => OutputSet::parse_request(parts),
/* reflection */
/* client to client */
Subscribe::COMMAND => Subscribe::parse_request(parts),
Unsubscribe::COMMAND => Unsubscribe::parse_request(parts),
Channels::COMMAND => Channels::parse_request(parts),
ReadMessages::COMMAND => ReadMessages::parse_request(parts),
SendMessage::COMMAND => SendMessage::parse_request(parts),
_ => unimplemented!(),
} }
} }
} }