Files
empidee/src/commands/controlling_playback/seek.rs
T
oysteikt 56d7c942db commands: remove toplevel Request/Response enums
This also exposes all the command types as public API
2026-06-21 15:39:23 +09:00

69 lines
1.9 KiB
Rust

use serde::{Deserialize, Serialize};
use crate::{
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
request_tokenizer::RequestTokenizer,
types::{SongPosition, TimeWithFractions},
};
pub struct Seek;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SeekRequest {
pub songpos: SongPosition,
pub time: TimeWithFractions,
}
impl SeekRequest {
pub fn new(songpos: SongPosition, time: TimeWithFractions) -> Self {
Self { songpos, time }
}
}
impl CommandRequest for SeekRequest {
const COMMAND: &'static str = "seek";
const MIN_ARGS: u32 = 2;
const MAX_ARGS: Option<u32> = Some(2);
fn serialize(&self) -> String {
format!("{} {} {}\n", Self::COMMAND, self.songpos, self.time)
}
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
let songpos = match parts.next() {
Some(s) => {
s.parse::<SongPosition>()
.map_err(|_| RequestParserError::SubtypeParserError {
argument_index: 0,
expected_type: "SongPosition",
raw_input: s.to_owned(),
})?
}
None => return Err(Self::missing_arguments_error(0)),
};
let time = match parts.next() {
Some(t) => t.parse::<TimeWithFractions>().map_err(|_| {
RequestParserError::SubtypeParserError {
argument_index: 1,
expected_type: "TimeWithFractions",
raw_input: t.to_owned(),
}
})?,
None => return Err(Self::missing_arguments_error(1)),
};
Self::throw_if_too_many_arguments(parts)?;
Ok(SeekRequest { songpos, time })
}
}
empty_command_response!(Seek);
impl Command for Seek {
type Request = SeekRequest;
type Response = SeekResponse;
}