From abb2e0664b4625becb8f06eafea5abcefee65ac5 Mon Sep 17 00:00:00 2001 From: Clay McLeod Date: Fri, 20 Sep 2024 23:35:37 -0500 Subject: [PATCH] revise: gates serialization/deserialization behind the `serde` feature --- CHANGELOG.md | 1 + Cargo.toml | 11 ++++++--- src/v1/client/tasks.rs | 8 +++---- src/v1/types/responses.rs | 8 +++---- src/v1/types/responses/service.rs | 20 +++++++++------- src/v1/types/responses/task.rs | 11 ++++----- src/v1/types/task.rs | 39 +++++++++++++++---------------- src/v1/types/task/executor.rs | 8 +++---- src/v1/types/task/file.rs | 10 ++++---- 9 files changed, 60 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b073e7..091b840 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Promotes the `v1::types::responses::service` module to public. +- Gates serialization/deserialization behind the `serde` feature. ## 0.2.0 - 08-08-2024 diff --git a/Cargo.toml b/Cargo.toml index a5d4c74..af8231b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,8 @@ ordered-float = { version = "4.2.2", features = ["serde"] } reqwest = { version = "0.12.7", features = ["json"] } reqwest-middleware = "0.3.3" reqwest-retry = "0.6.1" -serde = { version = "1.0.209", features = ["derive"] } -serde_json = "1.0.128" +serde = { version = "1.0.209", features = ["derive"], optional = true } +serde_json = { version = "1.0.128", optional = true } tokio = { version = "1.40.0", features = ["full", "time"] } tracing = "0.1.40" url = { version = "2.5.2", features = ["serde"], optional = true } @@ -28,9 +28,14 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } [features] default = ["types"] -client = ["dep:anyhow", "types", "dep:url"] +client = ["dep:anyhow", "types", "dep:serde_json", "dep:url"] +serde = ["dep:serde", "dep:serde_json"] types = ["dep:url"] +[[example]] +name = "simple" +required-features = ["client"] + [[example]] name = "service-info" required-features = ["client"] diff --git a/src/v1/client/tasks.rs b/src/v1/client/tasks.rs index 26f5b79..ef7bccd 100644 --- a/src/v1/client/tasks.rs +++ b/src/v1/client/tasks.rs @@ -1,13 +1,11 @@ //! Task-related entities used within a client. -use serde::Deserialize; -use serde::Serialize; - /// An argument that affects which fields are returned on certain task-related /// endpoints. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] -#[serde(rename_all = "UPPERCASE")] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "UPPERCASE"))] pub enum View { /// Only includes the `id` and `state` fields in the returned task. #[default] diff --git a/src/v1/types/responses.rs b/src/v1/types/responses.rs index 843aff4..8520e24 100644 --- a/src/v1/types/responses.rs +++ b/src/v1/types/responses.rs @@ -3,19 +3,19 @@ pub mod service; pub mod task; -use serde::Deserialize; -use serde::Serialize; pub use service::ServiceInfo; /// A response from `POST /tasks`. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct CreateTask { /// The ID of the created task. pub id: String, } /// The response from `GET /tasks`. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ListTasks { /// The tasks in this page of results. pub tasks: Vec, diff --git a/src/v1/types/responses/service.rs b/src/v1/types/responses/service.rs index 89af8ab..aafab53 100644 --- a/src/v1/types/responses/service.rs +++ b/src/v1/types/responses/service.rs @@ -2,24 +2,24 @@ use chrono::DateTime; use chrono::Utc; -use serde::Deserialize; -use serde::Serialize; use url::Url; /// Names of specifications supported. /// /// Note that, in the case of the Task Execution Service specification, this can /// only be `"tes"` but it's still technically listed as an enum. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Artifact { /// A task execution service. - #[serde(rename = "tes")] + #[cfg_attr(feature = "serde", serde(rename = "tes"))] #[default] TaskExecutionService, } /// An organization provided a TES service. -#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Organization { /// The organization name. pub name: String, @@ -29,7 +29,8 @@ pub struct Organization { } /// A type of service. -#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ServiceType { /// Namespace in reverse domain name format. pub group: String, @@ -42,8 +43,9 @@ pub struct ServiceType { } /// A set of service information for the server. -#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(rename_all = "camelCase")] +#[derive(Debug, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct ServiceInfo { /// A unique identifier for the service. pub id: String, @@ -90,6 +92,7 @@ mod tests { use super::*; + #[cfg(feature = "serde")] #[test] fn smoke() { let content = r#"{ @@ -151,6 +154,7 @@ mod tests { ); } + #[cfg(feature = "serde")] #[test] fn full_conversion() { let now = DateTime::parse_from_rfc3339("2024-09-07T20:27:35.345673Z") diff --git a/src/v1/types/responses/task.rs b/src/v1/types/responses/task.rs index 662106b..0c14b79 100644 --- a/src/v1/types/responses/task.rs +++ b/src/v1/types/responses/task.rs @@ -1,13 +1,11 @@ //! Responses related to tasks. -use serde::Deserialize; -use serde::Serialize; - use crate::v1::types::task::State; use crate::v1::types::Task; /// A response for when `?view=MINIMAL` in a task endpoint. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct MinimalTask { /// The ID. pub id: String, @@ -17,8 +15,9 @@ pub struct MinimalTask { } /// A generalized response for getting tasks with the `view` parameter. -#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] -#[serde(untagged)] +#[derive(Debug, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(untagged))] pub enum Response { /// A response for when `?view=MINIMAL` in a task endpoint. Minimal(MinimalTask), diff --git a/src/v1/types/task.rs b/src/v1/types/task.rs index 47909e9..d70e476 100644 --- a/src/v1/types/task.rs +++ b/src/v1/types/task.rs @@ -5,8 +5,6 @@ use std::collections::HashMap; use chrono::DateTime; use chrono::Utc; use ordered_float::OrderedFloat; -use serde::Deserialize; -use serde::Serialize; pub mod executor; pub mod file; @@ -14,43 +12,38 @@ pub mod file; pub use executor::Executor; /// State of TES task. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "UPPERCASE"))] pub enum State { /// An unknown state. - #[serde(rename = "UNKNOWN")] #[default] Unknown, /// A queued task. - #[serde(rename = "QUEUED")] Queued, /// A task that is initializing. - #[serde(rename = "INITIALIZING")] Initializing, /// A task that is running. - #[serde(rename = "RUNNING")] Running, /// A task that is paused. - #[serde(rename = "PAUSED")] Paused, /// A task that has completed. - #[serde(rename = "COMPLETE")] Complete, /// A task that has errored during execution. - #[serde(rename = "EXECUTOR_ERROR")] + #[cfg_attr(feature = "serde", serde(rename = "EXECUTOR_ERROR"))] ExecutorError, /// A task that has encountered a system error. - #[serde(rename = "SYSTEM_ERROR")] + #[cfg_attr(feature = "serde", serde(rename = "SYSTEM_ERROR"))] SystemError, /// A task that has been cancelled. - #[serde(rename = "CANCELED")] Canceled, } @@ -65,7 +58,8 @@ impl State { } /// An input for a TES task. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Input { /// An optional name. pub name: Option, @@ -80,7 +74,7 @@ pub struct Input { pub path: String, /// The type. - #[serde(rename = "type")] + #[cfg_attr(feature = "serde", serde(rename = "type"))] pub r#type: file::Type, /// The content. @@ -88,7 +82,8 @@ pub struct Input { } /// An output for a TES task. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Output { /// An optional name. pub name: Option, @@ -103,12 +98,13 @@ pub struct Output { pub path: String, /// The type. - #[serde(rename = "type")] + #[cfg_attr(feature = "serde", serde(rename = "type"))] pub r#type: file::Type, } /// Requested resources for a TES task. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Resources { /// The number of CPU cores. pub cpu_cores: Option, @@ -127,7 +123,8 @@ pub struct Resources { } /// An output file log. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct OutputFileLog { /// The URL. pub url: String, @@ -140,7 +137,8 @@ pub struct OutputFileLog { } /// A task log. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct TaskLog { /// The executor logs. pub logs: Vec, @@ -159,7 +157,8 @@ pub struct TaskLog { } /// A task. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Task { /// The ID. pub id: Option, diff --git a/src/v1/types/task/executor.rs b/src/v1/types/task/executor.rs index 794fd9f..a27ed6f 100644 --- a/src/v1/types/task/executor.rs +++ b/src/v1/types/task/executor.rs @@ -4,15 +4,14 @@ use std::collections::HashMap; use chrono::DateTime; use chrono::Utc; -use serde::Deserialize; -use serde::Serialize; /// An executor. /// /// In short, an executor is a single command that is run in a different /// container image. [`Executor`]s are run sequentially as they are specified in /// the task. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Executor { /// The image. pub image: String, @@ -37,7 +36,8 @@ pub struct Executor { } /// A log for an [`Executor`]. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Log { /// The start time. pub start_time: Option>, diff --git a/src/v1/types/task/file.rs b/src/v1/types/task/file.rs index f4b67f0..00059b5 100644 --- a/src/v1/types/task/file.rs +++ b/src/v1/types/task/file.rs @@ -1,17 +1,15 @@ //! Files declared within tasks. -use serde::Deserialize; -use serde::Serialize; - /// A type of file. -#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Debug, Default, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Type { /// A file. - #[serde(rename = "FILE")] + #[cfg_attr(feature = "serde", serde(rename = "FILE"))] #[default] File, /// A directory. - #[serde(rename = "DIRECTORY")] + #[cfg_attr(feature = "serde", serde(rename = "DIRECTORY"))] Directory, }