Skip to content

Commit

Permalink
Implement Default
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb authored and idubrov committed Mar 27, 2024
1 parent 1e6ae7b commit 5671977
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/lib.rs
Expand Up @@ -127,7 +127,7 @@ macro_rules! impl_display {
}

/// Representation of JSON Patch (list of patch operations)
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct Patch(pub Vec<PatchOperation>);

Expand All @@ -142,7 +142,7 @@ impl std::ops::Deref for Patch {
}

/// JSON Patch 'add' operation representation
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct AddOperation {
/// JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location
Expand All @@ -156,7 +156,7 @@ pub struct AddOperation {
impl_display!(AddOperation);

/// JSON Patch 'remove' operation representation
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct RemoveOperation {
/// JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location
Expand All @@ -168,7 +168,7 @@ pub struct RemoveOperation {
impl_display!(RemoveOperation);

/// JSON Patch 'replace' operation representation
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReplaceOperation {
/// JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location
Expand All @@ -182,7 +182,7 @@ pub struct ReplaceOperation {
impl_display!(ReplaceOperation);

/// JSON Patch 'move' operation representation
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct MoveOperation {
/// JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location
Expand All @@ -198,7 +198,7 @@ pub struct MoveOperation {
impl_display!(MoveOperation);

/// JSON Patch 'copy' operation representation
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct CopyOperation {
/// JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location
Expand All @@ -214,7 +214,7 @@ pub struct CopyOperation {
impl_display!(CopyOperation);

/// JSON Patch 'test' operation representation
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct TestOperation {
/// JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location
Expand Down Expand Up @@ -263,6 +263,12 @@ impl PatchOperation {
}
}

impl Default for PatchOperation {
fn default() -> Self {
PatchOperation::Test(TestOperation::default())
}
}

/// This type represents all possible errors that can occur when applying JSON patch
#[derive(Debug, Error)]
#[non_exhaustive]
Expand Down
12 changes: 12 additions & 0 deletions tests/basic.rs
Expand Up @@ -209,3 +209,15 @@ fn display_patch() {
]"#
);
}

#[test]
fn display_patch_default() {
let patch = Patch::default();
assert_eq!(patch.to_string(), r#"[]"#);
}

#[test]
fn display_patch_operation_default() {
let op = PatchOperation::default();
assert_eq!(op.to_string(), r#"{"op":"test","path":"","value":null}"#);
}

0 comments on commit 5671977

Please sign in to comment.