use serde::{Deserialize, Serialize};
use super::common::{Gid, ResourceRef, UserRef};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Story {
pub gid: Gid,
pub created_at: Option<String>,
pub created_by: Option<UserRef>,
pub resource_subtype: Option<StoryType>,
pub text: Option<String>,
pub html_text: Option<String>,
#[serde(default)]
pub is_pinned: bool,
#[serde(default)]
pub is_edited: bool,
pub target: Option<ResourceRef>,
#[serde(default)]
pub num_likes: u32,
#[serde(default)]
pub liked: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StoryType {
CommentAdded,
AssignedTo,
Unassigned,
DueDateChanged,
MarkedComplete,
MarkedIncomplete,
AddedToProject,
RemovedFromProject,
SectionChanged,
NameChanged,
NotesChanged,
AttachmentAdded,
TagAdded,
TagRemoved,
SubtaskAdded,
DependencyAdded,
DependentAdded,
Duplicated,
#[serde(other)]
Unknown,
}
impl Story {
pub fn is_comment(&self) -> bool {
matches!(self.resource_subtype, Some(StoryType::CommentAdded))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_deserialize_story_comment() {
let json = r#"{
"gid": "123",
"created_at": "2024-01-15T10:30:00.000Z",
"resource_subtype": "comment_added",
"text": "This is a comment",
"is_pinned": false
}"#;
let story: Story = serde_json::from_str(json).unwrap();
assert_eq!(story.gid, "123");
assert!(story.is_comment());
assert_eq!(story.text, Some("This is a comment".to_string()));
}
#[test]
fn test_deserialize_story_system() {
let json = r#"{
"gid": "456",
"resource_subtype": "marked_complete",
"text": "John Doe completed this task"
}"#;
let story: Story = serde_json::from_str(json).unwrap();
assert!(!story.is_comment());
assert_eq!(story.resource_subtype, Some(StoryType::MarkedComplete));
}
#[test]
fn test_deserialize_story_unknown_type() {
let json = r#"{
"gid": "789",
"resource_subtype": "some_new_type",
"text": "Some activity"
}"#;
let story: Story = serde_json::from_str(json).unwrap();
assert_eq!(story.resource_subtype, Some(StoryType::Unknown));
}
#[test]
fn test_story_type_variants() {
let assigned: StoryType = serde_json::from_str(r#""assigned_to""#).unwrap();
assert_eq!(assigned, StoryType::AssignedTo);
let completed: StoryType = serde_json::from_str(r#""marked_complete""#).unwrap();
assert_eq!(completed, StoryType::MarkedComplete);
}
}