Skip to content

Commit

Permalink
Merge pull request #61 from saschagrunert/builder-default
Browse files Browse the repository at this point in the history
Make builder pattern the new default
  • Loading branch information
Furisto authored Sep 6, 2021
2 parents a7b03bd + 44f6d4a commit f9a74af
Show file tree
Hide file tree
Showing 20 changed files with 2,068 additions and 2,639 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ categories = ["api-bindings"]
[features]
default = ["distribution", "image", "runtime"]
proptests = ["quickcheck"]
builder = ["derive_builder", "getset"]
distribution = []
image = []
runtime = []
Expand All @@ -29,8 +28,8 @@ serde = { version = "1.0.129", features = ["derive"] }
thiserror = "1.0.26"
serde_json = "1.0.66"
quickcheck = { version = "1.0.3", optional = true }
derive_builder = { version = "0.10.2", optional = true }
getset = { version = "0.1.1", optional = true }
derive_builder = "0.10.2"
getset = "0.1.1"

[dev-dependencies]
tempfile = "3.2.0"
Expand Down
85 changes: 38 additions & 47 deletions src/distribution/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Error types of the distribution spec.
use crate::error::OciSpecError;
use derive_builder::Builder;
use getset::Getters;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use thiserror::Error;
Expand Down Expand Up @@ -42,23 +45,18 @@ pub enum ErrorCode {
TooManyRequests,
}

make_pub!(
#[derive(Clone, Debug, Deserialize, Eq, Error, PartialEq, Serialize)]
#[cfg_attr(
feature = "builder",
derive(derive_builder::Builder),
builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "crate::error::OciSpecError")
)
)]
/// ErrorResponse is returned by a registry on an invalid request.
struct ErrorResponse {
/// Available errors within the response.
errors: Vec<ErrorInfo>,
}
);
#[derive(Builder, Clone, Debug, Deserialize, Eq, Error, Getters, PartialEq, Serialize)]
#[builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get = "pub")]
/// ErrorResponse is returned by a registry on an invalid request.
pub struct ErrorResponse {
/// Available errors within the response.
errors: Vec<ErrorInfo>,
}

impl Display for ErrorResponse {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Expand All @@ -73,40 +71,33 @@ impl ErrorResponse {
}
}

make_pub!(
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(
feature = "builder",
derive(derive_builder::Builder, getset::Getters),
builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "crate::error::OciSpecError")
),
getset(get = "pub")
)]
/// Describes a server error returned from a registry.
struct ErrorInfo {
/// The code field MUST be a unique identifier, containing only uppercase alphabetic
/// characters and underscores.
code: ErrorCode,
#[derive(Builder, Clone, Debug, Deserialize, Eq, Getters, PartialEq, Serialize)]
#[builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get = "pub")]
/// Describes a server error returned from a registry.
pub struct ErrorInfo {
/// The code field MUST be a unique identifier, containing only uppercase alphabetic
/// characters and underscores.
code: ErrorCode,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "builder", builder(default = "None"))]
/// The message field is OPTIONAL, and if present, it SHOULD be a human readable string or
/// MAY be empty.
message: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[builder(default = "None")]
/// The message field is OPTIONAL, and if present, it SHOULD be a human readable string or
/// MAY be empty.
message: Option<String>,

#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "builder", builder(default = "None"))]
/// The detail field is OPTIONAL and MAY contain arbitrary JSON data providing information
/// the client can use to resolve the issue.
detail: Option<String>,
}
);
#[serde(default, skip_serializing_if = "Option::is_none")]
#[builder(default = "None")]
/// The detail field is OPTIONAL and MAY contain arbitrary JSON data providing information
/// the client can use to resolve the issue.
detail: Option<String>,
}

#[cfg(test)]
#[cfg(feature = "builder")]
mod tests {
use super::*;
use crate::error::Result;
Expand Down
34 changes: 15 additions & 19 deletions src/distribution/repository.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
//! Repository types of the distribution spec.
use crate::error::OciSpecError;
use derive_builder::Builder;
use getset::Getters;
use serde::{Deserialize, Serialize};

make_pub!(
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(
feature = "builder",
derive(derive_builder::Builder, getset::Getters),
builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "crate::error::OciSpecError")
),
getset(get = "pub")
)]
/// RepositoryList returns a catalog of repositories maintained on the registry.
struct RepositoryList {
/// The items of the RepositoryList.
repositories: Vec<String>,
}
);
#[derive(Builder, Clone, Debug, Deserialize, Eq, Getters, PartialEq, Serialize)]
#[builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get = "pub")]
/// RepositoryList returns a catalog of repositories maintained on the registry.
pub struct RepositoryList {
/// The items of the RepositoryList.
repositories: Vec<String>,
}

#[cfg(test)]
#[cfg(feature = "builder")]
mod tests {
use super::*;
use crate::error::Result;
Expand Down
38 changes: 17 additions & 21 deletions src/distribution/tag.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
//! Tag types of the distribution spec.
use crate::error::OciSpecError;
use derive_builder::Builder;
use getset::Getters;
use serde::{Deserialize, Serialize};

make_pub!(
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(
feature = "builder",
derive(derive_builder::Builder, getset::Getters),
builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "crate::error::OciSpecError")
),
getset(get = "pub")
)]
/// A list of tags for a given repository.
struct TagList {
/// The namespace of the repository.
name: String,
#[derive(Builder, Clone, Debug, Deserialize, Eq, Getters, PartialEq, Serialize)]
#[builder(
pattern = "owned",
setter(into, strip_option),
build_fn(error = "OciSpecError")
)]
#[getset(get = "pub")]
/// A list of tags for a given repository.
pub struct TagList {
/// The namespace of the repository.
name: String,

/// Each tags on the repository.
tags: Vec<String>,
}
);
/// Each tags on the repository.
tags: Vec<String>,
}

#[cfg(test)]
#[cfg(feature = "builder")]
mod tests {
use super::*;
use crate::error::Result;
Expand Down
1 change: 0 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub enum OciSpecError {
SerDe(#[from] serde_json::Error),

/// Builder specific errors.
#[cfg(feature = "builder")]
#[error("uninitialized field")]
Builder(#[from] derive_builder::UninitializedFieldError),
}
Expand Down
Loading

0 comments on commit f9a74af

Please sign in to comment.