Compare commits
94 Commits
deduplicat
...
add-constr
| Author | SHA1 | Date | |
|---|---|---|---|
|
291aa6877c
|
|||
|
c48a8adcdc
|
|||
|
1f1e7fbb53
|
|||
|
7ec268094d
|
|||
|
afa690366f
|
|||
|
02ba7f2684
|
|||
|
44903f2ce0
|
|||
|
9f50d61ad5
|
|||
|
788a01ce83
|
|||
|
f5451b6c2f
|
|||
|
2de18cdbdb
|
|||
|
23bfd0c4a7
|
|||
|
3342293bf2
|
|||
|
2b3ab7389d
|
|||
|
f80d36e962
|
|||
|
86edd4c5b3
|
|||
|
a7a8ceedeb
|
|||
|
7ce0d68021
|
|||
|
b5cb4677ee
|
|||
|
38faf99de7
|
|||
|
69f252bad8
|
|||
|
c177c089a3
|
|||
|
d964e7857b
|
|||
|
8430592bee
|
|||
|
78cfc09d60
|
|||
|
a7b764ad0f
|
|||
|
f3e6fe13df
|
|||
|
4f8f5db620
|
|||
|
1e39640508
|
|||
|
b388bc727b
|
|||
|
9f859e1df1
|
|||
|
4bb5702eba
|
|||
|
955fdbcff1
|
|||
|
322c8c8fc3
|
|||
|
93afaf1bde
|
|||
|
c915c67f08
|
|||
|
813ffac614
|
|||
|
b0bc2752cc
|
|||
|
07e9161137
|
|||
|
f2977f1ba9
|
|||
|
b0b4134829
|
|||
|
c1dbdd4644
|
|||
|
64c94d6e89
|
|||
|
4d11df5ad1
|
|||
|
0cacbe2229
|
|||
|
7bbe1c4ced
|
|||
|
83918fd432
|
|||
|
07e1c76aa9
|
|||
|
d84e653db2
|
|||
|
7834bbd956
|
|||
|
249ffb2a36
|
|||
|
fdd4880d05
|
|||
|
9ca6544057
|
|||
|
e5f70ca87a
|
|||
|
b03f60c985
|
|||
|
b22016e970
|
|||
|
65c7798d01
|
|||
|
5188809327
|
|||
|
a4276a2caa
|
|||
|
7b27a650a1
|
|||
|
062dbcafb8
|
|||
|
57a6b0a3ee
|
|||
|
b5fbaadca2
|
|||
|
3bd7aaaad2
|
|||
|
8aba3d8859
|
|||
|
e4ece7d6b2
|
|||
|
f07f90aee5
|
|||
|
6e2aa2ba65
|
|||
|
1ef8ef669a
|
|||
|
c6a123a6e1
|
|||
|
06e24f0ce0
|
|||
|
da31ab75e2
|
|||
|
d09ca013d5
|
|||
|
10913fd48c
|
|||
|
73ddb6d498
|
|||
|
ede28623ef
|
|||
|
130fe49597
|
|||
|
4a1df97ad6
|
|||
|
7a966051d5
|
|||
|
7dc3d7f9cf
|
|||
|
2b1e99445a
|
|||
|
e932b62195
|
|||
|
49f440770e
|
|||
|
153ae9520f
|
|||
|
424c530d5d
|
|||
|
a637f24e66
|
|||
|
1d693b7b2a
|
|||
|
484b1fb68d
|
|||
|
2fa58533ba
|
|||
|
088665c9ff
|
|||
|
3e2e3fdc68
|
|||
|
59994ce740
|
|||
|
b42cad0b52
|
|||
|
3c49ece1a9
|
@@ -7,20 +7,20 @@ on:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: debian-latest-slim
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Install rust toolchain
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
|
||||
- name: Build
|
||||
run: cargo build --all-features --verbose --release
|
||||
run: cargo build --verbose --release
|
||||
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: debian-latest-slim
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Install rust toolchain
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
@@ -31,12 +31,13 @@ jobs:
|
||||
run: cargo fmt -- --check
|
||||
|
||||
- name: Check clippy
|
||||
run: cargo clippy --all-features -- --deny warnings
|
||||
# run: cargo clippy -- --deny warnings
|
||||
run: cargo clippy
|
||||
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: debian-latest-slim
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
- uses: cargo-bins/cargo-binstall@main
|
||||
|
||||
- name: Install rust toolchain
|
||||
@@ -49,7 +50,7 @@ jobs:
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
cargo nextest run --all-features --release --no-fail-fast
|
||||
cargo nextest run --release --no-fail-fast
|
||||
env:
|
||||
RUST_LOG: "trace"
|
||||
RUSTFLAGS: "-Cinstrument-coverage"
|
||||
@@ -83,13 +84,13 @@ jobs:
|
||||
target: ${{ gitea.ref_name }}/coverage/
|
||||
username: gitea-web
|
||||
ssh-key: ${{ secrets.WEB_SYNC_SSH_KEY }}
|
||||
host: bekkalokk.pvv.ntnu.no
|
||||
known-hosts: "bekkalokk.pvv.ntnu.no ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEI6VSaDrMG8+flg4/AeHlAFIen8RUzWh6URQKqFegSx"
|
||||
host: pages.pvv.ntnu.no
|
||||
known-hosts: "pages.pvv.ntnu.no ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH2QjfFB+city1SYqltkVqWACfo1j37k+oQQfj13mtgg"
|
||||
|
||||
docs:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: debian-latest-slim
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Install latest nightly toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
@@ -98,7 +99,7 @@ jobs:
|
||||
override: true
|
||||
|
||||
- name: Build docs
|
||||
run: cargo doc --all-features --document-private-items --release
|
||||
run: cargo doc --document-private-items --release
|
||||
|
||||
- name: Transfer files
|
||||
uses: https://git.pvv.ntnu.no/Projects/rsync-action@v1
|
||||
@@ -107,6 +108,5 @@ jobs:
|
||||
target: ${{ gitea.ref_name }}/docs/
|
||||
username: gitea-web
|
||||
ssh-key: ${{ secrets.WEB_SYNC_SSH_KEY }}
|
||||
host: bekkalokk.pvv.ntnu.no
|
||||
known-hosts: "bekkalokk.pvv.ntnu.no ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEI6VSaDrMG8+flg4/AeHlAFIen8RUzWh6URQKqFegSx"
|
||||
|
||||
host: pages.pvv.ntnu.no
|
||||
known-hosts: "pages.pvv.ntnu.no ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH2QjfFB+city1SYqltkVqWACfo1j37k+oQQfj13mtgg"
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
/target
|
||||
result
|
||||
result-*
|
||||
|
||||
Cargo.lock
|
||||
|
||||
95
Cargo.lock
generated
95
Cargo.lock
generated
@@ -1,95 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "diff"
|
||||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
|
||||
|
||||
[[package]]
|
||||
name = "empidee"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"indoc",
|
||||
"pretty_assertions",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "2.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd"
|
||||
|
||||
[[package]]
|
||||
name = "pretty_assertions"
|
||||
version = "1.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d"
|
||||
dependencies = [
|
||||
"diff",
|
||||
"yansi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.95"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.219"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.219"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.101"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
|
||||
[[package]]
|
||||
name = "yansi"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
|
||||
20
Cargo.toml
20
Cargo.toml
@@ -11,8 +11,24 @@ edition = "2024"
|
||||
rust-version = "1.85.0"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.210", features = ["derive"] }
|
||||
chrono = { version = "0.4.42", features = ["serde"] }
|
||||
futures-util = { version = "0.3.31", optional = true, features = ["io"] }
|
||||
lalrpop-util = { version = "0.22.2", features = ["lexer"] }
|
||||
paste = "1.0.15"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
thiserror = "2.0.17"
|
||||
tokio = { version = "1.48.0", optional = true, features = ["io-util"] }
|
||||
|
||||
[features]
|
||||
default = ["tokio"]
|
||||
futures = ["dep:futures-util"]
|
||||
tokio = ["dep:tokio"]
|
||||
|
||||
[dev-dependencies]
|
||||
indoc = "2.0.5"
|
||||
anyhow = "1.0.100"
|
||||
indoc = "2.0.7"
|
||||
pretty_assertions = "1.4.1"
|
||||
tokio = { version = "1.48.0", features = ["macros", "net", "rt"] }
|
||||
|
||||
[build-dependencies]
|
||||
lalrpop = "0.22.2"
|
||||
|
||||
8
build.rs
Normal file
8
build.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
fn main() {
|
||||
lalrpop::process_root().unwrap();
|
||||
// let debug_mode = std::env::var("PROFILE").unwrap() == "debug";
|
||||
// lalrpop::Configuration::new()
|
||||
// .emit_comments(debug_mode)
|
||||
// .process()
|
||||
// .unwrap();
|
||||
}
|
||||
@@ -1,3 +1,14 @@
|
||||
fn main() {
|
||||
todo!()
|
||||
use empidee::MpdClient;
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let socket = tokio::net::TcpSocket::new_v4()?;
|
||||
let mut stream = socket.connect("127.0.0.1:6600".parse()?).await?;
|
||||
|
||||
let mut client = MpdClient::new(&mut stream);
|
||||
println!("{}", client.read_initial_mpd_version().await?);
|
||||
|
||||
client.play(None).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
12
flake.lock
generated
12
flake.lock
generated
@@ -2,11 +2,11 @@
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1746461020,
|
||||
"narHash": "sha256-7+pG1I9jvxNlmln4YgnlW4o+w0TZX24k688mibiFDUE=",
|
||||
"lastModified": 1764950072,
|
||||
"narHash": "sha256-BmPWzogsG2GsXZtlT+MTcAWeDK5hkbGRZTeZNW42fwA=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "3730d8a308f94996a9ba7c7138ede69c1b9ac4ae",
|
||||
"rev": "f61125a668a320878494449750330ca58b78c557",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -29,11 +29,11 @@
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1746585402,
|
||||
"narHash": "sha256-Pf+ufu6bYNA1+KQKHnGMNEfTwpD9ZIcAeLoE2yPWIP0=",
|
||||
"lastModified": 1765075567,
|
||||
"narHash": "sha256-KFDCdQcHJ0hE3Nt5Gm5enRIhmtEifAjpxgUQ3mzSJpA=",
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"rev": "72dd969389583664f87aa348b3458f2813693617",
|
||||
"rev": "769156779b41e8787a46ca3d7d76443aaf68be6f",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
default = pkgs.mkShell {
|
||||
nativeBuildInputs = [
|
||||
toolchain
|
||||
pkgs.cargo-edit
|
||||
];
|
||||
|
||||
RUST_SRC_PATH = "${toolchain}/lib/rustlib/src/rust/library";
|
||||
|
||||
105
src/client.rs
Normal file
105
src/client.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
//! A high-level client for interacting with an Mpd server.
|
||||
//!
|
||||
//! The client provides methods for common operations such as playing, pausing, and
|
||||
//! managing the playlist, and returns the expected response types directly
|
||||
//! from its methods.
|
||||
|
||||
use crate::{Request, commands::*, types::SongPosition};
|
||||
|
||||
#[cfg(feature = "futures")]
|
||||
use futures_util::{
|
||||
AsyncBufReadExt,
|
||||
io::{AsyncRead, AsyncWrite, AsyncWriteExt, BufReader},
|
||||
};
|
||||
|
||||
use thiserror::Error;
|
||||
#[cfg(feature = "tokio")]
|
||||
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader};
|
||||
|
||||
pub struct MpdClient<'a, T>
|
||||
where
|
||||
T: AsyncWrite + AsyncRead + Unpin,
|
||||
{
|
||||
connection: &'a mut T,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum MpdClientError {
|
||||
#[error("Connection error: {0}")]
|
||||
ConnectionError(#[from] std::io::Error),
|
||||
|
||||
#[error("Failed to parse MPD response: {0}")]
|
||||
ResponseParseError(#[from] crate::commands::ResponseParserError),
|
||||
|
||||
#[error("MPD returned an error: {0}")]
|
||||
MpdError(#[from] crate::response::MpdError),
|
||||
}
|
||||
|
||||
impl<'a, T> MpdClient<'a, T>
|
||||
where
|
||||
T: AsyncWrite + AsyncRead + Unpin,
|
||||
{
|
||||
pub fn new(connection: &'a mut T) -> Self {
|
||||
MpdClient { connection }
|
||||
}
|
||||
|
||||
pub async fn read_initial_mpd_version(&mut self) -> Result<String, MpdClientError> {
|
||||
let mut reader = BufReader::new(&mut self.connection);
|
||||
let mut version_line = String::new();
|
||||
|
||||
reader
|
||||
.read_line(&mut version_line)
|
||||
.await
|
||||
.map_err(MpdClientError::ConnectionError)?;
|
||||
|
||||
Ok(version_line.trim().to_string())
|
||||
}
|
||||
|
||||
async fn read_response(&mut self) -> Result<Vec<u8>, MpdClientError> {
|
||||
let mut response = Vec::new();
|
||||
let mut reader = BufReader::new(&mut self.connection);
|
||||
|
||||
loop {
|
||||
let mut line = Vec::new();
|
||||
|
||||
let bytes_read = reader
|
||||
.read_until(b'\n', &mut line)
|
||||
.await
|
||||
.map_err(MpdClientError::ConnectionError)?;
|
||||
|
||||
if bytes_read == 0 {
|
||||
break; // EOF reached
|
||||
}
|
||||
|
||||
response.extend_from_slice(&line);
|
||||
|
||||
if line == b"OK\n" || line.starts_with(b"ACK ") {
|
||||
break; // End of response
|
||||
}
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn play(
|
||||
&mut self,
|
||||
position: Option<SongPosition>,
|
||||
) -> Result<PlayResponse, MpdClientError> {
|
||||
let message = Request::Play(position);
|
||||
let payload = message.serialize();
|
||||
|
||||
self.connection
|
||||
.write_all(payload.as_bytes())
|
||||
.await
|
||||
.map_err(MpdClientError::ConnectionError)?;
|
||||
|
||||
self.connection
|
||||
.flush()
|
||||
.await
|
||||
.map_err(MpdClientError::ConnectionError)?;
|
||||
|
||||
let response_bytes = self.read_response().await?;
|
||||
let response = PlayResponse::parse_raw(&response_bytes)?;
|
||||
Ok(response)
|
||||
}
|
||||
}
|
||||
1135
src/commands.rs
1135
src/commands.rs
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,11 @@
|
||||
pub mod disableoutput;
|
||||
pub mod enableoutput;
|
||||
pub mod outputs;
|
||||
pub mod outputset;
|
||||
pub mod toggleoutput;
|
||||
mod disableoutput;
|
||||
mod enableoutput;
|
||||
mod outputs;
|
||||
mod outputset;
|
||||
mod toggleoutput;
|
||||
|
||||
pub use disableoutput::DisableOutput;
|
||||
pub use enableoutput::EnableOutput;
|
||||
pub use outputs::Outputs;
|
||||
pub use outputset::OutputSet;
|
||||
pub use toggleoutput::ToggleOutput;
|
||||
pub use disableoutput::*;
|
||||
pub use enableoutput::*;
|
||||
pub use outputs::*;
|
||||
pub use outputset::*;
|
||||
pub use toggleoutput::*;
|
||||
|
||||
@@ -1,26 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::AudioOutputId,
|
||||
};
|
||||
|
||||
pub struct DisableOutput;
|
||||
|
||||
single_item_command_request!(DisableOutput, "disableoutput", AudioOutputId);
|
||||
|
||||
empty_command_response!(DisableOutput);
|
||||
|
||||
impl Command for DisableOutput {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "disableoutput";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let output_id = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::DisableOutput(output_id.to_string()), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError<'_>> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = DisableOutputRequest;
|
||||
type Response = DisableOutputResponse;
|
||||
}
|
||||
|
||||
@@ -1,26 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::AudioOutputId,
|
||||
};
|
||||
|
||||
pub struct EnableOutput;
|
||||
|
||||
single_item_command_request!(EnableOutput, "enableoutput", AudioOutputId);
|
||||
|
||||
empty_command_response!(EnableOutput);
|
||||
|
||||
impl Command for EnableOutput {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "enableoutput";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let output_id = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::EnableOutput(output_id.to_string()), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = EnableOutputRequest;
|
||||
type Response = EnableOutputResponse;
|
||||
}
|
||||
|
||||
@@ -2,35 +2,171 @@ use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
|
||||
response_tokenizer::{ResponseAttributes, expect_property_type},
|
||||
types::AudioOutputId,
|
||||
};
|
||||
|
||||
pub struct Outputs;
|
||||
|
||||
empty_command_request!(Outputs, "outputs");
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct OutputsResponse(Vec<Output>);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Output {
|
||||
pub id: u64,
|
||||
pub id: AudioOutputId,
|
||||
pub name: String,
|
||||
pub plugin: String,
|
||||
pub enabled: bool,
|
||||
pub attribute: HashMap<String, String>,
|
||||
}
|
||||
|
||||
pub type OutputsResponse = Vec<Output>;
|
||||
impl CommandResponse for OutputsResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts = parts.into_vec()?;
|
||||
|
||||
let result_len = parts.iter().filter(|(k, _)| *k == "outputid").count();
|
||||
let mut outputs = Vec::with_capacity(result_len);
|
||||
|
||||
let mut id: Option<AudioOutputId> = None;
|
||||
let mut name: Option<String> = None;
|
||||
let mut plugin: Option<String> = None;
|
||||
let mut enabled: Option<bool> = None;
|
||||
let mut attributes: HashMap<String, String> = HashMap::new();
|
||||
|
||||
for (k, v) in parts.into_iter() {
|
||||
match k {
|
||||
"outputid" => {
|
||||
// Reset and store the previous output if all fields are present
|
||||
if let (Some(id), Some(name), Some(plugin), Some(enabled)) =
|
||||
(id.take(), name.take(), plugin.take(), enabled.take())
|
||||
{
|
||||
outputs.push(Output {
|
||||
id,
|
||||
name,
|
||||
plugin,
|
||||
enabled,
|
||||
attribute: attributes,
|
||||
});
|
||||
}
|
||||
attributes = HashMap::new();
|
||||
let id_s = expect_property_type!(Some(v), k, Text);
|
||||
id = Some(
|
||||
id_s.parse()
|
||||
.map_err(|_| ResponseParserError::SyntaxError(0, id_s.to_string()))?,
|
||||
);
|
||||
}
|
||||
"outputname" => {
|
||||
name = Some(expect_property_type!(Some(v), k, Text).to_string());
|
||||
}
|
||||
"plugin" => {
|
||||
plugin = Some(expect_property_type!(Some(v), k, Text).to_string());
|
||||
}
|
||||
"outputenabled" => {
|
||||
let val_s = expect_property_type!(Some(v), k, Text);
|
||||
let val: u64 = val_s
|
||||
.parse::<u64>()
|
||||
.map_err(|_| ResponseParserError::SyntaxError(0, val_s.to_string()))?;
|
||||
enabled = Some(val != 0);
|
||||
}
|
||||
"attribute" => {
|
||||
let value = expect_property_type!(Some(v), k, Text);
|
||||
let mut parts = value.splitn(2, '=');
|
||||
let attr_key = parts
|
||||
.next()
|
||||
.ok_or(ResponseParserError::SyntaxError(0, value.to_string()))?
|
||||
.to_string();
|
||||
let attr_value = parts
|
||||
.next()
|
||||
.ok_or(ResponseParserError::SyntaxError(0, value.to_string()))?
|
||||
.to_string();
|
||||
attributes.insert(attr_key, attr_value);
|
||||
}
|
||||
_ => {
|
||||
return Err(ResponseParserError::UnexpectedProperty(k.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Store the last output if all fields are present
|
||||
if let (Some(id), Some(name), Some(plugin), Some(enabled)) =
|
||||
(id.take(), name.take(), plugin.take(), enabled.take())
|
||||
{
|
||||
outputs.push(Output {
|
||||
id,
|
||||
name,
|
||||
plugin,
|
||||
enabled,
|
||||
attribute: attributes,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(OutputsResponse(outputs))
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Outputs {
|
||||
type Request = OutputsRequest;
|
||||
type Response = OutputsResponse;
|
||||
const COMMAND: &'static str = "outputs";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Outputs, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
_parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use indoc::indoc;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_response() {
|
||||
let input = indoc! {"
|
||||
outputid: 0
|
||||
outputname: PipeWire Sound Server
|
||||
plugin: pipewire
|
||||
outputenabled: 1
|
||||
outputid: 1
|
||||
outputname: Visualizer feed
|
||||
plugin: fifo
|
||||
outputenabled: 1
|
||||
attribute: fifo_path=/tmp/empidee-visualizer.fifo
|
||||
OK
|
||||
"};
|
||||
let result = Outputs::parse_raw_response(input.as_bytes());
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok(OutputsResponse(vec![
|
||||
Output {
|
||||
id: 0,
|
||||
name: "PipeWire Sound Server".to_string(),
|
||||
plugin: "pipewire".to_string(),
|
||||
enabled: true,
|
||||
attribute: HashMap::new(),
|
||||
},
|
||||
Output {
|
||||
id: 1,
|
||||
name: "Visualizer feed".to_string(),
|
||||
plugin: "fifo".to_string(),
|
||||
enabled: true,
|
||||
attribute: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(
|
||||
"fifo_path".to_string(),
|
||||
"/tmp/empidee-visualizer.fifo".to_string(),
|
||||
);
|
||||
map
|
||||
},
|
||||
},
|
||||
])),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,87 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::AudioOutputId,
|
||||
};
|
||||
|
||||
pub struct OutputSet;
|
||||
|
||||
impl Command for OutputSet {
|
||||
type Response = ();
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct OutputSetRequest {
|
||||
pub output_id: AudioOutputId,
|
||||
pub attribute_name: String,
|
||||
pub attribute_value: String,
|
||||
}
|
||||
|
||||
impl OutputSetRequest {
|
||||
fn new(output_id: AudioOutputId, attribute_name: String, attribute_value: String) -> Self {
|
||||
Self {
|
||||
output_id,
|
||||
attribute_name,
|
||||
attribute_value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRequest for OutputSetRequest {
|
||||
const COMMAND: &'static str = "outputset";
|
||||
const MIN_ARGS: u32 = 3;
|
||||
const MAX_ARGS: Option<u32> = Some(3);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let output_id = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
let attribute_name = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
let attribute_value = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((
|
||||
Request::OutputSet(
|
||||
output_id.to_string(),
|
||||
attribute_name.to_string(),
|
||||
attribute_value.to_string(),
|
||||
),
|
||||
"",
|
||||
))
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::OutputSet(self.output_id, self.attribute_name, self.attribute_value)
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::OutputSet(output_id, attribute_name, attribute_value) => {
|
||||
Some(OutputSetRequest {
|
||||
output_id,
|
||||
attribute_name,
|
||||
attribute_value,
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
format!(
|
||||
"{} {} {} {}",
|
||||
Self::COMMAND,
|
||||
self.output_id,
|
||||
self.attribute_name,
|
||||
self.attribute_value
|
||||
)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let output_id = parts.next().ok_or(Self::missing_arguments_error(0))?;
|
||||
let output_id = output_id
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 1,
|
||||
expected_type: "AudioOutputId",
|
||||
raw_input: output_id.to_string(),
|
||||
})?;
|
||||
let attribute_name = parts.next().ok_or(Self::missing_arguments_error(1))?;
|
||||
let attribute_value = parts.next().ok_or(Self::missing_arguments_error(2))?;
|
||||
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok(OutputSetRequest {
|
||||
output_id,
|
||||
attribute_name: attribute_name.to_string(),
|
||||
attribute_value: attribute_value.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
empty_command_response!(OutputSet);
|
||||
|
||||
impl Command for OutputSet {
|
||||
type Request = OutputSetRequest;
|
||||
type Response = OutputSetResponse;
|
||||
}
|
||||
|
||||
@@ -1,26 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::AudioOutputId,
|
||||
};
|
||||
|
||||
pub struct ToggleOutput;
|
||||
|
||||
single_item_command_request!(ToggleOutput, "toggleoutput", AudioOutputId);
|
||||
|
||||
empty_command_response!(ToggleOutput);
|
||||
|
||||
impl Command for ToggleOutput {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "toggleoutput";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let output_id = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ToggleOutput(output_id.to_string()), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = ToggleOutputRequest;
|
||||
type Response = ToggleOutputResponse;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
pub mod channels;
|
||||
pub mod readmessages;
|
||||
pub mod sendmessage;
|
||||
pub mod subscribe;
|
||||
pub mod unsubscribe;
|
||||
mod channels;
|
||||
mod readmessages;
|
||||
mod sendmessage;
|
||||
mod subscribe;
|
||||
mod unsubscribe;
|
||||
|
||||
pub use channels::Channels;
|
||||
pub use readmessages::ReadMessages;
|
||||
pub use sendmessage::SendMessage;
|
||||
pub use subscribe::Subscribe;
|
||||
pub use unsubscribe::Unsubscribe;
|
||||
pub use channels::*;
|
||||
pub use readmessages::*;
|
||||
pub use sendmessage::*;
|
||||
pub use subscribe::*;
|
||||
pub use unsubscribe::*;
|
||||
|
||||
@@ -1,36 +1,39 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
expect_property_type,
|
||||
use crate::{
|
||||
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
|
||||
response_tokenizer::{ResponseAttributes, expect_property_type},
|
||||
types::ChannelName,
|
||||
};
|
||||
|
||||
pub struct Channels;
|
||||
|
||||
empty_command_request!(Channels, "channels");
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ChannelsResponse {
|
||||
pub channels: Vec<String>,
|
||||
pub channels: Vec<ChannelName>,
|
||||
}
|
||||
|
||||
impl Command for Channels {
|
||||
type Response = ChannelsResponse;
|
||||
const COMMAND: &'static str = "channels";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Channels, ""))
|
||||
impl CommandResponse for ChannelsResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: Vec<_> = parts.into();
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: Vec<_> = parts.into_vec()?;
|
||||
let mut channel_names = Vec::with_capacity(parts.len());
|
||||
for (key, value) in parts {
|
||||
debug_assert!(key == "channels");
|
||||
let channel_name = expect_property_type!(Some(value), "channels", Text);
|
||||
channel_names.push(channel_name.to_string());
|
||||
let channel_name = channel_name
|
||||
.parse()
|
||||
.map_err(|_| ResponseParserError::SyntaxError(0, channel_name.to_string()))?;
|
||||
channel_names.push(channel_name);
|
||||
}
|
||||
|
||||
Ok(ChannelsResponse {
|
||||
@@ -39,6 +42,11 @@ impl Command for Channels {
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Channels {
|
||||
type Request = ChannelsRequest;
|
||||
type Response = ChannelsResponse;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -53,11 +61,15 @@ mod tests {
|
||||
channels: baz
|
||||
OK
|
||||
"};
|
||||
let response = Channels::parse_raw_response(response).unwrap();
|
||||
let response = Channels::parse_raw_response(response.as_bytes()).unwrap();
|
||||
assert_eq!(
|
||||
response,
|
||||
ChannelsResponse {
|
||||
channels: vec!["foo".to_string(), "bar".to_string(), "baz".to_string()]
|
||||
channels: vec![
|
||||
"foo".parse().unwrap(),
|
||||
"bar".parse().unwrap(),
|
||||
"baz".parse().unwrap(),
|
||||
]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,31 +1,35 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
expect_property_type,
|
||||
use crate::{
|
||||
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
|
||||
response_tokenizer::{ResponseAttributes, expect_property_type},
|
||||
types::ChannelName,
|
||||
};
|
||||
|
||||
pub struct ReadMessages;
|
||||
|
||||
empty_command_request!(ReadMessages, "readmessages");
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ReadMessagesResponse {
|
||||
pub messages: Vec<(String, String)>,
|
||||
pub struct ReadMessagesResponse(Vec<ReadMessagesResponseEntry>);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ReadMessagesResponseEntry {
|
||||
channel: ChannelName,
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl Command for ReadMessages {
|
||||
type Response = ReadMessagesResponse;
|
||||
const COMMAND: &'static str = "readmessages";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ReadMessages, ""))
|
||||
impl CommandResponse for ReadMessagesResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: Vec<_> = parts.into();
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: Vec<_> = parts.into_vec()?;
|
||||
debug_assert!(parts.len() % 2 == 0);
|
||||
|
||||
let mut messages = Vec::with_capacity(parts.len() / 2);
|
||||
@@ -37,16 +41,25 @@ impl Command for ReadMessages {
|
||||
debug_assert!(ckey == "channel");
|
||||
debug_assert!(mkey == "message");
|
||||
|
||||
let channel = expect_property_type!(Some(cvalue), "channel", Text).to_string();
|
||||
let channel = expect_property_type!(Some(cvalue), "channel", Text);
|
||||
let channel = channel
|
||||
.parse()
|
||||
.map_err(|_| ResponseParserError::SyntaxError(0, channel.to_string()))?;
|
||||
|
||||
let message = expect_property_type!(Some(mvalue), "message", Text).to_string();
|
||||
|
||||
messages.push((channel, message));
|
||||
messages.push(ReadMessagesResponseEntry { channel, message });
|
||||
}
|
||||
|
||||
Ok(ReadMessagesResponse { messages })
|
||||
Ok(ReadMessagesResponse(messages))
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for ReadMessages {
|
||||
type Request = ReadMessagesRequest;
|
||||
type Response = ReadMessagesResponse;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use indoc::indoc;
|
||||
@@ -62,15 +75,19 @@ mod tests {
|
||||
message: message2
|
||||
OK
|
||||
"};
|
||||
let result = ReadMessages::parse_raw_response(input);
|
||||
let result = ReadMessages::parse_raw_response(input.as_bytes());
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok(ReadMessagesResponse {
|
||||
messages: vec![
|
||||
("channel1".to_string(), "message1".to_string()),
|
||||
("channel2".to_string(), "message2".to_string()),
|
||||
]
|
||||
})
|
||||
Ok(ReadMessagesResponse(vec![
|
||||
ReadMessagesResponseEntry {
|
||||
channel: "channel1".parse().unwrap(),
|
||||
message: "message1".to_string(),
|
||||
},
|
||||
ReadMessagesResponseEntry {
|
||||
channel: "channel2".parse().unwrap(),
|
||||
message: "message2".to_string(),
|
||||
},
|
||||
]))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,67 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::ChannelName,
|
||||
};
|
||||
|
||||
pub struct SendMessage;
|
||||
|
||||
impl Command for SendMessage {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "sendmessage";
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SendMessageRequest {
|
||||
pub channel: ChannelName,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let channel = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
impl SendMessageRequest {
|
||||
fn new(channel: ChannelName, message: String) -> Self {
|
||||
Self { channel, message }
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRequest for SendMessageRequest {
|
||||
const COMMAND: &'static str = "sendmessage";
|
||||
const MIN_ARGS: u32 = 2;
|
||||
const MAX_ARGS: Option<u32> = None;
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::SendMessage(self.channel, self.message)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::SendMessage(channel, message) => {
|
||||
Some(SendMessageRequest { channel, message })
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
format!("{} {} {}", Self::COMMAND, self.channel, self.message)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let channel = parts.next().ok_or(Self::missing_arguments_error(0))?;
|
||||
let channel = channel
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "ChannelName",
|
||||
raw_input: channel.to_string(),
|
||||
})?;
|
||||
|
||||
// TODO: SplitWhitespace::remainder() is unstable, use when stable
|
||||
let message = parts.collect::<Vec<_>>().join(" ");
|
||||
|
||||
debug_assert!(!message.is_empty());
|
||||
|
||||
Ok((Request::SendMessage(channel.to_string(), message), ""))
|
||||
Ok(SendMessageRequest { channel, message })
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(SendMessage);
|
||||
|
||||
impl Command for SendMessage {
|
||||
type Request = SendMessageRequest;
|
||||
type Response = SendMessageResponse;
|
||||
}
|
||||
|
||||
@@ -1,26 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::ChannelName,
|
||||
};
|
||||
|
||||
pub struct Subscribe;
|
||||
|
||||
single_item_command_request!(Subscribe, "subscribe", ChannelName);
|
||||
|
||||
empty_command_response!(Subscribe);
|
||||
|
||||
impl Command for Subscribe {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "subscribe";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let channel_name = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Subscribe(channel_name.to_string()), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = SubscribeRequest;
|
||||
type Response = SubscribeResponse;
|
||||
}
|
||||
|
||||
@@ -1,26 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::ChannelName,
|
||||
};
|
||||
|
||||
pub struct Unsubscribe;
|
||||
|
||||
single_item_command_request!(Unsubscribe, "unsubscribe", ChannelName);
|
||||
|
||||
empty_command_response!(Unsubscribe);
|
||||
|
||||
impl Command for Unsubscribe {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "unsubscribe";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let channel_name = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Unsubscribe(channel_name.to_string()), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = UnsubscribeRequest;
|
||||
type Response = UnsubscribeResponse;
|
||||
}
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
pub mod binary_limit;
|
||||
pub mod close;
|
||||
pub mod kill;
|
||||
pub mod password;
|
||||
pub mod ping;
|
||||
pub mod protocol;
|
||||
pub mod protocol_all;
|
||||
pub mod protocol_available;
|
||||
pub mod protocol_clear;
|
||||
pub mod protocol_disable;
|
||||
pub mod protocol_enable;
|
||||
pub mod tag_types;
|
||||
pub mod tag_types_all;
|
||||
pub mod tag_types_available;
|
||||
pub mod tag_types_clear;
|
||||
pub mod tag_types_disable;
|
||||
pub mod tag_types_enable;
|
||||
pub mod tag_types_reset;
|
||||
mod binary_limit;
|
||||
mod close;
|
||||
mod kill;
|
||||
mod password;
|
||||
mod ping;
|
||||
mod protocol;
|
||||
mod protocol_all;
|
||||
mod protocol_available;
|
||||
mod protocol_clear;
|
||||
mod protocol_disable;
|
||||
mod protocol_enable;
|
||||
mod tag_types;
|
||||
mod tag_types_all;
|
||||
mod tag_types_available;
|
||||
mod tag_types_clear;
|
||||
mod tag_types_disable;
|
||||
mod tag_types_enable;
|
||||
mod tag_types_reset;
|
||||
|
||||
pub use binary_limit::BinaryLimit;
|
||||
pub use close::Close;
|
||||
pub use kill::Kill;
|
||||
pub use password::Password;
|
||||
pub use ping::Ping;
|
||||
pub use protocol::Protocol;
|
||||
pub use protocol_all::ProtocolAll;
|
||||
pub use protocol_available::ProtocolAvailable;
|
||||
pub use protocol_clear::ProtocolClear;
|
||||
pub use protocol_disable::ProtocolDisable;
|
||||
pub use protocol_enable::ProtocolEnable;
|
||||
pub use tag_types::TagTypes;
|
||||
pub use tag_types_all::TagTypesAll;
|
||||
pub use tag_types_available::TagTypesAvailable;
|
||||
pub use tag_types_clear::TagTypesClear;
|
||||
pub use tag_types_disable::TagTypesDisable;
|
||||
pub use tag_types_enable::TagTypesEnable;
|
||||
pub use tag_types_reset::TagTypesReset;
|
||||
pub use binary_limit::*;
|
||||
pub use close::*;
|
||||
pub use kill::*;
|
||||
pub use password::*;
|
||||
pub use ping::*;
|
||||
pub use protocol::*;
|
||||
pub use protocol_all::*;
|
||||
pub use protocol_available::*;
|
||||
pub use protocol_clear::*;
|
||||
pub use protocol_disable::*;
|
||||
pub use protocol_enable::*;
|
||||
pub use tag_types::*;
|
||||
pub use tag_types_all::*;
|
||||
pub use tag_types_available::*;
|
||||
pub use tag_types_clear::*;
|
||||
pub use tag_types_disable::*;
|
||||
pub use tag_types_enable::*;
|
||||
pub use tag_types_reset::*;
|
||||
|
||||
@@ -1,27 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_response, single_item_command_request};
|
||||
|
||||
pub struct BinaryLimit;
|
||||
|
||||
single_item_command_request!(BinaryLimit, "binarylimit", u64);
|
||||
|
||||
empty_command_response!(BinaryLimit);
|
||||
|
||||
impl Command for BinaryLimit {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "binarylimit";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let limit = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
let limit = limit
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, limit.to_string()))?;
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::BinaryLimit(limit), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = BinaryLimitRequest;
|
||||
type Response = BinaryLimitResponse;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct Close;
|
||||
|
||||
empty_command_request!(Close, "close");
|
||||
|
||||
empty_command_response!(Close);
|
||||
|
||||
impl Command for Close {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "close";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Close, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = CloseRequest;
|
||||
type Response = CloseResponse;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct Kill;
|
||||
|
||||
empty_command_request!(Kill, "kill");
|
||||
|
||||
empty_command_response!(Kill);
|
||||
|
||||
impl Command for Kill {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "kill";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Kill, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = KillRequest;
|
||||
type Response = KillResponse;
|
||||
}
|
||||
|
||||
@@ -1,27 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_response, single_item_command_request};
|
||||
|
||||
pub struct Password;
|
||||
|
||||
single_item_command_request!(Password, "password", String);
|
||||
|
||||
empty_command_response!(Password);
|
||||
|
||||
impl Command for Password {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "password";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let password = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string();
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Password(password), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = PasswordRequest;
|
||||
type Response = PasswordResponse;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct Ping;
|
||||
|
||||
empty_command_request!(Ping, "ping");
|
||||
|
||||
empty_command_response!(Ping);
|
||||
|
||||
impl Command for Ping {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "ping";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Ping, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = PingRequest;
|
||||
type Response = PingResponse;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
commands::{Command, ResponseParserError, empty_command_request, multi_item_command_response},
|
||||
response_tokenizer::expect_property_type,
|
||||
};
|
||||
|
||||
pub struct Protocol;
|
||||
|
||||
empty_command_request!(Protocol, "protocol");
|
||||
|
||||
multi_item_command_response!(Protocol, "feature", String);
|
||||
|
||||
impl Command for Protocol {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "protocol";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Protocol, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
}
|
||||
type Request = ProtocolRequest;
|
||||
type Response = ProtocolResponse;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,12 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct ProtocolAll;
|
||||
|
||||
empty_command_request!(ProtocolAll, "protocol all");
|
||||
|
||||
empty_command_response!(ProtocolAll);
|
||||
|
||||
impl Command for ProtocolAll {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "protocol all";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::ProtocolAll, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = ProtocolAllRequest;
|
||||
type Response = ProtocolAllResponse;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
commands::{Command, ResponseParserError, empty_command_request, multi_item_command_response},
|
||||
response_tokenizer::expect_property_type,
|
||||
};
|
||||
|
||||
pub struct ProtocolAvailable;
|
||||
|
||||
empty_command_request!(ProtocolAvailable, "protocol available");
|
||||
|
||||
multi_item_command_response!(ProtocolAvailable, "feature", String);
|
||||
|
||||
impl Command for ProtocolAvailable {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "protocol available";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::ProtocolAvailable, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
}
|
||||
type Request = ProtocolAvailableRequest;
|
||||
type Response = ProtocolAvailableResponse;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,12 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct ProtocolClear;
|
||||
|
||||
empty_command_request!(ProtocolClear, "protocol clear");
|
||||
|
||||
empty_command_response!(ProtocolClear);
|
||||
|
||||
impl Command for ProtocolClear {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "protocol clear";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::ProtocolClear, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = ProtocolClearRequest;
|
||||
type Response = ProtocolClearResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,70 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{
|
||||
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::Feature,
|
||||
};
|
||||
|
||||
pub struct ProtocolDisable;
|
||||
|
||||
impl Command for ProtocolDisable {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "protocol disable";
|
||||
pub struct ProtocolDisableRequest(Vec<Feature>);
|
||||
|
||||
fn parse_request(parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl ProtocolDisableRequest {
|
||||
fn new(features: Vec<Feature>) -> Self {
|
||||
Self(features)
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRequest for ProtocolDisableRequest {
|
||||
const COMMAND: &'static str = "protocol disable";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = None;
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::ProtocolDisable(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::ProtocolDisable(features) => Some(ProtocolDisableRequest(features)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let features = self
|
||||
.0
|
||||
.iter()
|
||||
.map(|f| f.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(" ");
|
||||
format!("{} {}", Self::COMMAND, features)
|
||||
}
|
||||
|
||||
fn parse(parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let mut parts = parts.peekable();
|
||||
if parts.peek().is_none() {
|
||||
return Err(RequestParserError::UnexpectedEOF);
|
||||
return Err(Self::missing_arguments_error(0));
|
||||
}
|
||||
|
||||
// TODO: verify that the features are split by whitespace
|
||||
let mut features = Vec::new();
|
||||
for feature in parts {
|
||||
features.push(feature.to_string());
|
||||
let features = parts
|
||||
.enumerate()
|
||||
.map(|(i, f)| {
|
||||
f.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: i.try_into().unwrap_or(u32::MAX),
|
||||
expected_type: "Feature",
|
||||
raw_input: f.to_owned(),
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<Feature>, RequestParserError>>()?;
|
||||
|
||||
Ok(ProtocolDisableRequest(features))
|
||||
}
|
||||
}
|
||||
|
||||
Ok((Request::ProtocolDisable(features), ""))
|
||||
}
|
||||
empty_command_response!(ProtocolDisable);
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
impl Command for ProtocolDisable {
|
||||
type Request = ProtocolDisableRequest;
|
||||
type Response = ProtocolDisableResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,70 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{
|
||||
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::Feature,
|
||||
};
|
||||
|
||||
pub struct ProtocolEnable;
|
||||
|
||||
impl Command for ProtocolEnable {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "protocol enable";
|
||||
pub struct ProtocolEnableRequest(Vec<Feature>);
|
||||
|
||||
fn parse_request(parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl ProtocolEnableRequest {
|
||||
fn new(features: Vec<Feature>) -> Self {
|
||||
Self(features)
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRequest for ProtocolEnableRequest {
|
||||
const COMMAND: &'static str = "protocol enable";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = None;
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::ProtocolEnable(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::ProtocolEnable(features) => Some(ProtocolEnableRequest(features)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let features = self
|
||||
.0
|
||||
.iter()
|
||||
.map(|f| f.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(" ");
|
||||
format!("{} {}", Self::COMMAND, features)
|
||||
}
|
||||
|
||||
fn parse(parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let mut parts = parts.peekable();
|
||||
if parts.peek().is_none() {
|
||||
return Err(RequestParserError::UnexpectedEOF);
|
||||
return Err(Self::missing_arguments_error(0));
|
||||
}
|
||||
|
||||
// TODO: verify that the features are split by whitespace
|
||||
let mut features = Vec::new();
|
||||
for feature in parts {
|
||||
features.push(feature.to_string());
|
||||
let features = parts
|
||||
.enumerate()
|
||||
.map(|(i, f)| {
|
||||
f.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: i.try_into().unwrap_or(u32::MAX),
|
||||
expected_type: "Feature",
|
||||
raw_input: f.to_owned(),
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<Feature>, RequestParserError>>()?;
|
||||
|
||||
Ok(ProtocolEnableRequest(features))
|
||||
}
|
||||
}
|
||||
|
||||
Ok((Request::ProtocolEnable(features), ""))
|
||||
}
|
||||
empty_command_response!(ProtocolEnable);
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
impl Command for ProtocolEnable {
|
||||
type Request = ProtocolEnableRequest;
|
||||
type Response = ProtocolEnableResponse;
|
||||
}
|
||||
|
||||
@@ -1,42 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, ResponseParserError, empty_command_request, multi_item_command_response},
|
||||
response_tokenizer::expect_property_type,
|
||||
};
|
||||
|
||||
pub struct TagTypes;
|
||||
|
||||
pub type TagTypesResponse = Vec<String>;
|
||||
empty_command_request!(TagTypes, "tagtypes");
|
||||
|
||||
multi_item_command_response!(TagTypes, "tagtype", String);
|
||||
|
||||
impl Command for TagTypes {
|
||||
type Request = TagTypesRequest;
|
||||
type Response = TagTypesResponse;
|
||||
const COMMAND: &'static str = "tagtypes";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::TagTypes, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: Vec<_> = parts.into();
|
||||
|
||||
let mut tagtypes = Vec::with_capacity(parts.len());
|
||||
for (key, value) in parts.into_iter() {
|
||||
debug_assert_eq!(key, "tagtype");
|
||||
|
||||
let tagtype = match value {
|
||||
GenericResponseValue::Text(name) => name.to_string(),
|
||||
GenericResponseValue::Binary(_) => {
|
||||
return Err(ResponseParserError::UnexpectedPropertyType(
|
||||
"tagtype", "Binary",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
tagtypes.push(tagtype);
|
||||
}
|
||||
|
||||
Ok(tagtypes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,12 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct TagTypesAll;
|
||||
|
||||
empty_command_request!(TagTypesAll, "tagtypes all");
|
||||
|
||||
empty_command_response!(TagTypesAll);
|
||||
|
||||
impl Command for TagTypesAll {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "tagtypes all";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::TagTypesAll, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = TagTypesAllRequest;
|
||||
type Response = TagTypesAllResponse;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
commands::{Command, ResponseParserError, empty_command_request, multi_item_command_response},
|
||||
response_tokenizer::expect_property_type,
|
||||
};
|
||||
|
||||
pub struct TagTypesAvailable;
|
||||
|
||||
empty_command_request!(TagTypesAvailable, "tagtypes available");
|
||||
|
||||
multi_item_command_response!(TagTypesAvailable, "tagtype", String);
|
||||
|
||||
impl Command for TagTypesAvailable {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "tagtypes available";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::TagTypesAvailable, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
}
|
||||
type Request = TagTypesAvailableRequest;
|
||||
type Response = TagTypesAvailableResponse;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,12 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct TagTypesClear;
|
||||
|
||||
empty_command_request!(TagTypesClear, "tagtypes clear");
|
||||
|
||||
empty_command_response!(TagTypesClear);
|
||||
|
||||
impl Command for TagTypesClear {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "tagtypes clear";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::TagTypesClear, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = TagTypesClearRequest;
|
||||
type Response = TagTypesClearResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,74 @@
|
||||
use std::u32;
|
||||
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{
|
||||
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::TagName,
|
||||
};
|
||||
|
||||
pub struct TagTypesDisable;
|
||||
|
||||
impl Command for TagTypesDisable {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "tagtypes disable";
|
||||
pub struct TagTypesDisableRequest(Vec<TagName>);
|
||||
|
||||
fn parse_request(parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl TagTypesDisableRequest {
|
||||
fn new(tagnames: Vec<TagName>) -> Self {
|
||||
Self(tagnames)
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRequest for TagTypesDisableRequest {
|
||||
const COMMAND: &'static str = "tagtypes disable";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = None;
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::TagTypesDisable(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::TagTypesDisable(req) => Some(TagTypesDisableRequest(req)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
format!(
|
||||
"{} {}",
|
||||
Self::COMMAND,
|
||||
self.0
|
||||
.iter()
|
||||
.map(|tag| tag.as_str())
|
||||
.collect::<Vec<&str>>()
|
||||
.join(" ")
|
||||
)
|
||||
}
|
||||
|
||||
fn parse(parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let mut parts = parts.peekable();
|
||||
if parts.peek().is_none() {
|
||||
return Err(RequestParserError::UnexpectedEOF);
|
||||
return Err(Self::missing_arguments_error(0));
|
||||
}
|
||||
|
||||
// TODO: verify that the tag types are split by whitespace
|
||||
let mut tag_types = Vec::new();
|
||||
for tag_type in parts {
|
||||
tag_types.push(tag_type.to_string());
|
||||
let tag_types = parts
|
||||
.enumerate()
|
||||
.map(|(i, s)| {
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: i.try_into().unwrap_or(u32::MAX),
|
||||
expected_type: "TagName",
|
||||
raw_input: s.to_owned(),
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<TagName>, RequestParserError>>()?;
|
||||
|
||||
Ok(TagTypesDisableRequest(tag_types))
|
||||
}
|
||||
}
|
||||
|
||||
Ok((Request::TagTypesDisable(tag_types), ""))
|
||||
}
|
||||
empty_command_response!(TagTypesDisable);
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
impl Command for TagTypesDisable {
|
||||
type Request = TagTypesDisableRequest;
|
||||
type Response = TagTypesDisableResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,72 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{
|
||||
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::TagName,
|
||||
};
|
||||
|
||||
pub struct TagTypesEnable;
|
||||
|
||||
impl Command for TagTypesEnable {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "tagtypes enable";
|
||||
pub struct TagTypesEnableRequest(Vec<TagName>);
|
||||
|
||||
fn parse_request(parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl TagTypesEnableRequest {
|
||||
fn new(tagnames: Vec<TagName>) -> Self {
|
||||
Self(tagnames)
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRequest for TagTypesEnableRequest {
|
||||
const COMMAND: &'static str = "tagtypes enable";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = None;
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::TagTypesEnable(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::TagTypesEnable(req) => Some(TagTypesEnableRequest(req)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
format!(
|
||||
"{} {}",
|
||||
Self::COMMAND,
|
||||
self.0
|
||||
.iter()
|
||||
.map(|tag| tag.as_str())
|
||||
.collect::<Vec<&str>>()
|
||||
.join(" ")
|
||||
)
|
||||
}
|
||||
|
||||
fn parse(parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let mut parts = parts.peekable();
|
||||
if parts.peek().is_none() {
|
||||
return Err(RequestParserError::UnexpectedEOF);
|
||||
return Err(Self::missing_arguments_error(0));
|
||||
}
|
||||
|
||||
// TODO: verify that the tag types are split by whitespace
|
||||
let mut tag_types = Vec::new();
|
||||
for tag_type in parts {
|
||||
tag_types.push(tag_type.to_string());
|
||||
let tag_types = parts
|
||||
.enumerate()
|
||||
.map(|(i, s)| {
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: i.try_into().unwrap_or(u32::MAX),
|
||||
expected_type: "TagName",
|
||||
raw_input: s.to_owned(),
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<TagName>, RequestParserError>>()?;
|
||||
|
||||
Ok(TagTypesEnableRequest(tag_types))
|
||||
}
|
||||
}
|
||||
|
||||
Ok((Request::TagTypesEnable(tag_types), ""))
|
||||
}
|
||||
empty_command_response!(TagTypesEnable);
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
impl Command for TagTypesEnable {
|
||||
type Request = TagTypesEnableRequest;
|
||||
type Response = TagTypesEnableResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,72 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{
|
||||
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::TagName,
|
||||
};
|
||||
|
||||
pub struct TagTypesReset;
|
||||
|
||||
impl Command for TagTypesReset {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "tagtypes reset";
|
||||
pub struct TagTypesResetRequest(Vec<TagName>);
|
||||
|
||||
fn parse_request(parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl TagTypesResetRequest {
|
||||
fn new(tagnames: Vec<TagName>) -> Self {
|
||||
Self(tagnames)
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRequest for TagTypesResetRequest {
|
||||
const COMMAND: &'static str = "tagtypes reset";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = None;
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::TagTypesReset(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::TagTypesReset(req) => Some(TagTypesResetRequest(req)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
format!(
|
||||
"{} {}",
|
||||
Self::COMMAND,
|
||||
self.0
|
||||
.iter()
|
||||
.map(|tag| tag.as_str())
|
||||
.collect::<Vec<&str>>()
|
||||
.join(" ")
|
||||
)
|
||||
}
|
||||
|
||||
fn parse(parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let mut parts = parts.peekable();
|
||||
if parts.peek().is_none() {
|
||||
return Err(RequestParserError::UnexpectedEOF);
|
||||
return Err(Self::missing_arguments_error(0));
|
||||
}
|
||||
|
||||
// TODO: verify that the tag types are split by whitespace
|
||||
let mut tag_types = Vec::new();
|
||||
for tag_type in parts {
|
||||
tag_types.push(tag_type.to_string());
|
||||
let tag_types = parts
|
||||
.enumerate()
|
||||
.map(|(i, s)| {
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: i.try_into().unwrap_or(u32::MAX),
|
||||
expected_type: "TagName",
|
||||
raw_input: s.to_owned(),
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<TagName>, RequestParserError>>()?;
|
||||
|
||||
Ok(TagTypesResetRequest(tag_types))
|
||||
}
|
||||
}
|
||||
|
||||
Ok((Request::TagTypesReset(tag_types), ""))
|
||||
}
|
||||
empty_command_response!(TagTypesReset);
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
impl Command for TagTypesReset {
|
||||
type Request = TagTypesResetRequest;
|
||||
type Response = TagTypesResetResponse;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
pub mod next;
|
||||
pub mod pause;
|
||||
pub mod play;
|
||||
pub mod playid;
|
||||
pub mod previous;
|
||||
pub mod seek;
|
||||
pub mod seekcur;
|
||||
pub mod seekid;
|
||||
pub mod stop;
|
||||
mod next;
|
||||
mod pause;
|
||||
mod play;
|
||||
mod playid;
|
||||
mod previous;
|
||||
mod seek;
|
||||
mod seekcur;
|
||||
mod seekid;
|
||||
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;
|
||||
pub use next::*;
|
||||
pub use pause::*;
|
||||
pub use play::*;
|
||||
pub use playid::*;
|
||||
pub use previous::*;
|
||||
pub use seek::*;
|
||||
pub use seekcur::*;
|
||||
pub use seekid::*;
|
||||
pub use stop::*;
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct Next;
|
||||
|
||||
empty_command_request!(Next, "next");
|
||||
|
||||
empty_command_response!(Next);
|
||||
|
||||
impl Command for Next {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "next";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Next, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = NextRequest;
|
||||
type Response = NextResponse;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,63 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
};
|
||||
|
||||
pub struct Pause;
|
||||
|
||||
impl Command for Pause {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "pause";
|
||||
pub struct PauseRequest(Option<bool>);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl PauseRequest {
|
||||
fn new(toggle: Option<bool>) -> Self {
|
||||
Self(toggle)
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandRequest for PauseRequest {
|
||||
const COMMAND: &'static str = "pause";
|
||||
const MIN_ARGS: u32 = 0;
|
||||
const MAX_ARGS: Option<u32> = Some(1);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Pause(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Pause(value) => Some(PauseRequest(value)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
match self.0 {
|
||||
Some(true) => format!("{} 1\n", Self::COMMAND),
|
||||
Some(false) => format!("{} 0\n", Self::COMMAND),
|
||||
None => Self::COMMAND.to_string() + "\n",
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let result = match parts.next() {
|
||||
Some("0") => Ok((Request::Pause(Some(false)), "")),
|
||||
Some("1") => Ok((Request::Pause(Some(true)), "")),
|
||||
Some(s) => Err(RequestParserError::SyntaxError(0, s.to_string())),
|
||||
None => Ok((Request::Pause(None), "")),
|
||||
Some("0") => Ok(Some(false)),
|
||||
Some("1") => Ok(Some(true)),
|
||||
Some(s) => Err(RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "Option<bool>",
|
||||
raw_input: s.to_owned(),
|
||||
}),
|
||||
None => Ok(None),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
result
|
||||
result.map(PauseRequest)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(Pause);
|
||||
|
||||
impl Command for Pause {
|
||||
type Request = PauseRequest;
|
||||
type Response = PauseResponse;
|
||||
}
|
||||
|
||||
@@ -1,34 +1,15 @@
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::SongPosition,
|
||||
commands::{Command, empty_command_response, single_optional_item_command_request},
|
||||
types::SongPosition,
|
||||
};
|
||||
|
||||
pub struct Play;
|
||||
|
||||
single_optional_item_command_request!(Play, "play", SongPosition);
|
||||
|
||||
empty_command_response!(Play);
|
||||
|
||||
impl Command for Play {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "play";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let songpos = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<SongPosition>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Play(songpos), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = PlayRequest;
|
||||
type Response = PlayResponse;
|
||||
}
|
||||
|
||||
@@ -1,34 +1,15 @@
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::SongId,
|
||||
commands::{Command, empty_command_response, single_optional_item_command_request},
|
||||
types::SongId,
|
||||
};
|
||||
|
||||
pub struct PlayId;
|
||||
|
||||
single_optional_item_command_request!(PlayId, "playid", SongId);
|
||||
|
||||
empty_command_response!(PlayId);
|
||||
|
||||
impl Command for PlayId {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "playid";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let songid = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<SongId>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::PlayId(songid), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = PlayIdRequest;
|
||||
type Response = PlayIdResponse;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct Previous;
|
||||
|
||||
empty_command_request!(Previous, "previous");
|
||||
|
||||
empty_command_response!(Previous);
|
||||
|
||||
impl Command for Previous {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "previous";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Previous, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = PreviousRequest;
|
||||
type Response = PreviousResponse;
|
||||
}
|
||||
|
||||
@@ -1,41 +1,73 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::{SongPosition, TimeWithFractions},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::{SongPosition, TimeWithFractions},
|
||||
};
|
||||
|
||||
pub struct Seek;
|
||||
|
||||
impl Command for Seek {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "seek";
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SeekRequest {
|
||||
pub songpos: SongPosition,
|
||||
pub time: TimeWithFractions,
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl CommandRequest for SeekRequest {
|
||||
const COMMAND: &'static str = "seek";
|
||||
const MIN_ARGS: u32 = 2;
|
||||
const MAX_ARGS: Option<u32> = Some(2);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Seek(self.songpos, self.time)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Seek(songpos, time) => Some(SeekRequest { songpos, time }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
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::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
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::SyntaxError(0, t.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
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)),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::Seek(songpos, time), ""))
|
||||
Ok(SeekRequest { songpos, time })
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(Seek);
|
||||
|
||||
impl Command for Seek {
|
||||
type Request = SeekRequest;
|
||||
type Response = SeekResponse;
|
||||
}
|
||||
|
||||
@@ -1,53 +1,96 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::{SeekMode, TimeWithFractions},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::{SeekMode, TimeWithFractions},
|
||||
};
|
||||
|
||||
pub struct SeekCur;
|
||||
|
||||
impl Command for SeekCur {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "seekcur";
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SeekCurRequest {
|
||||
pub mode: SeekMode,
|
||||
pub time: TimeWithFractions,
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl CommandRequest for SeekCurRequest {
|
||||
const COMMAND: &'static str = "seekcur";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(1);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::SeekCur(self.mode, self.time)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::SeekCur(mode, time) => Some(SeekCurRequest { mode, time }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let time_str = match self.mode {
|
||||
SeekMode::Absolute => format!("{}", self.time),
|
||||
SeekMode::Relative => format!("+{}", self.time),
|
||||
SeekMode::RelativeReverse => format!("-{}", self.time),
|
||||
};
|
||||
|
||||
format!("{} {}\n", Self::COMMAND, time_str)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let time_raw = match parts.next() {
|
||||
Some(t) => t,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
// TODO: DRY
|
||||
let (mode, time) = match time_raw {
|
||||
t if t.starts_with('+') => (
|
||||
SeekMode::Relative,
|
||||
t[1..]
|
||||
.parse::<TimeWithFractions>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, t.to_owned()))?,
|
||||
t[1..].parse::<TimeWithFractions>().map_err(|_| {
|
||||
RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "TimeWithFractions",
|
||||
raw_input: t[1..].to_owned(),
|
||||
}
|
||||
})?,
|
||||
),
|
||||
t if t.starts_with('-') => (
|
||||
SeekMode::Relative,
|
||||
-t[1..]
|
||||
.parse::<TimeWithFractions>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, t.to_owned()))?,
|
||||
SeekMode::RelativeReverse,
|
||||
t[1..].parse::<TimeWithFractions>().map_err(|_| {
|
||||
RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "TimeWithFractions",
|
||||
raw_input: t[1..].to_owned(),
|
||||
}
|
||||
})?,
|
||||
),
|
||||
t => (
|
||||
SeekMode::Absolute,
|
||||
t.parse::<TimeWithFractions>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, t.to_owned()))?,
|
||||
t.parse::<TimeWithFractions>().map_err(|_| {
|
||||
RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "TimeWithFractions",
|
||||
raw_input: t.to_owned(),
|
||||
}
|
||||
})?,
|
||||
),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
debug_assert!(time >= 0.0);
|
||||
|
||||
Ok((Request::SeekCur(mode, time), ""))
|
||||
Ok(SeekCurRequest { mode, time })
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(SeekCur);
|
||||
|
||||
impl Command for SeekCur {
|
||||
type Request = SeekCurRequest;
|
||||
type Response = SeekCurResponse;
|
||||
}
|
||||
|
||||
@@ -1,41 +1,71 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::{SongId, TimeWithFractions},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::{SongId, TimeWithFractions},
|
||||
};
|
||||
|
||||
pub struct SeekId;
|
||||
|
||||
impl Command for SeekId {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "seekid";
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SeekIdRequest {
|
||||
pub songid: SongId,
|
||||
pub time: TimeWithFractions,
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl CommandRequest for SeekIdRequest {
|
||||
const COMMAND: &'static str = "seekid";
|
||||
const MIN_ARGS: u32 = 2;
|
||||
const MAX_ARGS: Option<u32> = Some(2);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::SeekId(self.songid, self.time)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::SeekId(songid, time) => Some(SeekIdRequest { songid, time }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
format!("{} {} {}\n", Self::COMMAND, self.songid, self.time)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let songid = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<SongId>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "SongId",
|
||||
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::SyntaxError(0, t.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
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)),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::SeekId(songid, time), ""))
|
||||
Ok(SeekIdRequest { songid, time })
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(SeekId);
|
||||
|
||||
impl Command for SeekId {
|
||||
type Request = SeekIdRequest;
|
||||
type Response = SeekIdResponse;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
pub struct Stop;
|
||||
|
||||
empty_command_request!(Stop, "stop");
|
||||
|
||||
empty_command_response!(Stop);
|
||||
|
||||
impl Command for Stop {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "stop";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::Stop, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = StopRequest;
|
||||
type Response = StopResponse;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
pub mod listmounts;
|
||||
pub mod listneighbors;
|
||||
pub mod mount;
|
||||
pub mod unmount;
|
||||
mod listmounts;
|
||||
mod listneighbors;
|
||||
mod mount;
|
||||
mod unmount;
|
||||
|
||||
pub use listmounts::ListMounts;
|
||||
pub use listneighbors::ListNeighbors;
|
||||
pub use mount::Mount;
|
||||
pub use unmount::Unmount;
|
||||
pub use listmounts::*;
|
||||
pub use listneighbors::*;
|
||||
pub use mount::*;
|
||||
pub use unmount::*;
|
||||
|
||||
@@ -1,22 +1,39 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
commands::{Command, ResponseParserError, empty_command_request, multi_item_command_response},
|
||||
response_tokenizer::expect_property_type,
|
||||
};
|
||||
|
||||
pub struct ListMounts;
|
||||
|
||||
empty_command_request!(ListMounts, "listmounts");
|
||||
|
||||
multi_item_command_response!(ListMounts, "mount", String);
|
||||
|
||||
impl Command for ListMounts {
|
||||
type Response = Vec<(String, String)>;
|
||||
const COMMAND: &'static str = "listmounts";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::ListMounts, ""))
|
||||
type Request = ListMountsRequest;
|
||||
type Response = ListMountsResponse;
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use indoc::indoc;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_response() {
|
||||
let input = indoc! {"
|
||||
mount:
|
||||
mount: /mnt/music
|
||||
OK
|
||||
"};
|
||||
let result = ListMounts::parse_raw_response(input.as_bytes());
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok(ListMountsResponse(vec![
|
||||
"".to_string(),
|
||||
"/mnt/music".to_string(),
|
||||
]))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,52 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{Command, RequestParserResult, ResponseAttributes, ResponseParserError},
|
||||
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
|
||||
response_tokenizer::{ResponseAttributes, expect_property_type},
|
||||
};
|
||||
|
||||
pub struct ListNeighbors;
|
||||
|
||||
empty_command_request!(ListNeighbors, "listneighbors");
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ListNeighborsResponse(HashMap<String, String>);
|
||||
|
||||
impl CommandResponse for ListNeighborsResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: Vec<_> = parts.into_vec()?;
|
||||
debug_assert!(parts.len() % 2 == 0);
|
||||
|
||||
let mut result = HashMap::with_capacity(parts.len() / 2);
|
||||
|
||||
for channel_message_pair in parts.chunks_exact(2) {
|
||||
let (neigh_key, neigh_value) = channel_message_pair[0];
|
||||
let (name_key, name_value) = channel_message_pair[1];
|
||||
|
||||
debug_assert!(neigh_key == "neighbor");
|
||||
debug_assert!(name_key == "name");
|
||||
|
||||
let neighbor = expect_property_type!(Some(neigh_value), "neighbor", Text).to_string();
|
||||
let name = expect_property_type!(Some(name_value), "name", Text).to_string();
|
||||
|
||||
result.insert(neighbor, name);
|
||||
}
|
||||
|
||||
Ok(ListNeighborsResponse(result))
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for ListNeighbors {
|
||||
type Response = Vec<(String, String)>;
|
||||
const COMMAND: &'static str = "listneighbors";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::ListNeighbors, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
}
|
||||
type Request = ListNeighborsRequest;
|
||||
type Response = ListNeighborsResponse;
|
||||
}
|
||||
|
||||
@@ -1,36 +1,69 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{
|
||||
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
},
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::{MountPath, Uri},
|
||||
};
|
||||
|
||||
pub struct Mount;
|
||||
|
||||
impl Command for Mount {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "mount";
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct MountRequest {
|
||||
pub path: MountPath,
|
||||
pub uri: Uri,
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let path = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string();
|
||||
impl CommandRequest for MountRequest {
|
||||
const COMMAND: &'static str = "mount";
|
||||
const MIN_ARGS: u32 = 2;
|
||||
const MAX_ARGS: Option<u32> = Some(2);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Mount(self.path, self.uri)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Mount(path, uri) => Some(MountRequest { path, uri }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
debug_assert!(self.path.to_str().is_some());
|
||||
format!(
|
||||
"{} {} {}\n",
|
||||
Self::COMMAND,
|
||||
self.path.to_str().unwrap_or("<invalid path>"),
|
||||
self.uri
|
||||
)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let path = parts.next().ok_or(Self::missing_arguments_error(0))?;
|
||||
let path = path
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "MountPath",
|
||||
raw_input: path.to_string(),
|
||||
})?;
|
||||
|
||||
let uri = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.ok_or(Self::missing_arguments_error(1))?
|
||||
.to_string();
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::Mount(path, uri), ""))
|
||||
Ok(MountRequest { path, uri })
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(Mount);
|
||||
|
||||
impl Command for Mount {
|
||||
type Request = MountRequest;
|
||||
type Response = MountResponse;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,59 @@
|
||||
use crate::{
|
||||
Request,
|
||||
commands::{
|
||||
Command, RequestParserError, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
Command, CommandRequest, RequestParserError, RequestTokenizer, empty_command_response,
|
||||
},
|
||||
types::MountPath,
|
||||
};
|
||||
|
||||
pub struct Unmount;
|
||||
|
||||
impl Command for Unmount {
|
||||
type Response = ();
|
||||
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct UnmountRequest(MountPath);
|
||||
|
||||
impl CommandRequest for UnmountRequest {
|
||||
const COMMAND: &'static str = "unmount";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(1);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let path = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string();
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Unmount(path), ""))
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Unmount(self.0)
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Unmount(item) => Some(UnmountRequest(item)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
debug_assert!(self.0.to_str().is_some());
|
||||
format!(
|
||||
"{} {}\n",
|
||||
Self::COMMAND,
|
||||
self.0.to_str().unwrap_or("<invalid path>")
|
||||
)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let path = parts.next().ok_or(Self::missing_arguments_error(0))?;
|
||||
let path =
|
||||
path.parse::<MountPath>()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "MountPath",
|
||||
raw_input: path.to_string(),
|
||||
})?;
|
||||
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok(UnmountRequest(path))
|
||||
}
|
||||
}
|
||||
|
||||
empty_command_response!(Unmount);
|
||||
|
||||
impl Command for Unmount {
|
||||
type Request = UnmountRequest;
|
||||
type Response = UnmountResponse;
|
||||
}
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
pub mod albumart;
|
||||
pub mod count;
|
||||
pub mod find;
|
||||
pub mod findadd;
|
||||
pub mod getfingerprint;
|
||||
pub mod list;
|
||||
pub mod listall;
|
||||
pub mod listallinfo;
|
||||
pub mod listfiles;
|
||||
pub mod lsinfo;
|
||||
pub mod readcomments;
|
||||
pub mod readpicture;
|
||||
pub mod rescan;
|
||||
pub mod search;
|
||||
pub mod searchadd;
|
||||
pub mod searchaddpl;
|
||||
pub mod searchcount;
|
||||
pub mod update;
|
||||
mod albumart;
|
||||
mod count;
|
||||
mod find;
|
||||
mod findadd;
|
||||
mod getfingerprint;
|
||||
mod list;
|
||||
mod listall;
|
||||
mod listallinfo;
|
||||
mod listfiles;
|
||||
mod lsinfo;
|
||||
mod readcomments;
|
||||
mod readpicture;
|
||||
mod rescan;
|
||||
mod search;
|
||||
mod searchadd;
|
||||
mod searchaddpl;
|
||||
mod searchcount;
|
||||
mod update;
|
||||
|
||||
pub use albumart::AlbumArt;
|
||||
pub use count::Count;
|
||||
pub use find::Find;
|
||||
pub use findadd::FindAdd;
|
||||
pub use getfingerprint::GetFingerprint;
|
||||
pub use list::List;
|
||||
pub use listall::ListAll;
|
||||
pub use listallinfo::ListAllInfo;
|
||||
pub use listfiles::ListFiles;
|
||||
pub use lsinfo::LsInfo;
|
||||
pub use readcomments::ReadComments;
|
||||
pub use readpicture::ReadPicture;
|
||||
pub use rescan::Rescan;
|
||||
pub use search::Search;
|
||||
pub use searchadd::SearchAdd;
|
||||
pub use searchaddpl::SearchAddPl;
|
||||
pub use searchcount::SearchCount;
|
||||
pub use update::Update;
|
||||
pub use albumart::*;
|
||||
pub use count::*;
|
||||
pub use find::*;
|
||||
pub use findadd::*;
|
||||
pub use getfingerprint::*;
|
||||
pub use list::*;
|
||||
pub use listall::*;
|
||||
pub use listallinfo::*;
|
||||
pub use listfiles::*;
|
||||
pub use lsinfo::*;
|
||||
pub use readcomments::*;
|
||||
pub use readpicture::*;
|
||||
pub use rescan::*;
|
||||
pub use search::*;
|
||||
pub use searchadd::*;
|
||||
pub use searchaddpl::*;
|
||||
pub use searchcount::*;
|
||||
pub use update::*;
|
||||
|
||||
@@ -1,46 +1,85 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError, get_and_parse_property, get_property,
|
||||
},
|
||||
common::Offset,
|
||||
commands::{Command, CommandRequest, CommandResponse, RequestParserError, ResponseParserError},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
response_tokenizer::{ResponseAttributes, get_and_parse_property, get_property},
|
||||
types::{Offset, Uri},
|
||||
};
|
||||
|
||||
pub struct AlbumArt;
|
||||
|
||||
pub struct AlbumArtResponse {
|
||||
pub size: usize,
|
||||
pub binary: Vec<u8>,
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct AlbumArtRequest {
|
||||
uri: Uri,
|
||||
offset: Offset,
|
||||
}
|
||||
|
||||
impl Command for AlbumArt {
|
||||
type Response = AlbumArtResponse;
|
||||
impl CommandRequest for AlbumArtRequest {
|
||||
const COMMAND: &'static str = "albumart";
|
||||
const MIN_ARGS: u32 = 2;
|
||||
const MAX_ARGS: Option<u32> = Some(2);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::AlbumArt(self.uri, self.offset)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::AlbumArt(uri, offset) => Some(AlbumArtRequest { uri, offset }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
format!("{} {} {}\n", Self::COMMAND, self.uri, self.offset)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let uri = match parts.next() {
|
||||
Some(s) => s,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
let offset = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<Offset>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 1,
|
||||
expected_type: "Offset",
|
||||
raw_input: s.to_string(),
|
||||
})?,
|
||||
None => return Err(Self::missing_arguments_error(1)),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::AlbumArt(uri.to_string(), offset), ""))
|
||||
Ok(AlbumArtRequest {
|
||||
uri: uri.to_string(),
|
||||
offset,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct AlbumArtResponse {
|
||||
pub size: usize,
|
||||
pub binary: Vec<u8>,
|
||||
}
|
||||
|
||||
impl CommandResponse for AlbumArtResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let size = get_and_parse_property!(parts, "size", Text);
|
||||
|
||||
@@ -49,3 +88,8 @@ impl Command for AlbumArt {
|
||||
Ok(AlbumArtResponse { size, binary })
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for AlbumArt {
|
||||
type Request = AlbumArtRequest;
|
||||
type Response = AlbumArtResponse;
|
||||
}
|
||||
|
||||
@@ -1,48 +1,100 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError, get_and_parse_property,
|
||||
},
|
||||
filter::parse_filter,
|
||||
commands::{Command, CommandRequest, CommandResponse, RequestParserError, ResponseParserError},
|
||||
filter::Filter,
|
||||
request_tokenizer::RequestTokenizer,
|
||||
response_tokenizer::{ResponseAttributes, get_and_parse_property},
|
||||
types::GroupType,
|
||||
};
|
||||
|
||||
pub struct Count;
|
||||
|
||||
pub struct CountResponse {
|
||||
pub songs: usize,
|
||||
pub playtime: u64,
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct CountRequest {
|
||||
filter: Filter,
|
||||
group: Option<GroupType>,
|
||||
}
|
||||
|
||||
impl Command for Count {
|
||||
type Response = CountResponse;
|
||||
impl CommandRequest for CountRequest {
|
||||
const COMMAND: &'static str = "count";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(2);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let filter = parse_filter(&mut parts)?;
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Count(self.filter, self.group)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Count(filter, group) => Some(CountRequest { filter, group }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let mut cmd = format!("{} {}", Self::COMMAND, self.filter);
|
||||
if let Some(group) = self.group.as_ref() {
|
||||
cmd.push_str(&format!(" group {}", group));
|
||||
}
|
||||
cmd.push('\n');
|
||||
cmd
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let filter = match parts.next() {
|
||||
Some(f) => {
|
||||
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
let group = if let Some("group") = parts.next() {
|
||||
let group = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
let group = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
argument_index: 1,
|
||||
keyword: "group",
|
||||
})?;
|
||||
|
||||
Some(
|
||||
group
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, group.to_owned()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 1,
|
||||
expected_type: "GroupType",
|
||||
raw_input: group.to_owned(),
|
||||
})?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::Count(filter, group), ""))
|
||||
Ok(CountRequest { filter, group })
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct CountResponse {
|
||||
pub songs: usize,
|
||||
pub playtime: u64,
|
||||
}
|
||||
|
||||
impl CommandResponse for CountResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let songs = get_and_parse_property!(parts, "songs", Text);
|
||||
let playtime = get_and_parse_property!(parts, "playtime", Text);
|
||||
@@ -50,3 +102,8 @@ impl Command for Count {
|
||||
Ok(CountResponse { songs, playtime })
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Count {
|
||||
type Request = CountRequest;
|
||||
type Response = CountResponse;
|
||||
}
|
||||
|
||||
@@ -1,51 +1,143 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
filter::parse_filter,
|
||||
commands::{Command, CommandRequest, CommandResponse, RequestParserError, ResponseParserError},
|
||||
filter::Filter,
|
||||
request_tokenizer::RequestTokenizer,
|
||||
response_tokenizer::ResponseAttributes,
|
||||
types::{DbSelectionPrintResponse, DbSongInfo, Sort, WindowRange},
|
||||
};
|
||||
|
||||
pub struct Find;
|
||||
|
||||
pub struct FindResponse {}
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct FindRequest {
|
||||
filter: Filter,
|
||||
sort: Option<Sort>,
|
||||
window: Option<WindowRange>,
|
||||
}
|
||||
|
||||
impl Command for Find {
|
||||
type Response = FindResponse;
|
||||
impl CommandRequest for FindRequest {
|
||||
const COMMAND: &'static str = "find";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(3);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let filter = parse_filter(&mut parts)?;
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Find(self.filter, self.sort, self.window)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Find(filter, sort, window) => Some(FindRequest {
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let mut cmd = format!("{} {}", Self::COMMAND, self.filter);
|
||||
if let Some(sort) = &self.sort {
|
||||
cmd.push_str(&format!(" sort {}", sort));
|
||||
}
|
||||
if let Some(window) = &self.window {
|
||||
cmd.push_str(&format!(" window {}", window));
|
||||
}
|
||||
cmd.push('\n');
|
||||
cmd
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let filter = match parts.next() {
|
||||
Some(f) => {
|
||||
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
let mut argument_index_counter = 0;
|
||||
let mut sort_or_window = parts.next();
|
||||
let mut sort = None;
|
||||
if let Some("sort") = sort_or_window {
|
||||
sort = Some(
|
||||
parts
|
||||
argument_index_counter += 1;
|
||||
let s = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string(),
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "sort",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
sort = Some(
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "Sort",
|
||||
raw_input: s.to_string(),
|
||||
})?,
|
||||
);
|
||||
sort_or_window = parts.next();
|
||||
}
|
||||
|
||||
let mut window = None;
|
||||
if let Some("window") = sort_or_window {
|
||||
let w = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
argument_index_counter += 1;
|
||||
let w = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "window",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
window = Some(
|
||||
w.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, w.to_string()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "WindowRange",
|
||||
raw_input: w.to_string(),
|
||||
})?,
|
||||
);
|
||||
}
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::Find(filter, sort, window), ""))
|
||||
Ok(FindRequest {
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct FindResponse(Vec<DbSongInfo>);
|
||||
|
||||
impl CommandResponse for FindResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
DbSelectionPrintResponse::parse(parts)?
|
||||
.into_iter()
|
||||
.map(|i| match i {
|
||||
DbSelectionPrintResponse::Song(db_song_info) => Ok(db_song_info),
|
||||
DbSelectionPrintResponse::Directory(_db_directory_info) => Err(
|
||||
ResponseParserError::UnexpectedProperty("directory".to_string()),
|
||||
),
|
||||
DbSelectionPrintResponse::Playlist(_db_playlist_info) => Err(
|
||||
ResponseParserError::UnexpectedProperty("playlist".to_string()),
|
||||
),
|
||||
})
|
||||
.collect::<Result<Vec<DbSongInfo>, ResponseParserError>>()
|
||||
.map(FindResponse)
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Find {
|
||||
type Request = FindRequest;
|
||||
type Response = FindResponse;
|
||||
}
|
||||
|
||||
@@ -1,60 +1,141 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
filter::parse_filter,
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
filter::Filter,
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::{SongPosition, Sort, WindowRange},
|
||||
};
|
||||
|
||||
pub struct FindAdd;
|
||||
|
||||
impl Command for FindAdd {
|
||||
type Response = ();
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct FindAddRequest {
|
||||
filter: Filter,
|
||||
sort: Option<Sort>,
|
||||
window: Option<WindowRange>,
|
||||
position: Option<SongPosition>,
|
||||
}
|
||||
|
||||
impl CommandRequest for FindAddRequest {
|
||||
const COMMAND: &'static str = "findadd";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(4);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let filter = parse_filter(&mut parts)?;
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::FindAdd(self.filter, self.sort, self.window, self.position)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::FindAdd(filter, sort, window, position) => Some(FindAddRequest {
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
position,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let mut cmd = format!("{} {}", Self::COMMAND, self.filter);
|
||||
if let Some(sort) = &self.sort {
|
||||
cmd.push_str(&format!(" sort {}", sort));
|
||||
}
|
||||
if let Some(window) = &self.window {
|
||||
cmd.push_str(&format!(" window {}", window));
|
||||
}
|
||||
if let Some(position) = &self.position {
|
||||
cmd.push_str(&format!(" position {}", position));
|
||||
}
|
||||
cmd.push('\n');
|
||||
cmd
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let filter = match parts.next() {
|
||||
Some(f) => {
|
||||
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
let mut argument_index_counter = 0;
|
||||
let mut sort_or_window_or_position = parts.next();
|
||||
let mut sort = None;
|
||||
if let Some("sort") = sort_or_window_or_position {
|
||||
sort = Some(
|
||||
parts
|
||||
argument_index_counter += 1;
|
||||
let s = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string(),
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "sort",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
sort = Some(
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "Sort",
|
||||
raw_input: s.to_string(),
|
||||
})?,
|
||||
);
|
||||
sort_or_window_or_position = parts.next();
|
||||
}
|
||||
|
||||
let mut window = None;
|
||||
if let Some("window") = sort_or_window_or_position {
|
||||
let w = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
argument_index_counter += 1;
|
||||
let w = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "window",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
window = Some(
|
||||
w.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, w.to_string()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "WindowRange",
|
||||
raw_input: w.to_string(),
|
||||
})?,
|
||||
);
|
||||
sort_or_window_or_position = parts.next();
|
||||
}
|
||||
|
||||
let mut position = None;
|
||||
if let Some("position") = sort_or_window_or_position {
|
||||
let p = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
argument_index_counter += 1;
|
||||
let p = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "position",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
position = Some(
|
||||
p.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, p.to_string()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "SongPosition",
|
||||
raw_input: p.to_string(),
|
||||
})?,
|
||||
);
|
||||
}
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::FindAdd(filter, sort, window, position), ""))
|
||||
Ok(FindAddRequest {
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
position,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(FindAdd);
|
||||
|
||||
impl Command for FindAdd {
|
||||
type Request = FindAddRequest;
|
||||
type Response = FindAddResponse;
|
||||
}
|
||||
|
||||
@@ -1,38 +1,41 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError, get_and_parse_property,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{Command, CommandResponse, ResponseParserError, single_item_command_request},
|
||||
response_tokenizer::{ResponseAttributes, get_and_parse_property},
|
||||
types::Uri,
|
||||
};
|
||||
|
||||
pub struct GetFingerprint;
|
||||
|
||||
single_item_command_request!(GetFingerprint, "getfingerprint", Uri);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct GetFingerprintResponse {
|
||||
pub chromaprint: String,
|
||||
}
|
||||
|
||||
impl Command for GetFingerprint {
|
||||
type Response = GetFingerprintResponse;
|
||||
const COMMAND: &'static str = "getfingerprint";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
let uri = uri
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, uri.to_owned()))?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::GetFingerprint(uri), ""))
|
||||
impl CommandResponse for GetFingerprintResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let chromaprint = get_and_parse_property!(parts, "chromaprint", Text);
|
||||
|
||||
Ok(GetFingerprintResponse { chromaprint })
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for GetFingerprint {
|
||||
type Request = GetFingerprintRequest;
|
||||
type Response = GetFingerprintResponse;
|
||||
}
|
||||
|
||||
@@ -1,58 +1,169 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError, expect_property_type,
|
||||
},
|
||||
filter::parse_filter,
|
||||
commands::{Command, CommandRequest, CommandResponse, RequestParserError, ResponseParserError},
|
||||
filter::Filter,
|
||||
request_tokenizer::RequestTokenizer,
|
||||
response_tokenizer::{ResponseAttributes, expect_property_type},
|
||||
types::{GroupType, TagName, WindowRange},
|
||||
};
|
||||
|
||||
pub struct List;
|
||||
|
||||
pub type ListResponse = Vec<String>;
|
||||
|
||||
impl Command for List {
|
||||
type Response = ListResponse;
|
||||
const COMMAND: &'static str = "list";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let tagtype = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
let tagtype = tagtype
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, tagtype.to_owned()))?;
|
||||
|
||||
// TODO: This should be optional
|
||||
let filter = parse_filter(&mut parts)?;
|
||||
|
||||
let group = if let Some("group") = parts.next() {
|
||||
let group = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
Some(
|
||||
group
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, group.to_owned()))?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::List(tagtype, filter, group), ""))
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ListRequest {
|
||||
tagname: TagName,
|
||||
filter: Option<Filter>,
|
||||
groups: Vec<GroupType>,
|
||||
window: Option<WindowRange>,
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
impl CommandRequest for ListRequest {
|
||||
const COMMAND: &'static str = "list";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = None;
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::List(self.tagname, self.filter, self.groups, self.window)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::List(tagname, filter, groups, window) => Some(ListRequest {
|
||||
tagname,
|
||||
filter,
|
||||
groups,
|
||||
window,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let mut cmd = match &self.filter {
|
||||
Some(f) => format!("{} {} {}", Self::COMMAND, self.tagname, f),
|
||||
None => format!("{} {}", Self::COMMAND, self.tagname),
|
||||
};
|
||||
for group in &self.groups {
|
||||
cmd.push_str(&format!(" group {}", group));
|
||||
}
|
||||
if let Some(window) = &self.window {
|
||||
cmd.push_str(&format!(" window {}", window));
|
||||
}
|
||||
cmd.push('\n');
|
||||
cmd
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let tagname = parts.next().ok_or(Self::missing_arguments_error(0))?;
|
||||
let tagname = tagname
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "TagName",
|
||||
raw_input: tagname.to_owned(),
|
||||
})?;
|
||||
|
||||
let mut filter = None;
|
||||
let mut groups = Vec::new();
|
||||
let mut window = None;
|
||||
|
||||
let mut next = parts.next();
|
||||
|
||||
let mut argument_index_counter = 0;
|
||||
if let Some(f) = next
|
||||
&& f != "group"
|
||||
&& f != "window"
|
||||
{
|
||||
argument_index_counter += 1;
|
||||
let parsed_filter =
|
||||
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?;
|
||||
filter = Some(parsed_filter);
|
||||
next = parts.next();
|
||||
}
|
||||
|
||||
while let Some(g) = next
|
||||
&& g == "group"
|
||||
{
|
||||
argument_index_counter += 1;
|
||||
let group = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "group",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
let parsed_group =
|
||||
group
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 2,
|
||||
expected_type: "GroupType",
|
||||
raw_input: group.to_owned(),
|
||||
})?;
|
||||
groups.push(parsed_group);
|
||||
next = parts.next();
|
||||
}
|
||||
|
||||
if let Some(w) = next
|
||||
&& w == "window"
|
||||
{
|
||||
let window_str = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "window",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
let parsed_window =
|
||||
window_str
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 3,
|
||||
expected_type: "WindowRange",
|
||||
raw_input: window_str.to_owned(),
|
||||
})?;
|
||||
window = Some(parsed_window);
|
||||
}
|
||||
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok(ListRequest {
|
||||
tagname,
|
||||
filter,
|
||||
groups,
|
||||
window,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ListResponse(Vec<String>);
|
||||
|
||||
impl CommandResponse for ListResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts_: Vec<_> = parts.into_vec()?;
|
||||
debug_assert!({
|
||||
let key = parts.0.first().map(|(k, _)| k);
|
||||
parts.0.iter().all(|(k, _)| k == key.unwrap())
|
||||
let key = parts_.first().map(|(k, _)| k);
|
||||
parts_.iter().all(|(k, _)| k == key.unwrap())
|
||||
});
|
||||
|
||||
let list = parts
|
||||
.0
|
||||
let list = parts_
|
||||
.into_iter()
|
||||
.map(|(k, v)| Ok(expect_property_type!(Some(v), k, Text).to_string()))
|
||||
.collect::<Result<Vec<_>, ResponseParserError>>()?;
|
||||
|
||||
Ok(list)
|
||||
Ok(ListResponse(list))
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for List {
|
||||
type Request = ListRequest;
|
||||
type Response = ListResponse;
|
||||
}
|
||||
|
||||
@@ -1,34 +1,163 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, CommandResponse, ResponseParserError, single_optional_item_command_request,
|
||||
},
|
||||
response_tokenizer::ResponseAttributes,
|
||||
types::{DbSelectionPrintResponse, Uri},
|
||||
};
|
||||
|
||||
pub struct ListAll;
|
||||
|
||||
single_optional_item_command_request!(ListAll, "listall", Uri);
|
||||
|
||||
// TODO: This is supposed to be a tree-like structure, with directories containing files and playlists
|
||||
pub type ListAllResponse = Vec<String>;
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ListAllResponse(Vec<DbSelectionPrintResponse>);
|
||||
|
||||
impl CommandResponse for ListAllResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let result = DbSelectionPrintResponse::parse(parts)?;
|
||||
|
||||
Ok(ListAllResponse(result))
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for ListAll {
|
||||
type Request = ListAllRequest;
|
||||
type Response = ListAllResponse;
|
||||
const COMMAND: &'static str = "listall";
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = parts
|
||||
.next()
|
||||
.map(|s| {
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::types::{DbDirectoryInfo, DbPlaylistInfo, DbSongInfo};
|
||||
|
||||
use super::*;
|
||||
|
||||
use indoc::indoc;
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_parse_response() {
|
||||
let response = indoc! {"
|
||||
directory: albums
|
||||
directory: albums/a
|
||||
file: albums/a/song1.mp3
|
||||
file: albums/a/song2.mp3
|
||||
file: albums/a/song3.mp3
|
||||
playlist: albums/a/album a.m3u8
|
||||
directory: albums/b
|
||||
file: albums/b/song1.mp3
|
||||
file: albums/b/song2.mp3
|
||||
file: albums/b/song3.mp3
|
||||
playlist: albums/b/album b.m3u8
|
||||
OK
|
||||
"};
|
||||
|
||||
let result = ListAll::parse_raw_response(response.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok(ListAllResponse(vec![
|
||||
DbSelectionPrintResponse::Directory(DbDirectoryInfo {
|
||||
directory: PathBuf::from("albums"),
|
||||
last_modified: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Directory(DbDirectoryInfo {
|
||||
directory: PathBuf::from("albums/a"),
|
||||
last_modified: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Song(DbSongInfo {
|
||||
file: PathBuf::from("albums/a/song1.mp3"),
|
||||
range: None,
|
||||
last_modified: None,
|
||||
added: None,
|
||||
format: None,
|
||||
tags: vec![],
|
||||
time: None,
|
||||
duration: None,
|
||||
playlist: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Song(DbSongInfo {
|
||||
file: PathBuf::from("albums/a/song2.mp3"),
|
||||
range: None,
|
||||
last_modified: None,
|
||||
added: None,
|
||||
format: None,
|
||||
tags: vec![],
|
||||
time: None,
|
||||
duration: None,
|
||||
playlist: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Song(DbSongInfo {
|
||||
file: PathBuf::from("albums/a/song3.mp3"),
|
||||
range: None,
|
||||
last_modified: None,
|
||||
added: None,
|
||||
format: None,
|
||||
tags: vec![],
|
||||
time: None,
|
||||
duration: None,
|
||||
playlist: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Playlist(DbPlaylistInfo {
|
||||
playlist: PathBuf::from("albums/a/album a.m3u8"),
|
||||
last_modified: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Directory(DbDirectoryInfo {
|
||||
directory: PathBuf::from("albums/b"),
|
||||
last_modified: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Song(DbSongInfo {
|
||||
file: PathBuf::from("albums/b/song1.mp3"),
|
||||
range: None,
|
||||
last_modified: None,
|
||||
added: None,
|
||||
format: None,
|
||||
tags: vec![],
|
||||
time: None,
|
||||
duration: None,
|
||||
playlist: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Song(DbSongInfo {
|
||||
file: PathBuf::from("albums/b/song2.mp3"),
|
||||
range: None,
|
||||
last_modified: None,
|
||||
added: None,
|
||||
format: None,
|
||||
tags: vec![],
|
||||
time: None,
|
||||
duration: None,
|
||||
playlist: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Song(DbSongInfo {
|
||||
file: PathBuf::from("albums/b/song3.mp3"),
|
||||
range: None,
|
||||
last_modified: None,
|
||||
added: None,
|
||||
format: None,
|
||||
tags: vec![],
|
||||
time: None,
|
||||
duration: None,
|
||||
playlist: None
|
||||
}),
|
||||
DbSelectionPrintResponse::Playlist(DbPlaylistInfo {
|
||||
playlist: PathBuf::from("albums/b/album b.m3u8"),
|
||||
last_modified: None
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ListAll(uri), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
]))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,120 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, CommandResponse, ResponseParserError, single_optional_item_command_request,
|
||||
},
|
||||
response_tokenizer::ResponseAttributes,
|
||||
types::{DbSelectionPrintResponse, Uri},
|
||||
};
|
||||
|
||||
pub struct ListAllInfo;
|
||||
|
||||
single_optional_item_command_request!(ListAllInfo, "listallinfo", Uri);
|
||||
|
||||
// TODO: This is supposed to be a tree-like structure, with directories containing files and playlists
|
||||
// in addition to the metadata of each entry
|
||||
pub type ListAllInfoResponse = Vec<String>;
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ListAllInfoResponse(Vec<DbSelectionPrintResponse>);
|
||||
|
||||
impl CommandResponse for ListAllInfoResponse {
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let result = DbSelectionPrintResponse::parse(parts)?;
|
||||
|
||||
Ok(ListAllInfoResponse(result))
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for ListAllInfo {
|
||||
type Request = ListAllInfoRequest;
|
||||
type Response = ListAllInfoResponse;
|
||||
const COMMAND: &'static str = "listallinfo";
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = parts
|
||||
.next()
|
||||
.map(|s| {
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::types::{DbDirectoryInfo, DbPlaylistInfo, DbSongInfo, Tag};
|
||||
|
||||
use super::*;
|
||||
|
||||
use indoc::indoc;
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_parse_response() {
|
||||
let response = indoc! {"
|
||||
directory: albums
|
||||
Last-Modified: 2024-10-01T10:00:00Z
|
||||
directory: albums/a
|
||||
Last-Modified: 2024-10-01T10:00:00Z
|
||||
file: albums/a/song1.mp3
|
||||
Last-Modified: 2022-12-31T09:00:00Z
|
||||
Added: 2021-12-31T09:00:00Z
|
||||
Format: 44100:16:2
|
||||
Title: Song A
|
||||
Artist: Artist A
|
||||
Album: Album A
|
||||
AlbumArtist: Artist A
|
||||
Composer: Artist A
|
||||
Genre: Pop
|
||||
Track: 1
|
||||
Disc: 1
|
||||
Date: 2020-01-01
|
||||
Time: 360
|
||||
duration: 360.123
|
||||
playlist: albums/a/album a.m3u8
|
||||
Last-Modified: 2022-12-31T09:00:00Z
|
||||
OK
|
||||
"};
|
||||
|
||||
let result = ListAllInfo::parse_raw_response(response.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok(ListAllInfoResponse(vec![
|
||||
DbSelectionPrintResponse::Directory(DbDirectoryInfo {
|
||||
directory: PathBuf::from("albums"),
|
||||
last_modified: Some("2024-10-01T10:00:00Z".to_string()),
|
||||
}),
|
||||
DbSelectionPrintResponse::Directory(DbDirectoryInfo {
|
||||
directory: PathBuf::from("albums/a"),
|
||||
last_modified: Some("2024-10-01T10:00:00Z".to_string()),
|
||||
}),
|
||||
DbSelectionPrintResponse::Song(DbSongInfo {
|
||||
file: PathBuf::from("albums/a/song1.mp3"),
|
||||
range: None,
|
||||
last_modified: Some("2022-12-31T09:00:00Z".to_string()),
|
||||
added: Some("2021-12-31T09:00:00Z".to_string()),
|
||||
format: Some("44100:16:2".to_string()),
|
||||
tags: vec![
|
||||
Tag::Album("Album A".to_string()),
|
||||
Tag::AlbumArtist("Artist A".to_string()),
|
||||
Tag::Artist("Artist A".to_string()),
|
||||
Tag::Composer("Artist A".to_string()),
|
||||
Tag::Date("2020-01-01".to_string()),
|
||||
Tag::Disc("1".to_string()),
|
||||
Tag::Genre("Pop".to_string()),
|
||||
Tag::Title("Song A".to_string()),
|
||||
Tag::Track("1".to_string()),
|
||||
],
|
||||
time: Some(360),
|
||||
duration: Some(360.123),
|
||||
playlist: None,
|
||||
}),
|
||||
DbSelectionPrintResponse::Playlist(DbPlaylistInfo {
|
||||
playlist: PathBuf::from("albums/a/album a.m3u8"),
|
||||
last_modified: Some("2022-12-31T09:00:00Z".to_string())
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ListAllInfo(uri), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
])),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,47 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, CommandResponse, ResponseParserError, single_optional_item_command_request,
|
||||
},
|
||||
response_tokenizer::ResponseAttributes,
|
||||
types::{DbDirectoryInfo, DbSelectionPrintResponse, Uri},
|
||||
};
|
||||
|
||||
pub struct ListFiles;
|
||||
|
||||
// TODO: fix this type
|
||||
pub type ListFilesResponse = Vec<String>;
|
||||
single_optional_item_command_request!(ListFiles, "listfiles", Uri);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ListFilesResponse(Vec<DbDirectoryInfo>);
|
||||
|
||||
impl CommandResponse for ListFilesResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
DbSelectionPrintResponse::parse(parts)?
|
||||
.into_iter()
|
||||
.map(|i| match i {
|
||||
DbSelectionPrintResponse::Directory(db_directory_info) => Ok(db_directory_info),
|
||||
DbSelectionPrintResponse::Song(_db_song_info) => {
|
||||
Err(ResponseParserError::UnexpectedProperty("file".to_string()))
|
||||
}
|
||||
DbSelectionPrintResponse::Playlist(_db_playlist_info) => Err(
|
||||
ResponseParserError::UnexpectedProperty("playlist".to_string()),
|
||||
),
|
||||
})
|
||||
.collect::<Result<Vec<_>, ResponseParserError>>()
|
||||
.map(ListFilesResponse)
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for ListFiles {
|
||||
type Request = ListFilesRequest;
|
||||
type Response = ListFilesResponse;
|
||||
const COMMAND: &'static str = "listfiles";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = parts
|
||||
.next()
|
||||
.map(|s| {
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ListFiles(uri), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,95 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, CommandResponse, ResponseParserError, single_optional_item_command_request,
|
||||
},
|
||||
response_tokenizer::ResponseAttributes,
|
||||
types::{DbSelectionPrintResponse, Uri},
|
||||
};
|
||||
|
||||
pub struct LsInfo;
|
||||
|
||||
// TODO: fix this type
|
||||
pub type LsInfoResponse = Vec<String>;
|
||||
single_optional_item_command_request!(LsInfo, "lsinfo", Uri);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct LsInfoResponse(Vec<DbSelectionPrintResponse>);
|
||||
|
||||
impl CommandResponse for LsInfoResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let result = DbSelectionPrintResponse::parse(parts)?;
|
||||
|
||||
Ok(LsInfoResponse(result))
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for LsInfo {
|
||||
type Request = LsInfoRequest;
|
||||
type Response = LsInfoResponse;
|
||||
const COMMAND: &'static str = "lsinfo";
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = parts
|
||||
.next()
|
||||
.map(|s| {
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::types::{DbDirectoryInfo, DbPlaylistInfo};
|
||||
|
||||
use super::*;
|
||||
|
||||
use indoc::indoc;
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn test_parse_response() {
|
||||
let response = indoc! {"
|
||||
directory: albums
|
||||
Last-Modified: 2024-10-01T10:00:00Z
|
||||
directory: albums/a
|
||||
Last-Modified: 2024-10-01T10:00:00Z
|
||||
playlist: albums/a/album a.m3u8
|
||||
Last-Modified: 2022-12-31T09:00:00Z
|
||||
directory: albums/b
|
||||
Last-Modified: 2023-10-01T10:00:00Z
|
||||
playlist: albums/b/album b.m3u8
|
||||
Last-Modified: 2021-12-31T09:00:00Z
|
||||
OK
|
||||
"};
|
||||
|
||||
let result = LsInfo::parse_raw_response(response.as_bytes());
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
Ok(LsInfoResponse(vec![
|
||||
DbSelectionPrintResponse::Directory(DbDirectoryInfo {
|
||||
directory: PathBuf::from("albums"),
|
||||
last_modified: Some("2024-10-01T10:00:00Z".to_string())
|
||||
}),
|
||||
DbSelectionPrintResponse::Directory(DbDirectoryInfo {
|
||||
directory: PathBuf::from("albums/a"),
|
||||
last_modified: Some("2024-10-01T10:00:00Z".to_string())
|
||||
}),
|
||||
DbSelectionPrintResponse::Playlist(DbPlaylistInfo {
|
||||
playlist: PathBuf::from("albums/a/album a.m3u8"),
|
||||
last_modified: Some("2022-12-31T09:00:00Z".to_string())
|
||||
}),
|
||||
DbSelectionPrintResponse::Directory(DbDirectoryInfo {
|
||||
directory: PathBuf::from("albums/b"),
|
||||
last_modified: Some("2023-10-01T10:00:00Z".to_string())
|
||||
}),
|
||||
DbSelectionPrintResponse::Playlist(DbPlaylistInfo {
|
||||
playlist: PathBuf::from("albums/b/album b.m3u8"),
|
||||
last_modified: Some("2021-12-31T09:00:00Z".to_string())
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::LsInfo(uri), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
])),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,47 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::commands::{
|
||||
Command, GenericResponseValue, Request, RequestParserError, RequestParserResult,
|
||||
ResponseAttributes, ResponseParserError,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{Command, CommandResponse, ResponseParserError, single_item_command_request},
|
||||
response_tokenizer::{GenericResponseValue, ResponseAttributes},
|
||||
types::Uri,
|
||||
};
|
||||
|
||||
pub struct ReadComments;
|
||||
|
||||
pub type ReadCommentsResponse = HashMap<String, String>;
|
||||
single_item_command_request!(ReadComments, "readcomments", Uri);
|
||||
|
||||
impl Command for ReadComments {
|
||||
type Response = ReadCommentsResponse;
|
||||
const COMMAND: &'static str = "readcomments";
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ReadCommentsResponse(HashMap<String, String>);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
let uri = uri
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, uri.to_owned()))?;
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ReadComments(uri), ""))
|
||||
impl CommandResponse for ReadCommentsResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let comments = parts
|
||||
.iter()
|
||||
.map(|(k, v)| match v {
|
||||
GenericResponseValue::Text(s) => Ok((k.to_string(), s.to_string())),
|
||||
GenericResponseValue::Binary(_) => Err(ResponseParserError::SyntaxError(1, k)),
|
||||
GenericResponseValue::Binary(_) => {
|
||||
Err(ResponseParserError::SyntaxError(1, k.to_string()))
|
||||
}
|
||||
})
|
||||
.collect::<Result<HashMap<_, _>, ResponseParserError>>()?;
|
||||
|
||||
Ok(comments)
|
||||
Ok(ReadCommentsResponse(comments))
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for ReadComments {
|
||||
type Request = ReadCommentsRequest;
|
||||
type Response = ReadCommentsResponse;
|
||||
}
|
||||
|
||||
@@ -1,62 +1,109 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError, get_and_parse_property, get_optional_property, get_property,
|
||||
commands::{Command, CommandRequest, CommandResponse, RequestParserError, ResponseParserError},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
response_tokenizer::{
|
||||
ResponseAttributes, get_and_parse_property, get_optional_property, get_property,
|
||||
},
|
||||
common::Offset,
|
||||
types::{Offset, Uri},
|
||||
};
|
||||
|
||||
pub struct ReadPicture;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ReadPictureRequest {
|
||||
pub uri: Uri,
|
||||
pub offset: Offset,
|
||||
}
|
||||
|
||||
impl CommandRequest for ReadPictureRequest {
|
||||
const COMMAND: &'static str = "readpicture";
|
||||
const MIN_ARGS: u32 = 2;
|
||||
const MAX_ARGS: Option<u32> = Some(2);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::ReadPicture(self.uri, self.offset)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::ReadPicture(uri, offset) => Some(ReadPictureRequest { uri, offset }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
format!("{} {} {}\n", Self::COMMAND, self.uri, self.offset)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let uri = match parts.next() {
|
||||
Some(s) => s,
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
let offset = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<Offset>()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 1,
|
||||
expected_type: "Offset",
|
||||
raw_input: s.to_owned(),
|
||||
})?,
|
||||
None => return Err(Self::missing_arguments_error(1)),
|
||||
};
|
||||
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok(ReadPictureRequest {
|
||||
uri: uri.to_string(),
|
||||
offset,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ReadPictureResponse {
|
||||
pub size: usize,
|
||||
pub binary: Vec<u8>,
|
||||
pub mimetype: Option<String>,
|
||||
}
|
||||
|
||||
impl Command for ReadPicture {
|
||||
type Response = Option<ReadPictureResponse>;
|
||||
const COMMAND: &'static str = "readpicture";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = match parts.next() {
|
||||
Some(s) => s,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
let offset = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<Offset>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ReadPicture(uri.to_string(), offset), ""))
|
||||
impl CommandResponse for ReadPictureResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
|
||||
if parts.is_empty() {
|
||||
return Ok(None);
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
// TODO: is empty response possible?
|
||||
// if parts.is_empty() {
|
||||
// return Err(ResponseParserError::UnexpectedEOF);
|
||||
// }
|
||||
|
||||
let size = get_and_parse_property!(parts, "size", Text);
|
||||
|
||||
let binary = get_property!(parts, "binary", Binary).into();
|
||||
|
||||
let mimetype = get_optional_property!(parts, "mimetype", Text).map(|s| s.to_string());
|
||||
|
||||
Ok(Some(ReadPictureResponse {
|
||||
Ok(ReadPictureResponse {
|
||||
size,
|
||||
binary,
|
||||
mimetype,
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for ReadPicture {
|
||||
type Request = ReadPictureRequest;
|
||||
type Response = ReadPictureResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,43 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
get_and_parse_property,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, CommandResponse, ResponseParserError, single_optional_item_command_request,
|
||||
},
|
||||
response_tokenizer::{ResponseAttributes, get_and_parse_property},
|
||||
types::Uri,
|
||||
};
|
||||
|
||||
pub struct Rescan;
|
||||
|
||||
single_optional_item_command_request!(Rescan, "rescan", Uri);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct RescanResponse {
|
||||
pub updating_db: usize,
|
||||
}
|
||||
|
||||
impl Command for Rescan {
|
||||
type Response = RescanResponse;
|
||||
const COMMAND: &'static str = "rescan";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = parts.next().map(|s| s.to_string());
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Rescan(uri), ""))
|
||||
impl CommandResponse for RescanResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let updating_db = get_and_parse_property!(parts, "updating_db", Text);
|
||||
|
||||
Ok(RescanResponse { updating_db })
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Rescan {
|
||||
type Request = RescanRequest;
|
||||
type Response = RescanResponse;
|
||||
}
|
||||
|
||||
@@ -1,51 +1,143 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
filter::parse_filter,
|
||||
commands::{Command, CommandRequest, CommandResponse, RequestParserError, ResponseParserError},
|
||||
filter::Filter,
|
||||
request_tokenizer::RequestTokenizer,
|
||||
response_tokenizer::ResponseAttributes,
|
||||
types::{DbSelectionPrintResponse, DbSongInfo, Sort, WindowRange},
|
||||
};
|
||||
|
||||
pub struct Search;
|
||||
|
||||
pub struct SearchResponse {}
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SearchRequest {
|
||||
filter: Filter,
|
||||
sort: Option<Sort>,
|
||||
window: Option<WindowRange>,
|
||||
}
|
||||
|
||||
impl Command for Search {
|
||||
type Response = SearchResponse;
|
||||
impl CommandRequest for SearchRequest {
|
||||
const COMMAND: &'static str = "search";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(3);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let filter = parse_filter(&mut parts)?;
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Search(self.filter, self.sort, self.window)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Search(filter, sort, window) => Some(SearchRequest {
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let mut cmd = format!("{} {}", Self::COMMAND, self.filter);
|
||||
if let Some(sort) = &self.sort {
|
||||
cmd.push_str(&format!(" sort {}", sort));
|
||||
}
|
||||
if let Some(window) = &self.window {
|
||||
cmd.push_str(&format!(" window {}", window));
|
||||
}
|
||||
cmd.push('\n');
|
||||
cmd
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let filter = match parts.next() {
|
||||
Some(f) => {
|
||||
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
let mut argument_index_counter = 0;
|
||||
let mut sort_or_window = parts.next();
|
||||
let mut sort = None;
|
||||
if let Some("sort") = sort_or_window {
|
||||
sort = Some(
|
||||
parts
|
||||
argument_index_counter += 1;
|
||||
let s = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string(),
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "sort",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
sort = Some(
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "Sort",
|
||||
raw_input: s.to_string(),
|
||||
})?,
|
||||
);
|
||||
sort_or_window = parts.next();
|
||||
}
|
||||
|
||||
let mut window = None;
|
||||
if let Some("window") = sort_or_window {
|
||||
let w = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
argument_index_counter += 1;
|
||||
let w = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "window",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
window = Some(
|
||||
w.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, w.to_string()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "WindowRange",
|
||||
raw_input: w.to_string(),
|
||||
})?,
|
||||
);
|
||||
}
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::Search(filter, sort, window), ""))
|
||||
Ok(SearchRequest {
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SearchResponse(Vec<DbSongInfo>);
|
||||
|
||||
impl CommandResponse for SearchResponse {
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
DbSelectionPrintResponse::parse(parts)?
|
||||
.into_iter()
|
||||
.map(|i| match i {
|
||||
DbSelectionPrintResponse::Song(db_song_info) => Ok(db_song_info),
|
||||
DbSelectionPrintResponse::Directory(_db_directory_info) => Err(
|
||||
ResponseParserError::UnexpectedProperty("directory".to_string()),
|
||||
),
|
||||
DbSelectionPrintResponse::Playlist(_db_playlist_info) => Err(
|
||||
ResponseParserError::UnexpectedProperty("playlist".to_string()),
|
||||
),
|
||||
})
|
||||
.collect::<Result<Vec<DbSongInfo>, ResponseParserError>>()
|
||||
.map(SearchResponse)
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Search {
|
||||
type Request = SearchRequest;
|
||||
type Response = SearchResponse;
|
||||
}
|
||||
|
||||
@@ -1,60 +1,141 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
filter::parse_filter,
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
filter::Filter,
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::{SongPosition, Sort, WindowRange},
|
||||
};
|
||||
|
||||
pub struct SearchAdd;
|
||||
|
||||
impl Command for SearchAdd {
|
||||
type Response = ();
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SearchAddRequest {
|
||||
filter: Filter,
|
||||
sort: Option<Sort>,
|
||||
window: Option<WindowRange>,
|
||||
position: Option<SongPosition>,
|
||||
}
|
||||
|
||||
impl CommandRequest for SearchAddRequest {
|
||||
const COMMAND: &'static str = "searchadd";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(4);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let filter = parse_filter(&mut parts)?;
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::SearchAdd(self.filter, self.sort, self.window, self.position)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::SearchAdd(filter, sort, window, position) => Some(SearchAddRequest {
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
position,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let mut cmd = format!("{} {}", Self::COMMAND, self.filter);
|
||||
if let Some(sort) = &self.sort {
|
||||
cmd.push_str(&format!(" sort {}", sort));
|
||||
}
|
||||
if let Some(window) = &self.window {
|
||||
cmd.push_str(&format!(" window {}", window));
|
||||
}
|
||||
if let Some(position) = &self.position {
|
||||
cmd.push_str(&format!(" position {}", position));
|
||||
}
|
||||
cmd.push('\n');
|
||||
cmd
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let filter = match parts.next() {
|
||||
Some(f) => {
|
||||
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
let mut argument_index_counter = 0;
|
||||
let mut sort_or_window_or_position = parts.next();
|
||||
let mut sort = None;
|
||||
if let Some("sort") = sort_or_window_or_position {
|
||||
sort = Some(
|
||||
parts
|
||||
argument_index_counter += 1;
|
||||
let s = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string(),
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "sort",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
sort = Some(
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "Sort",
|
||||
raw_input: s.to_string(),
|
||||
})?,
|
||||
);
|
||||
sort_or_window_or_position = parts.next();
|
||||
}
|
||||
|
||||
let mut window = None;
|
||||
if let Some("window") = sort_or_window_or_position {
|
||||
let w = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
argument_index_counter += 1;
|
||||
let w = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "window",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
window = Some(
|
||||
w.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, w.to_string()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "WindowRange",
|
||||
raw_input: w.to_string(),
|
||||
})?,
|
||||
);
|
||||
sort_or_window_or_position = parts.next();
|
||||
}
|
||||
|
||||
let mut position = None;
|
||||
if let Some("position") = sort_or_window_or_position {
|
||||
let p = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
argument_index_counter += 1;
|
||||
let p = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "position",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
position = Some(
|
||||
p.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, p.to_string()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "SongPosition",
|
||||
raw_input: p.to_string(),
|
||||
})?,
|
||||
);
|
||||
}
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::SearchAdd(filter, sort, window, position), ""))
|
||||
Ok(SearchAddRequest {
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
position,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(SearchAdd);
|
||||
|
||||
impl Command for SearchAdd {
|
||||
type Request = SearchAddRequest;
|
||||
type Response = SearchAddResponse;
|
||||
}
|
||||
|
||||
@@ -1,68 +1,157 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
filter::parse_filter,
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
filter::Filter,
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::{PlaylistName, SongPosition, Sort, WindowRange},
|
||||
};
|
||||
|
||||
pub struct SearchAddPl;
|
||||
|
||||
impl Command for SearchAddPl {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "searchaddpl";
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SearchAddPlRequest {
|
||||
playlist_name: PlaylistName,
|
||||
filter: Filter,
|
||||
sort: Option<Sort>,
|
||||
window: Option<WindowRange>,
|
||||
position: Option<SongPosition>,
|
||||
}
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl CommandRequest for SearchAddPlRequest {
|
||||
const COMMAND: &'static str = "searchaddpl";
|
||||
const MIN_ARGS: u32 = 2;
|
||||
const MAX_ARGS: Option<u32> = Some(5);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::SearchAddPl(
|
||||
self.playlist_name,
|
||||
self.filter,
|
||||
self.sort,
|
||||
self.window,
|
||||
self.position,
|
||||
)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::SearchAddPl(playlist_name, filter, sort, window, position) => {
|
||||
Some(SearchAddPlRequest {
|
||||
playlist_name,
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
position,
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let mut cmd = format!("{} {} {}", Self::COMMAND, self.playlist_name, self.filter);
|
||||
if let Some(sort) = &self.sort {
|
||||
cmd.push_str(&format!(" sort {}", sort));
|
||||
}
|
||||
if let Some(window) = &self.window {
|
||||
cmd.push_str(&format!(" window {}", window));
|
||||
}
|
||||
if let Some(position) = &self.position {
|
||||
cmd.push_str(&format!(" position {}", position));
|
||||
}
|
||||
cmd.push('\n');
|
||||
cmd
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let playlist_name = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.ok_or(Self::missing_arguments_error(0))?
|
||||
.to_string();
|
||||
|
||||
let filter = parse_filter(&mut parts)?;
|
||||
let filter = match parts.next() {
|
||||
Some(f) => {
|
||||
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(1)),
|
||||
};
|
||||
|
||||
let mut argument_index_counter = 1;
|
||||
let mut sort_or_window_or_position = parts.next();
|
||||
let mut sort = None;
|
||||
if let Some("sort") = sort_or_window_or_position {
|
||||
sort = Some(
|
||||
parts
|
||||
argument_index_counter += 1;
|
||||
let s = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string(),
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "sort",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
sort = Some(
|
||||
s.parse()
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "Sort",
|
||||
raw_input: s.to_string(),
|
||||
})?,
|
||||
);
|
||||
sort_or_window_or_position = parts.next();
|
||||
}
|
||||
|
||||
let mut window = None;
|
||||
if let Some("window") = sort_or_window_or_position {
|
||||
let w = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
argument_index_counter += 1;
|
||||
let w = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "window",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
window = Some(
|
||||
w.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, w.to_string()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "WindowRange",
|
||||
raw_input: w.to_string(),
|
||||
})?,
|
||||
);
|
||||
sort_or_window_or_position = parts.next();
|
||||
}
|
||||
|
||||
let mut position = None;
|
||||
if let Some("position") = sort_or_window_or_position {
|
||||
let p = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
argument_index_counter += 1;
|
||||
let p = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "position",
|
||||
argument_index: argument_index_counter,
|
||||
})?;
|
||||
position = Some(
|
||||
p.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, p.to_string()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: argument_index_counter,
|
||||
expected_type: "SongPosition",
|
||||
raw_input: p.to_string(),
|
||||
})?,
|
||||
);
|
||||
}
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((
|
||||
Request::SearchAddPl(playlist_name, filter, sort, window, position),
|
||||
"",
|
||||
))
|
||||
Ok(SearchAddPlRequest {
|
||||
playlist_name,
|
||||
filter,
|
||||
sort,
|
||||
window,
|
||||
position,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(SearchAddPl);
|
||||
|
||||
impl Command for SearchAddPl {
|
||||
type Request = SearchAddPlRequest;
|
||||
type Response = SearchAddPlResponse;
|
||||
}
|
||||
|
||||
@@ -1,47 +1,101 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError, get_and_parse_property,
|
||||
},
|
||||
filter::parse_filter,
|
||||
commands::{Command, CommandRequest, CommandResponse, RequestParserError, ResponseParserError},
|
||||
filter::Filter,
|
||||
request_tokenizer::RequestTokenizer,
|
||||
response_tokenizer::{ResponseAttributes, get_and_parse_property},
|
||||
types::{GroupType, Seconds},
|
||||
};
|
||||
|
||||
pub struct SearchCount;
|
||||
|
||||
pub struct SearchCountResponse {
|
||||
pub songs: usize,
|
||||
pub playtime: u64,
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SearchCountRequest {
|
||||
filter: Filter,
|
||||
group: Option<GroupType>,
|
||||
}
|
||||
|
||||
impl Command for SearchCount {
|
||||
type Response = SearchCountResponse;
|
||||
impl CommandRequest for SearchCountRequest {
|
||||
const COMMAND: &'static str = "searchcount";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(2);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let filter = parse_filter(&mut parts)?;
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::SearchCount(self.filter, self.group)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::SearchCount(filter, group) => {
|
||||
Some(SearchCountRequest { filter, group })
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let mut cmd = format!("{} {}", Self::COMMAND, self.filter);
|
||||
if let Some(group) = &self.group {
|
||||
cmd.push_str(&format!(" group {}", group));
|
||||
}
|
||||
cmd.push('\n');
|
||||
cmd
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let filter = match parts.next() {
|
||||
Some(f) => {
|
||||
Filter::parse(f).map_err(|_| RequestParserError::SyntaxError(1, f.to_owned()))?
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
let group = if let Some("group") = parts.next() {
|
||||
let group = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
|
||||
let group = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::MissingKeywordValue {
|
||||
keyword: "group",
|
||||
argument_index: 1,
|
||||
})?;
|
||||
Some(
|
||||
group
|
||||
.parse()
|
||||
.map_err(|_| RequestParserError::SyntaxError(1, group.to_owned()))?,
|
||||
.map_err(|_| RequestParserError::SubtypeParserError {
|
||||
argument_index: 1,
|
||||
expected_type: "Group",
|
||||
raw_input: group.to_string(),
|
||||
})?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::SearchCount(filter, group), ""))
|
||||
Ok(SearchCountRequest { filter, group })
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct SearchCountResponse {
|
||||
pub songs: usize,
|
||||
pub playtime: Seconds,
|
||||
}
|
||||
|
||||
impl CommandResponse for SearchCountResponse {
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let songs = get_and_parse_property!(parts, "songs", Text);
|
||||
let playtime = get_and_parse_property!(parts, "playtime", Text);
|
||||
@@ -49,3 +103,8 @@ impl Command for SearchCount {
|
||||
Ok(SearchCountResponse { songs, playtime })
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for SearchCount {
|
||||
type Request = SearchCountRequest;
|
||||
type Response = SearchCountResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,43 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
get_and_parse_property,
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, CommandResponse, ResponseParserError, single_optional_item_command_request,
|
||||
},
|
||||
response_tokenizer::{ResponseAttributes, get_and_parse_property},
|
||||
types::Uri,
|
||||
};
|
||||
|
||||
pub struct Update;
|
||||
|
||||
single_optional_item_command_request!(Update, "update", Uri);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct UpdateResponse {
|
||||
updating_db: usize,
|
||||
}
|
||||
|
||||
impl Command for Update {
|
||||
type Response = UpdateResponse;
|
||||
const COMMAND: &'static str = "update";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let uri = parts.next().map(|s| s.to_string());
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Update(uri), ""))
|
||||
impl CommandResponse for UpdateResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let updating_db = get_and_parse_property!(parts, "updating_db", Text);
|
||||
|
||||
Ok(UpdateResponse { updating_db })
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Update {
|
||||
type Request = UpdateRequest;
|
||||
type Response = UpdateResponse;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
pub mod delpartition;
|
||||
pub mod listpartitions;
|
||||
pub mod moveoutput;
|
||||
pub mod newpartition;
|
||||
pub mod partition;
|
||||
mod delpartition;
|
||||
mod listpartitions;
|
||||
mod moveoutput;
|
||||
mod newpartition;
|
||||
mod partition;
|
||||
|
||||
pub use delpartition::DelPartition;
|
||||
pub use listpartitions::ListPartitions;
|
||||
pub use moveoutput::MoveOutput;
|
||||
pub use newpartition::NewPartition;
|
||||
pub use partition::Partition;
|
||||
pub use delpartition::*;
|
||||
pub use listpartitions::*;
|
||||
pub use moveoutput::*;
|
||||
pub use newpartition::*;
|
||||
pub use partition::*;
|
||||
|
||||
@@ -1,29 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::PartitionName,
|
||||
};
|
||||
|
||||
pub struct DelPartition;
|
||||
|
||||
single_item_command_request!(DelPartition, "delpartition", PartitionName);
|
||||
|
||||
empty_command_response!(DelPartition);
|
||||
|
||||
impl Command for DelPartition {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "delpartition";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let partition = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string();
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::DelPartition(partition), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = DelPartitionRequest;
|
||||
type Response = DelPartitionResponse;
|
||||
}
|
||||
|
||||
@@ -1,33 +1,16 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
expect_property_type,
|
||||
use crate::{
|
||||
commands::{Command, ResponseParserError, empty_command_request, multi_item_command_response},
|
||||
response_tokenizer::expect_property_type,
|
||||
types::PartitionName,
|
||||
};
|
||||
|
||||
pub struct ListPartitions;
|
||||
|
||||
pub type ListPartitionsResponse = Vec<String>;
|
||||
empty_command_request!(ListPartitions, "listpartitions");
|
||||
|
||||
multi_item_command_response!(ListPartitions, "partition", PartitionName);
|
||||
|
||||
impl Command for ListPartitions {
|
||||
type Request = ListPartitionsRequest;
|
||||
type Response = ListPartitionsResponse;
|
||||
const COMMAND: &'static str = "listpartitions";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::ListPartitions, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: Vec<_> = parts.into();
|
||||
|
||||
let mut partitions = Vec::with_capacity(parts.len());
|
||||
for (key, value) in parts.into_iter() {
|
||||
debug_assert_eq!(key, "partition");
|
||||
let partition = expect_property_type!(Some(value), "partition", Text).to_string();
|
||||
partitions.push(partition);
|
||||
}
|
||||
|
||||
Ok(partitions)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_response, single_item_command_request};
|
||||
|
||||
pub struct MoveOutput;
|
||||
|
||||
single_item_command_request!(MoveOutput, "moveoutput", String);
|
||||
|
||||
empty_command_response!(MoveOutput);
|
||||
|
||||
impl Command for MoveOutput {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "moveoutput";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let output_name = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string();
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::MoveOutput(output_name), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = MoveOutputRequest;
|
||||
type Response = MoveOutputResponse;
|
||||
}
|
||||
|
||||
@@ -1,29 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::PartitionName,
|
||||
};
|
||||
|
||||
pub struct NewPartition;
|
||||
|
||||
single_item_command_request!(NewPartition, "newpartition", PartitionName);
|
||||
|
||||
empty_command_response!(NewPartition);
|
||||
|
||||
impl Command for NewPartition {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "newpartition";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let partition = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string();
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::NewPartition(partition), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = NewPartitionRequest;
|
||||
type Response = NewPartitionResponse;
|
||||
}
|
||||
|
||||
@@ -1,29 +1,15 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::PartitionName,
|
||||
};
|
||||
|
||||
pub struct Partition;
|
||||
|
||||
single_item_command_request!(Partition, "partition", PartitionName);
|
||||
|
||||
empty_command_response!(Partition);
|
||||
|
||||
impl Command for Partition {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "partition";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let partition = parts
|
||||
.next()
|
||||
.ok_or(RequestParserError::UnexpectedEOF)?
|
||||
.to_string();
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Partition(partition), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = PartitionRequest;
|
||||
type Response = PartitionResponse;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
pub mod consume;
|
||||
pub mod crossfade;
|
||||
pub mod getvol;
|
||||
pub mod mixrampdb;
|
||||
pub mod mixrampdelay;
|
||||
pub mod random;
|
||||
pub mod repeat;
|
||||
pub mod replay_gain_mode;
|
||||
pub mod replay_gain_status;
|
||||
pub mod setvol;
|
||||
pub mod single;
|
||||
pub mod volume;
|
||||
mod consume;
|
||||
mod crossfade;
|
||||
mod getvol;
|
||||
mod mixrampdb;
|
||||
mod mixrampdelay;
|
||||
mod random;
|
||||
mod repeat;
|
||||
mod replay_gain_mode;
|
||||
mod replay_gain_status;
|
||||
mod setvol;
|
||||
mod single;
|
||||
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;
|
||||
pub use consume::*;
|
||||
pub use crossfade::*;
|
||||
pub use getvol::*;
|
||||
pub use mixrampdb::*;
|
||||
pub use mixrampdelay::*;
|
||||
pub use random::*;
|
||||
pub use repeat::*;
|
||||
pub use replay_gain_mode::*;
|
||||
pub use replay_gain_status::*;
|
||||
pub use setvol::*;
|
||||
pub use single::*;
|
||||
pub use volume::*;
|
||||
|
||||
@@ -1,32 +1,15 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::BoolOrOneshot,
|
||||
};
|
||||
|
||||
pub struct Consume;
|
||||
|
||||
single_item_command_request!(Consume, "consume", BoolOrOneshot);
|
||||
|
||||
empty_command_response!(Consume);
|
||||
|
||||
impl Command for Consume {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "consume";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let state = match parts.next() {
|
||||
Some(s) => crate::common::BoolOrOneshot::from_str(s)
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Consume(state), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = ConsumeRequest;
|
||||
type Response = ConsumeResponse;
|
||||
}
|
||||
|
||||
@@ -1,34 +1,15 @@
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::Seconds,
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::Seconds,
|
||||
};
|
||||
|
||||
pub struct Crossfade;
|
||||
|
||||
single_item_command_request!(Crossfade, "crossfade", Seconds);
|
||||
|
||||
empty_command_response!(Crossfade);
|
||||
|
||||
impl Command for Crossfade {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "crossfade";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let seconds = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<Seconds>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Crossfade(seconds), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = CrossfadeRequest;
|
||||
type Response = CrossfadeResponse;
|
||||
}
|
||||
|
||||
@@ -1,30 +1,15 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
get_and_parse_property,
|
||||
},
|
||||
common::VolumeValue,
|
||||
commands::{Command, empty_command_request, single_item_command_response},
|
||||
types::VolumeValue,
|
||||
};
|
||||
|
||||
pub struct GetVol;
|
||||
|
||||
empty_command_request!(GetVol, "getvol");
|
||||
|
||||
single_item_command_response!(GetVol, "volume", VolumeValue);
|
||||
|
||||
impl Command for GetVol {
|
||||
type Response = VolumeValue;
|
||||
const COMMAND: &'static str = "getvol";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::GetVol, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
assert_eq!(parts.len(), 1);
|
||||
let volume = get_and_parse_property!(parts, "volume", Text);
|
||||
Ok(volume)
|
||||
}
|
||||
type Request = GetVolRequest;
|
||||
type Response = GetVolResponse;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,12 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_response, single_item_command_request};
|
||||
|
||||
pub struct MixRampDb;
|
||||
|
||||
single_item_command_request!(MixRampDb, "mixrampdb", f32);
|
||||
|
||||
empty_command_response!(MixRampDb);
|
||||
|
||||
impl Command for MixRampDb {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "mixrampdb";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let db = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<f32>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::MixRampDb(db), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = MixRampDbRequest;
|
||||
type Response = MixRampDbResponse;
|
||||
}
|
||||
|
||||
@@ -1,34 +1,14 @@
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::Seconds,
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::Seconds,
|
||||
};
|
||||
|
||||
pub struct MixRampDelay;
|
||||
|
||||
single_item_command_request!(MixRampDelay, "mixrampdelay", Seconds);
|
||||
empty_command_response!(MixRampDelay);
|
||||
|
||||
impl Command for MixRampDelay {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "mixrampdelay";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let seconds = match parts.next() {
|
||||
Some(s) => s
|
||||
.parse::<Seconds>()
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::MixRampDelay(seconds), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = MixRampDelayRequest;
|
||||
type Response = MixRampDelayResponse;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,56 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
};
|
||||
|
||||
pub struct Random;
|
||||
|
||||
impl Command for Random {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "random";
|
||||
pub struct RandomRequest(bool);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl CommandRequest for RandomRequest {
|
||||
const COMMAND: &'static str = "random";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(1);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Random(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Random(state) => Some(RandomRequest(state)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let state = if self.0 { "1" } else { "0" };
|
||||
format!("{} {}\n", Self::COMMAND, state)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let state = match parts.next() {
|
||||
Some("0") => false,
|
||||
Some("1") => true,
|
||||
Some(s) => return Err(RequestParserError::SyntaxError(0, s.to_owned())),
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
Some(s) => {
|
||||
return Err(RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "bool",
|
||||
raw_input: s.to_owned(),
|
||||
});
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::Random(state), ""))
|
||||
Ok(RandomRequest(state))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(Random);
|
||||
|
||||
impl Command for Random {
|
||||
type Request = RandomRequest;
|
||||
type Response = RandomResponse;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,56 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
};
|
||||
|
||||
pub struct Repeat;
|
||||
|
||||
impl Command for Repeat {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "repeat";
|
||||
pub struct RepeatRequest(bool);
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
impl CommandRequest for RepeatRequest {
|
||||
const COMMAND: &'static str = "repeat";
|
||||
const MIN_ARGS: u32 = 1;
|
||||
const MAX_ARGS: Option<u32> = Some(1);
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Repeat(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Repeat(state) => Some(RepeatRequest(state)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
let state = if self.0 { "1" } else { "0" };
|
||||
format!("{} {}\n", Self::COMMAND, state)
|
||||
}
|
||||
|
||||
fn parse(mut parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let state = match parts.next() {
|
||||
Some("0") => false,
|
||||
Some("1") => true,
|
||||
Some(s) => return Err(RequestParserError::SyntaxError(0, s.to_owned())),
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
Some(s) => {
|
||||
return Err(RequestParserError::SubtypeParserError {
|
||||
argument_index: 0,
|
||||
expected_type: "bool",
|
||||
raw_input: s.to_owned(),
|
||||
});
|
||||
}
|
||||
None => return Err(Self::missing_arguments_error(0)),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
Self::throw_if_too_many_arguments(parts)?;
|
||||
|
||||
Ok((Request::Repeat(state), ""))
|
||||
Ok(RepeatRequest(state))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(Repeat);
|
||||
|
||||
impl Command for Repeat {
|
||||
type Request = RepeatRequest;
|
||||
type Response = RepeatResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,15 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::ReplayGainModeMode,
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::ReplayGainModeMode,
|
||||
};
|
||||
|
||||
pub struct ReplayGainMode;
|
||||
|
||||
single_item_command_request!(ReplayGainMode, "replay_gain_mode", ReplayGainModeMode);
|
||||
|
||||
empty_command_response!(ReplayGainMode);
|
||||
|
||||
impl Command for ReplayGainMode {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "replay_gain_mode";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let mode = match parts.next() {
|
||||
Some(s) => ReplayGainModeMode::from_str(s)
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ReplayGainMode(mode), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = ReplayGainModeRequest;
|
||||
type Response = ReplayGainModeResponse;
|
||||
}
|
||||
|
||||
@@ -3,39 +3,45 @@ use std::{collections::HashMap, str::FromStr};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
get_property,
|
||||
},
|
||||
common::ReplayGainModeMode,
|
||||
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
|
||||
response_tokenizer::{ResponseAttributes, get_property},
|
||||
types::ReplayGainModeMode,
|
||||
};
|
||||
|
||||
pub struct ReplayGainStatus;
|
||||
|
||||
empty_command_request!(ReplayGainStatus, "replay_gain_status");
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ReplayGainStatusResponse {
|
||||
pub replay_gain_mode: ReplayGainModeMode,
|
||||
}
|
||||
|
||||
impl Command for ReplayGainStatus {
|
||||
type Response = ReplayGainStatusResponse;
|
||||
const COMMAND: &'static str = "replay_gain_status";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
Ok((Request::ReplayGainStatus, ""))
|
||||
impl CommandResponse for ReplayGainStatusResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
let replay_gain_mode = get_property!(parts, "replay_gain_mode", Text);
|
||||
|
||||
Ok(ReplayGainStatusResponse {
|
||||
replay_gain_mode: ReplayGainModeMode::from_str(replay_gain_mode).map_err(|_| {
|
||||
ResponseParserError::InvalidProperty("replay_gain_mode", replay_gain_mode)
|
||||
ResponseParserError::InvalidProperty(
|
||||
"replay_gain_mode".to_string(),
|
||||
replay_gain_mode.to_string(),
|
||||
)
|
||||
})?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for ReplayGainStatus {
|
||||
type Request = ReplayGainStatusRequest;
|
||||
type Response = ReplayGainStatusResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,15 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::VolumeValue,
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::VolumeValue,
|
||||
};
|
||||
|
||||
pub struct SetVol;
|
||||
|
||||
single_item_command_request!(SetVol, "setvol", VolumeValue);
|
||||
|
||||
empty_command_response!(SetVol);
|
||||
|
||||
impl Command for SetVol {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "setvol";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let volume = match parts.next() {
|
||||
Some(s) => VolumeValue::from_str(s)
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::SetVol(volume), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = SetVolRequest;
|
||||
type Response = SetVolResponse;
|
||||
}
|
||||
|
||||
@@ -1,32 +1,15 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::BoolOrOneshot,
|
||||
};
|
||||
|
||||
pub struct Single;
|
||||
|
||||
single_item_command_request!(Single, "single", BoolOrOneshot);
|
||||
|
||||
empty_command_response!(Single);
|
||||
|
||||
impl Command for Single {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "single";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let state = match parts.next() {
|
||||
Some(s) => crate::common::BoolOrOneshot::from_str(s)
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Single(state), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = SingleRequest;
|
||||
type Response = SingleResponse;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,15 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::{
|
||||
commands::{
|
||||
Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError,
|
||||
},
|
||||
common::VolumeValue,
|
||||
commands::{Command, empty_command_response, single_item_command_request},
|
||||
types::VolumeValue,
|
||||
};
|
||||
|
||||
pub struct Volume;
|
||||
|
||||
single_item_command_request!(Volume, "volume", VolumeValue);
|
||||
|
||||
empty_command_response!(Volume);
|
||||
|
||||
impl Command for Volume {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "volume";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let change = match parts.next() {
|
||||
Some(s) => VolumeValue::from_str(s)
|
||||
.map_err(|_| RequestParserError::SyntaxError(0, s.to_owned()))?,
|
||||
None => return Err(RequestParserError::UnexpectedEOF),
|
||||
};
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Volume(change), ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = VolumeRequest;
|
||||
type Response = VolumeResponse;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
pub mod clearerror;
|
||||
pub mod currentsong;
|
||||
pub mod idle;
|
||||
pub mod stats;
|
||||
pub mod status;
|
||||
mod clearerror;
|
||||
mod currentsong;
|
||||
mod idle;
|
||||
mod stats;
|
||||
mod status;
|
||||
|
||||
pub use clearerror::ClearError;
|
||||
pub use currentsong::CurrentSong;
|
||||
pub use idle::Idle;
|
||||
pub use stats::Stats;
|
||||
pub use status::Status;
|
||||
pub use clearerror::*;
|
||||
pub use currentsong::*;
|
||||
pub use idle::*;
|
||||
pub use stats::*;
|
||||
pub use status::*;
|
||||
|
||||
@@ -1,24 +1,13 @@
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
};
|
||||
use crate::commands::{Command, empty_command_request, empty_command_response};
|
||||
|
||||
/// Clears the current error message in status (this is also accomplished by any command that starts playback)
|
||||
pub struct ClearError;
|
||||
|
||||
empty_command_request!(ClearError, "clearerror");
|
||||
|
||||
empty_command_response!(ClearError);
|
||||
|
||||
impl Command for ClearError {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "clearerror";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::ClearError, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
type Request = ClearErrorRequest;
|
||||
type Response = ClearErrorResponse;
|
||||
}
|
||||
|
||||
@@ -1,28 +1,60 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
|
||||
response_tokenizer::{
|
||||
ResponseAttributes, get_and_parse_optional_property, get_and_parse_property,
|
||||
},
|
||||
types::{DbSongInfo, Priority, SongId, SongPosition},
|
||||
};
|
||||
|
||||
/// Displays the song info of the current song (same song that is identified in status)
|
||||
pub struct CurrentSong;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct CurrentSongResponse {}
|
||||
empty_command_request!(CurrentSong, "currentsong");
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct CurrentSongResponse {
|
||||
position: SongPosition,
|
||||
id: SongId,
|
||||
priority: Option<Priority>,
|
||||
song_info: DbSongInfo,
|
||||
}
|
||||
|
||||
impl CommandResponse for CurrentSongResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let mut parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let position: SongPosition = get_and_parse_property!(parts, "Pos", Text);
|
||||
let id: SongId = get_and_parse_property!(parts, "Id", Text);
|
||||
let priority: Option<Priority> = get_and_parse_optional_property!(parts, "Prio", Text);
|
||||
|
||||
parts.remove("Pos");
|
||||
parts.remove("Id");
|
||||
parts.remove("Prio");
|
||||
|
||||
let song_info = DbSongInfo::parse_map(parts)?;
|
||||
|
||||
Ok(CurrentSongResponse {
|
||||
position,
|
||||
id,
|
||||
priority,
|
||||
song_info,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for CurrentSong {
|
||||
type Request = CurrentSongRequest;
|
||||
type Response = CurrentSongResponse;
|
||||
const COMMAND: &'static str = "currentsong";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::CurrentSong, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
_parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +1,62 @@
|
||||
use std::str::{FromStr, SplitWhitespace};
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::common::SubSystem;
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
use crate::{
|
||||
commands::{Command, CommandRequest, RequestParserError, empty_command_response},
|
||||
request_tokenizer::RequestTokenizer,
|
||||
types::SubSystem,
|
||||
};
|
||||
|
||||
pub struct Idle;
|
||||
|
||||
impl Command for Idle {
|
||||
type Response = ();
|
||||
const COMMAND: &'static str = "idle";
|
||||
pub struct IdleRequest(Option<Vec<SubSystem>>);
|
||||
|
||||
fn parse_request(mut parts: SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
let result = parts
|
||||
.next()
|
||||
.map_or(Ok((Request::Idle(None), "")), |subsystems| {
|
||||
impl CommandRequest for IdleRequest {
|
||||
const COMMAND: &'static str = "idle";
|
||||
const MIN_ARGS: u32 = 0;
|
||||
const MAX_ARGS: Option<u32> = None;
|
||||
|
||||
fn into_request_enum(self) -> crate::Request {
|
||||
crate::Request::Idle(self.0)
|
||||
}
|
||||
|
||||
fn from_request_enum(request: crate::Request) -> Option<Self> {
|
||||
match request {
|
||||
crate::Request::Idle(subsystems) => Some(IdleRequest(subsystems)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize(&self) -> String {
|
||||
match &self.0 {
|
||||
Some(subsystems) => {
|
||||
let subsystems_str = subsystems
|
||||
.iter()
|
||||
.map(|subsystem| subsystem.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
format!("{} {}\n", Self::COMMAND, subsystems_str)
|
||||
}
|
||||
None => Self::COMMAND.to_string() + "\n",
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(parts: RequestTokenizer<'_>) -> Result<Self, RequestParserError> {
|
||||
let mut parts = parts;
|
||||
let result = parts.next().map_or(Ok(None), |subsystems| {
|
||||
let subsystems = subsystems
|
||||
.split(',')
|
||||
.map(|subsystem| SubSystem::from_str(subsystem).unwrap())
|
||||
.collect();
|
||||
Ok((Request::Idle(Some(subsystems)), ""))
|
||||
Ok(Some(subsystems))
|
||||
});
|
||||
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
result
|
||||
result.map(IdleRequest)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
debug_assert!(parts.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
empty_command_response!(Idle);
|
||||
|
||||
impl Command for Idle {
|
||||
type Request = IdleRequest;
|
||||
type Response = IdleResponse;
|
||||
}
|
||||
|
||||
@@ -2,13 +2,17 @@ use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::commands::{
|
||||
Command, Request, RequestParserResult, ResponseAttributes, ResponseParserError,
|
||||
get_and_parse_optional_property, get_and_parse_property,
|
||||
use crate::{
|
||||
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
|
||||
response_tokenizer::{
|
||||
ResponseAttributes, get_and_parse_optional_property, get_and_parse_property,
|
||||
},
|
||||
};
|
||||
|
||||
pub struct Stats;
|
||||
|
||||
empty_command_request!(Stats, "stats");
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct StatsResponse {
|
||||
pub uptime: u64,
|
||||
@@ -20,20 +24,17 @@ pub struct StatsResponse {
|
||||
pub db_update: Option<u64>,
|
||||
}
|
||||
|
||||
impl Command for Stats {
|
||||
type Response = StatsResponse;
|
||||
const COMMAND: &'static str = "stats";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Stats, ""))
|
||||
impl CommandResponse for StatsResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into();
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
|
||||
let uptime = get_and_parse_property!(parts, "uptime", Text);
|
||||
let playtime = get_and_parse_property!(parts, "playtime", Text);
|
||||
@@ -54,3 +55,8 @@ impl Command for Stats {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Stats {
|
||||
type Request = StatsRequest;
|
||||
type Response = StatsResponse;
|
||||
}
|
||||
|
||||
@@ -3,14 +3,19 @@ use std::str::FromStr;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::common::{Audio, BoolOrOneshot, SongId, SongPosition};
|
||||
|
||||
use crate::commands::{
|
||||
Command, GenericResponseValue, Request, RequestParserResult, ResponseAttributes,
|
||||
ResponseParserError, get_and_parse_optional_property, get_and_parse_property,
|
||||
use crate::{
|
||||
commands::{Command, CommandResponse, ResponseParserError, empty_command_request},
|
||||
response_tokenizer::{
|
||||
ResponseAttributes, get_and_parse_optional_property, get_and_parse_property,
|
||||
get_optional_property, get_property,
|
||||
},
|
||||
types::{Audio, BoolOrOneshot, SongId, SongPosition},
|
||||
};
|
||||
|
||||
pub struct Status;
|
||||
|
||||
empty_command_request!(Status, "status");
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum StatusResponseState {
|
||||
Play,
|
||||
@@ -60,32 +65,42 @@ pub struct StatusResponse {
|
||||
pub last_loaded_playlist: Option<String>,
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn parse_status_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<StatusResponse, ResponseParserError> {
|
||||
let parts: HashMap<&str, GenericResponseValue> = parts.into();
|
||||
impl CommandResponse for StatusResponse {
|
||||
fn into_response_enum(self) -> crate::Response {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn from_response_enum(response: crate::Response) -> Option<Self> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
|
||||
let parts: HashMap<_, _> = parts.into_map()?;
|
||||
let partition = get_property!(parts, "partition", Text).to_string();
|
||||
|
||||
let volume = match get_property!(parts, "volume", Text) {
|
||||
"-1" => None,
|
||||
volume => Some(
|
||||
volume
|
||||
.parse()
|
||||
.map_err(|_| ResponseParserError::InvalidProperty("volume", volume))?,
|
||||
),
|
||||
volume => Some(volume.parse().map_err(|_| {
|
||||
ResponseParserError::InvalidProperty("volume".to_string(), volume.to_string())
|
||||
})?),
|
||||
};
|
||||
|
||||
let repeat = match get_property!(parts, "repeat", Text) {
|
||||
"0" => Ok(false),
|
||||
"1" => Ok(true),
|
||||
repeat => Err(ResponseParserError::InvalidProperty("repeat", repeat)),
|
||||
repeat => Err(ResponseParserError::InvalidProperty(
|
||||
"repeat".to_string(),
|
||||
repeat.to_string(),
|
||||
)),
|
||||
}?;
|
||||
|
||||
let random = match get_property!(parts, "random", Text) {
|
||||
"0" => Ok(false),
|
||||
"1" => Ok(true),
|
||||
random => Err(ResponseParserError::InvalidProperty("random", random)),
|
||||
random => Err(ResponseParserError::InvalidProperty(
|
||||
"random".to_string(),
|
||||
random.to_string(),
|
||||
)),
|
||||
}?;
|
||||
|
||||
let single = get_and_parse_property!(parts, "single", Text);
|
||||
@@ -95,22 +110,28 @@ fn parse_status_response(
|
||||
let state: StatusResponseState = get_and_parse_property!(parts, "state", Text);
|
||||
let song: Option<SongPosition> = get_and_parse_optional_property!(parts, "song", Text);
|
||||
let song_id: Option<SongId> = get_and_parse_optional_property!(parts, "songid", Text);
|
||||
let next_song: Option<SongPosition> = get_and_parse_optional_property!(parts, "nextsong", Text);
|
||||
let next_song_id: Option<SongId> = get_and_parse_optional_property!(parts, "nextsongid", Text);
|
||||
let next_song: Option<SongPosition> =
|
||||
get_and_parse_optional_property!(parts, "nextsong", Text);
|
||||
let next_song_id: Option<SongId> =
|
||||
get_and_parse_optional_property!(parts, "nextsongid", Text);
|
||||
|
||||
let time = match get_optional_property!(parts, "time", Text) {
|
||||
Some(time) => {
|
||||
let mut parts = time.split(':');
|
||||
let elapsed = parts
|
||||
.next()
|
||||
.ok_or(ResponseParserError::SyntaxError(0, time))?
|
||||
.ok_or(ResponseParserError::SyntaxError(0, time.to_string()))?
|
||||
.parse()
|
||||
.map_err(|_| ResponseParserError::InvalidProperty("time", time))?;
|
||||
.map_err(|_| {
|
||||
ResponseParserError::InvalidProperty("time".to_string(), time.to_string())
|
||||
})?;
|
||||
let duration = parts
|
||||
.next()
|
||||
.ok_or(ResponseParserError::SyntaxError(0, time))?
|
||||
.ok_or(ResponseParserError::SyntaxError(0, time.to_string()))?
|
||||
.parse()
|
||||
.map_err(|_| ResponseParserError::InvalidProperty("time", time))?;
|
||||
.map_err(|_| {
|
||||
ResponseParserError::InvalidProperty("time".to_string(), time.to_string())
|
||||
})?;
|
||||
Some((elapsed, duration))
|
||||
}
|
||||
None => None,
|
||||
@@ -155,24 +176,11 @@ fn parse_status_response(
|
||||
last_loaded_playlist,
|
||||
})
|
||||
}
|
||||
|
||||
pub struct Status;
|
||||
}
|
||||
|
||||
impl Command for Status {
|
||||
type Request = StatusRequest;
|
||||
type Response = StatusResponse;
|
||||
const COMMAND: &'static str = "status";
|
||||
|
||||
fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
|
||||
debug_assert!(parts.next().is_none());
|
||||
|
||||
Ok((Request::Status, ""))
|
||||
}
|
||||
|
||||
fn parse_response(
|
||||
parts: ResponseAttributes<'_>,
|
||||
) -> Result<Self::Response, ResponseParserError> {
|
||||
parse_status_response(parts)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -207,7 +215,7 @@ mod tests {
|
||||
"# };
|
||||
|
||||
assert_eq!(
|
||||
Status::parse_raw_response(contents),
|
||||
Status::parse_raw_response(contents.as_bytes()),
|
||||
Ok(StatusResponse {
|
||||
partition: "default".into(),
|
||||
volume: Some(66),
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user