1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Defines the context for [`Annotation`] data.

use std::collections::BTreeSet;

use serde::Serialize;

use crate::models::annotation::{Annotation, AnnotationMetadata};

/// A struct representing an [`Annotation`] within a template context.
///
/// See [`Annotation`] for undocumented fields.
#[derive(Debug, Serialize)]
pub struct AnnotationContext<'a> {
    #[allow(missing_docs)]
    pub body: &'a str,
    #[allow(missing_docs)]
    pub style: &'a str,
    #[allow(missing_docs)]
    pub notes: &'a str,
    #[allow(missing_docs)]
    pub tags: &'a BTreeSet<String>,
    #[allow(missing_docs)]
    pub metadata: &'a AnnotationMetadata,

    /// An [`Annotation`]s slugified strings.
    pub slugs: AnnotationSlugs,
}

impl<'a> From<&'a Annotation> for AnnotationContext<'a> {
    fn from(annotation: &'a Annotation) -> Self {
        Self {
            body: &annotation.body,
            style: &annotation.style,
            notes: &annotation.notes,
            tags: &annotation.tags,
            metadata: &annotation.metadata,
            slugs: AnnotationSlugs {
                metadata: AnnotationMetadataSlugs {
                    created: crate::utils::to_slug_date(&annotation.metadata.created),
                    modified: crate::utils::to_slug_date(&annotation.metadata.modified),
                },
            },
        }
    }
}

/// A struct representing an [`Annotation`]'s slugified strings.
#[derive(Debug, Serialize)]
pub struct AnnotationSlugs {
    #[allow(missing_docs)]
    metadata: AnnotationMetadataSlugs,
}

/// A struct representing an [`AnnotationMetadata`]'s slugified strings.
///
/// See [`AnnotationMetadata`] for undocumented fields.
#[derive(Debug, Serialize)]
pub struct AnnotationMetadataSlugs {
    #[allow(missing_docs)]
    created: String,
    #[allow(missing_docs)]
    modified: String,
}