pub(crate) mod info;
pub(crate) mod read;
pub(crate) mod write;
use crate::drive::client::DriveClient;
use anyhow::Result;
use clap::{Parser, Subcommand};
#[derive(Parser)]
pub struct SlidesCommand {
#[command(subcommand)]
pub command: SlidesSubcommands,
}
#[derive(Subcommand)]
pub enum SlidesSubcommands {
Info(info::InfoCommand),
Read(read::ReadCommand),
Replace(write::ReplaceCommand),
}
impl SlidesCommand {
pub async fn execute(self, client: &DriveClient) -> Result<()> {
match self.command {
SlidesSubcommands::Info(cmd) => cmd.execute(client).await,
SlidesSubcommands::Read(cmd) => cmd.execute(client).await,
SlidesSubcommands::Replace(cmd) => cmd.execute(client).await,
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
use crate::drive::slides::client::{test_support::mock_clients, SLIDES_API_URL};
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
#[tokio::test]
async fn every_verb_dispatches_to_its_leaf_against_the_slides_host() {
let guard = crate::drive::test_support::EnvGuard::take();
let _home = guard.clear_credentials();
let server = MockServer::start().await;
let (drive, _) = mock_clients(&server).await;
std::env::set_var(SLIDES_API_URL, server.uri());
Mock::given(method("GET"))
.and(path("/drive/v3/files/p1"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"id":"p1","name":"Deck","mimeType":crate::drive::types::GOOGLE_SLIDES_MIME_TYPE,"version":"1"
})))
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/v1/presentations/p1"))
.respond_with(ResponseTemplate::new(200).set_body_json(
serde_json::json!({"title":"Deck","revisionId":"r","slides":[{"objectId":"s1"}]}),
))
.mount(&server)
.await;
for args in [
vec!["slides", "info", "p1", "-o", "json"],
vec!["slides", "read", "p1", "-o", "json"],
vec![
"slides",
"replace",
"p1",
"--search",
"a",
"--replace",
"b",
"--dry-run",
"-o",
"json",
],
] {
SlidesCommand::try_parse_from(args)
.unwrap()
.execute(&drive)
.await
.unwrap();
}
let slides_reads = server
.received_requests()
.await
.unwrap()
.iter()
.filter(|r| r.url.path() == "/v1/presentations/p1")
.count();
assert_eq!(slides_reads, 2, "info and read hit the Slides host");
}
}