mpvsock/command/commands/
mod.rs

1use std::borrow::Cow;
2
3use crate::model::FileloadInfo;
4
5use super::{property::MpvProperty, MpvCommandRaw};
6
7use super::MpvCommand;
8
9pub struct CmdRawText<S: AsRef<str>>(S);
10impl<S: AsRef<str>> CmdRawText<S> {
11	pub fn new(text: S) -> Self {
12		Self(text)
13	}
14}
15impl<S: AsRef<str>> MpvCommandRaw for CmdRawText<S> {
16	fn write(
17		&self,
18		mut w: impl std::io::Write,
19		_request_id: Option<std::num::NonZeroI64>
20	) -> std::io::Result<()> {
21		write!(w, "{}", self.0.as_ref())
22	}
23}
24
25pub struct CmdRawJsonArgs<S: AsRef<str>>(S);
26impl<S: AsRef<str>> CmdRawJsonArgs<S> {
27	pub fn new(text: S) -> Self {
28		Self(text)
29	}
30}
31impl<S: AsRef<str>> MpvCommand for CmdRawJsonArgs<S> {
32	type Data = Option<serde_json::Value>;
33	type Error = std::convert::Infallible;
34	type ParsedData = serde_json::Value;
35
36	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
37		write!(w, "{}", self.0.as_ref())
38	}
39
40	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
41		let value = match data {
42			None => serde_json::Value::Null,
43			Some(value) => value
44		};
45
46		Ok(value)
47	}
48}
49
50pub struct CmdGetVersion(std::marker::PhantomData<()>);
51impl CmdGetVersion {
52	pub fn new() -> Self {
53		CmdGetVersion(std::marker::PhantomData)
54	}
55}
56impl MpvCommand for CmdGetVersion {
57	type Data = u32;
58	type Error = serde_json::Error;
59	type ParsedData = (u16, u16);
60
61	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
62		write!(w, "\"get_version\"")
63	}
64
65	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
66		let major = (data >> 16) & 0xFFFF;
67		let minor = data & 0xFFFF;
68
69		Ok((major as u16, minor as u16))
70	}
71}
72
73pub struct CmdGetProperty<P: MpvProperty>(P);
74impl<P: MpvProperty> CmdGetProperty<P> {
75	pub fn new(property: P) -> Self {
76		CmdGetProperty(property)
77	}
78}
79impl<P: MpvProperty> MpvCommand for CmdGetProperty<P> {
80	type Data = P::Value;
81	type Error = std::convert::Infallible;
82	type ParsedData = Self::Data;
83
84	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
85		write!(w, "\"get_property\",\"{}\"", self.0.name())
86	}
87
88	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
89		Ok(data)
90	}
91}
92
93
94pub struct CmdSetProperty<P: MpvProperty>(P, P::Value);
95impl<P: MpvProperty> CmdSetProperty<P> {
96	pub fn new(property: P, value: P::Value) -> Self {
97		CmdSetProperty(property, value)
98	}
99}
100impl<P: MpvProperty> MpvCommand for CmdSetProperty<P> {
101	type Data = Option<()>;
102	type Error = serde_json::Error;
103	type ParsedData = Self::Data;
104
105	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
106		write!(w, "\"set_property\",\"{}\",", self.0.name())?;
107		serde_json::to_writer(w, &self.1)?;
108
109		Ok(())
110	}
111
112	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
113		Ok(data)
114	}
115}
116
117pub struct CmdCycleProperty<P: MpvProperty>(P, bool);
118impl<P: MpvProperty> CmdCycleProperty<P> {
119	pub fn new(property: P, down: bool) -> Self {
120		CmdCycleProperty(property, down)
121	}
122}
123impl<P: MpvProperty> MpvCommand for CmdCycleProperty<P> {
124	type Data = Option<()>;
125	type Error = serde_json::Error;
126	type ParsedData = Self::Data;
127
128	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
129		let direction = match self.1 {
130			false => "up",
131			true => "down"
132		};
133
134		write!(w, "\"cycle\",\"{}\",\"{}\"", self.0.name(), direction)?;
135
136
137		Ok(())
138	}
139
140	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
141		Ok(data)
142	}
143}
144
145pub struct CmdObserveProperty<P: MpvProperty>(u32, P);
146impl<P: MpvProperty> CmdObserveProperty<P> {
147	pub fn new(observer_id: u32, property: P) -> Self {
148		CmdObserveProperty(observer_id, property)
149	}
150}
151impl<P: MpvProperty> MpvCommand for CmdObserveProperty<P> {
152	type Data = Option<()>;
153	type Error = std::convert::Infallible;
154	type ParsedData = Self::Data;
155
156	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
157		write!(w, "\"observe_property\",{},\"{}\"", self.0, self.1.name())
158	}
159
160	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
161		Ok(data)
162	}
163}
164
165pub struct CmdUnobserveProperty(u32);
166impl CmdUnobserveProperty {
167	pub fn new(observer_id: u32) -> Self {
168		CmdUnobserveProperty(observer_id)
169	}
170}
171impl MpvCommand for CmdUnobserveProperty {
172	type Data = Option<()>;
173	type Error = std::convert::Infallible;
174	type ParsedData = Self::Data;
175
176	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
177		write!(w, "\"unobserve_property\",{}", self.0)
178	}
179
180	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
181		Ok(data)
182	}
183}
184
185
186pub struct CmdLoadfile<'a>(Cow<'a, str>, bool);
187impl<'a> CmdLoadfile<'a> {
188	pub fn new(file_path: Cow<'a, str>) -> Self {
189		CmdLoadfile(file_path, false)
190	}
191
192	pub fn new_append(file_path: Cow<'a, str>) -> Self {
193		CmdLoadfile(file_path, true)
194	}
195}
196impl<'a> MpvCommand for CmdLoadfile<'a> {
197	type Data = FileloadInfo;
198	type Error = std::convert::Infallible;
199	type ParsedData = Self::Data;
200
201	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
202		if self.1 {
203			write!(w, "\"loadfile\",\"{}\",\"append\"", self.0)
204		} else {
205			write!(w, "\"loadfile\",\"{}\"", self.0)
206		}
207	}
208
209	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
210		Ok(data)
211	}
212}
213
214pub struct CmdStop(bool);
215impl CmdStop {
216	pub fn new(keep_playlist: bool) -> Self {
217		CmdStop(keep_playlist)
218	}
219}
220impl MpvCommand for CmdStop {
221	type Data = Option<()>;
222	type Error = std::convert::Infallible;
223	type ParsedData = Self::Data;
224
225	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
226		if self.0 {
227			write!(w, "\"stop\",\"keep-playlist\"")
228		} else {
229			write!(w, "\"stop\"")
230		}
231	}
232
233	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
234		Ok(data)
235	}
236}
237
238enum CmdSeekInner {
239	AbsoluteTime(f64),
240	AbsolutePercent(f64),
241	RelativeTime(f64),
242	RelativePercent(f64)
243}
244pub struct CmdSeek(CmdSeekInner);
245impl CmdSeek {
246	pub fn time(time: f64, absolute: bool) -> Self {
247		if absolute {
248			CmdSeek(CmdSeekInner::AbsoluteTime(time))
249		} else {
250			CmdSeek(CmdSeekInner::RelativeTime(time))
251		}
252	}
253
254	pub fn percent(percent: f64, absolute: bool) -> Self {
255		if absolute {
256			CmdSeek(CmdSeekInner::AbsolutePercent(percent))
257		} else {
258			CmdSeek(CmdSeekInner::RelativePercent(percent))
259		}
260	}
261}
262impl MpvCommand for CmdSeek {
263	type Data = Option<()>;
264	type Error = std::convert::Infallible;
265	type ParsedData = Self::Data;
266
267	fn write_args(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
268		match self.0 {
269			CmdSeekInner::AbsoluteTime(time) => write!(w, "\"seek\",{},\"absolute\"", time),
270			CmdSeekInner::AbsolutePercent(percent) => {
271				write!(w, "\"seek\",{},\"absolute-percent\"", percent)
272			}
273			CmdSeekInner::RelativeTime(time) => write!(w, "\"seek\",{},\"relative\"", time),
274			CmdSeekInner::RelativePercent(percent) => {
275				write!(w, "\"seek\",{},\"relative-percent\"", percent)
276			}
277		}
278	}
279
280	fn parse_data(&self, data: Self::Data) -> Result<Self::ParsedData, Self::Error> {
281		Ok(data)
282	}
283}
284
285pub struct CmdShowProgress(std::marker::PhantomData<()>);
286impl CmdShowProgress {
287	pub fn new() -> Self {
288		CmdShowProgress(std::marker::PhantomData)
289	}
290}
291impl MpvCommandRaw for CmdShowProgress {
292	fn write(
293		&self,
294		mut w: impl std::io::Write,
295		_request_id: Option<std::num::NonZeroI64>
296	) -> std::io::Result<()> {
297		write!(w, "show-progress")
298	}
299}
300
301pub struct CmdPlaylistClear(std::marker::PhantomData<()>);
302impl CmdPlaylistClear {
303	pub fn new() -> Self {
304		CmdPlaylistClear(std::marker::PhantomData)
305	}
306}
307impl MpvCommandRaw for CmdPlaylistClear {
308	fn write(
309		&self,
310		mut w: impl std::io::Write,
311		_request_id: Option<std::num::NonZeroI64>
312	) -> std::io::Result<()> {
313		write!(w, "playlist-clear")
314	}
315}
316
317pub struct CmdPlaylistShuffle(std::marker::PhantomData<()>);
318impl CmdPlaylistShuffle {
319	pub fn new() -> Self {
320		CmdPlaylistShuffle(std::marker::PhantomData)
321	}
322}
323impl MpvCommandRaw for CmdPlaylistShuffle {
324	fn write(
325		&self,
326		mut w: impl std::io::Write,
327		_request_id: Option<std::num::NonZeroI64>
328	) -> std::io::Result<()> {
329		write!(w, "playlist-shuffle")
330	}
331}