Skip to content

Commit

Permalink
revise: gates serialization/deserialization behind the serde feature
Browse files Browse the repository at this point in the history
  • Loading branch information
claymcleod committed Sep 21, 2024
1 parent ba4e7e5 commit abb2e06
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
Expand Down
8 changes: 3 additions & 5 deletions src/v1/client/tasks.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
8 changes: 4 additions & 4 deletions src/v1/types/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Task> {
/// The tasks in this page of results.
pub tasks: Vec<Task>,
Expand Down
20 changes: 12 additions & 8 deletions src/v1/types/responses/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -90,6 +92,7 @@ mod tests {

use super::*;

#[cfg(feature = "serde")]
#[test]
fn smoke() {
let content = r#"{
Expand Down Expand Up @@ -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")
Expand Down
11 changes: 5 additions & 6 deletions src/v1/types/responses/task.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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),
Expand Down
39 changes: 19 additions & 20 deletions src/v1/types/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,45 @@ 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;

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,
}

Expand All @@ -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<String>,
Expand All @@ -80,15 +74,16 @@ 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.
pub content: Option<String>,
}

/// 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<String>,
Expand All @@ -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<i64>,
Expand All @@ -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,
Expand All @@ -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<executor::Log>,
Expand All @@ -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<String>,
Expand Down
8 changes: 4 additions & 4 deletions src/v1/types/task/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<DateTime<Utc>>,
Expand Down
10 changes: 4 additions & 6 deletions src/v1/types/task/file.rs
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit abb2e06

Please sign in to comment.