From ada4a7733c090c564b75c116c9d88781a19a84e9 Mon Sep 17 00:00:00 2001 From: Scott Lyons Date: Sun, 11 Aug 2024 13:36:39 -0700 Subject: [PATCH] fix(models): fixing parsing of snapshot data --- .pre-commit-config.yaml | 5 ++ szurubooru-client/src/lib.rs | 2 - szurubooru-client/src/models.rs | 109 +++++++++++++++++++++++++++++--- 3 files changed, 104 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 347469d..5f34df0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,6 +13,11 @@ repos: hooks: - id: fmt - id: cargo-check + stages: [pre-push] + - id: build + stages: [pre-push] + - id: test + stages: [pre-push] - repo: https://github.com/compilerla/conventional-pre-commit rev: v3.4.0 hooks: diff --git a/szurubooru-client/src/lib.rs b/szurubooru-client/src/lib.rs index 40d504b..e6eddf9 100644 --- a/szurubooru-client/src/lib.rs +++ b/szurubooru-client/src/lib.rs @@ -35,6 +35,4 @@ pub mod errors; pub use errors::SzurubooruResult; pub mod models; -#[cfg(test)] -mod tests; pub mod tokens; diff --git a/szurubooru-client/src/models.rs b/szurubooru-client/src/models.rs index 6a5b8d6..49ed2e2 100644 --- a/szurubooru-client/src/models.rs +++ b/szurubooru-client/src/models.rs @@ -890,6 +890,9 @@ pub enum SnapshotResourceType { Post, /// Pool resource Pool, + /// Pool Category + #[serde(rename = "pool_category")] + PoolCategory, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -905,6 +908,8 @@ pub enum SnapshotCreationDeletionData { Post(PostResource), /// A pool resource that was created Pool(PoolResource), + /// A pool category resource that was created + PoolCategory(PoolCategoryResource), } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -922,13 +927,6 @@ pub struct SnapshotModificationData { pub value: serde_json::Value, } -#[derive(Debug, Clone, Serialize, Deserialize)] -/// Data for a merged resource -pub struct SnapshotMergeData { - /// Resource IDs that have been merged - pub merged: Vec, -} - #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(untagged)] /// Type representing the data as part of a snapshot @@ -939,7 +937,7 @@ pub enum SnapshotData { /// Data for a modified resource Modify(SnapshotModificationData), /// Data for a merged resource - Merge(SnapshotMergeData), + Merge(Vec), } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -952,7 +950,7 @@ pub struct SnapshotResource { /// The resource type pub resource_type: Option, /// The ID of the snapshot itself - pub id: Option, + pub id: Option, /// The user who created this change pub user: Option, /// The data associated with this resource change @@ -994,7 +992,7 @@ pub struct AroundPostResult { #[cfg(test)] mod tests { - use crate::models::{GlobalInfo, GlobalInfoConfig, TagCategoryResource}; + use crate::models::{GlobalInfo, GlobalInfoConfig, SnapshotResource, TagCategoryResource}; use chrono::Datelike; #[test] @@ -1065,4 +1063,95 @@ mod tests { .expect("Unable to parse tag category string"); assert_eq!(tag_cat.name, Some("default".to_string())); } + + #[test] + fn test_parse_snapshot() { + let input_str = r#" + { + "operation": "merged", + "type": "pool", + "id": "2", + "user": { + "name": "integration_user", + "avatarUrl": "https://gravatar.com/avatar/6ab25d2babacc114ca560bff7c264d08?d=retro&s=300" + }, + "data": [ + "pool", + 1 + ], + "time": "2024-08-11T19:53:34.384644Z" + } + "#; + serde_json::from_str::(input_str) + .expect("Could not parse merged snapshot resource"); + + let input_str = r#" + { + "operation": "modified", + "type": "pool_category", + "id": "cat_pool_category", + "user": { + "name": "integration_user", + "avatarUrl": "https://gravatar.com/avatar/6ab25d2babacc114ca560bff7c264d08?d=retro&s=300" + }, + "data": { + "type": "object change", + "value": { + "default": { + "type": "primitive change", + "old-value": false, + "new-value": true + } + } + }, + "time": "2024-08-11T19:53:33.422437Z" + } + "#; + serde_json::from_str::(input_str) + .expect("Could not parse modified snapshot resource"); + + let input_str = r#" + { + "operation": "deleted", + "type": "pool", + "id": "3", + "user": { + "name": "integration_user", + "avatarUrl": "https://gravatar.com/avatar/6ab25d2babacc114ca560bff7c264d08?d=retro&s=300" + }, + "data": { + "names": [ + "dogs_pool" + ], + "category": "cat_pool_category", + "posts": [] + }, + "time": "2024-08-11T19:53:34.006828Z" + } + "#; + serde_json::from_str::(input_str) + .expect("Could not parse deleted snapshot resource"); + + let input_str = r#" + { + "operation": "created", + "type": "pool", + "id": "1", + "user": { + "name": "integration_user", + "avatarUrl": "https://gravatar.com/avatar/6ab25d2babacc114ca560bff7c264d08?d=retro&s=300" + }, + "data": { + "names": [ + "cats_pool" + ], + "category": "cat_pool_category", + "posts": [] + }, + "time": "2024-08-11T19:53:33.613959Z" + } + "#; + serde_json::from_str::(input_str) + .expect("Could not parse created snapshot resource"); + } }