Skip to main content

tilo_resource/
origin.rs

1//! Where content came from, when it was written somewhere else first.
2//!
3//! Content moved into a workspace (an issue from another tracker, a page
4//! from another wiki, a message from another chat) was written by someone
5//! there, at a time, and has a page there. An action that writes such
6//! content takes an [`Origin`] beside its other arguments and keeps it with
7//! the item, next to who wrote the item here: the record keeps both. A view
8//! shows the item as by its original author in its source, on its original
9//! date, and the record's own history still says who moved it and when.
10//!
11//! The original author is a name and a handle at the source, never a member
12//! of the workspace: someone named in an old tracker need not have joined.
13
14use serde::{Deserialize, Serialize};
15
16use crate::{Model, Refusal};
17
18/// The longest name, handle, source or link an origin keeps.
19pub const MAX_ORIGIN_CHARS: usize = 2048;
20
21/// Where content was first written, before it came into the workspace.
22#[derive(
23    Model, Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, crate::JsonSchema,
24)]
25pub struct Origin {
26    /// Its author there, by name (`Jane Doe`).
27    pub author: String,
28    /// Its author's handle there (`jane`, `jane@example.com`), when it has
29    /// one.
30    #[serde(default)]
31    pub handle: Option<String>,
32    /// The tool it was written in (`Jira`, `GitHub`, `Notion`).
33    pub source: String,
34    /// Its page there.
35    #[serde(default)]
36    pub url: Option<String>,
37    /// How it is named there, when the tool gives it a name of its own
38    /// (`ENG-12`, `#431`): what people who knew it there look for.
39    #[serde(default)]
40    pub reference: Option<String>,
41    /// When it was written there, in milliseconds since the epoch.
42    #[serde(default)]
43    pub created_ms: Option<i64>,
44    /// When it last changed there, in milliseconds since the epoch.
45    #[serde(default)]
46    pub updated_ms: Option<i64>,
47}
48
49impl Origin {
50    /// The origin as it is kept: its texts trimmed, blank optional ones
51    /// dropped.
52    ///
53    /// # Errors
54    /// Refuses an origin without an author or a source, a text too long to
55    /// keep, or a last change before its creation.
56    pub fn checked(self) -> Result<Self, Refusal> {
57        let trim = |text: String| text.trim().to_owned();
58        let optional = |text: Option<String>| text.map(trim).filter(|text| !text.is_empty());
59        let origin = Self {
60            author: trim(self.author),
61            handle: optional(self.handle),
62            source: trim(self.source),
63            url: optional(self.url),
64            reference: optional(self.reference),
65            created_ms: self.created_ms,
66            updated_ms: self.updated_ms,
67        };
68        if origin.author.is_empty() {
69            return Err(Refusal::new("An origin names who wrote it there"));
70        }
71        if origin.source.is_empty() {
72            return Err(Refusal::new("An origin names the tool it was written in"));
73        }
74        let texts = [
75            Some(&origin.author),
76            origin.handle.as_ref(),
77            Some(&origin.source),
78            origin.url.as_ref(),
79            origin.reference.as_ref(),
80        ];
81        if texts
82            .into_iter()
83            .flatten()
84            .any(|text| text.chars().count() > MAX_ORIGIN_CHARS)
85        {
86            return Err(Refusal::new("An origin's texts fit on a line"));
87        }
88        if let (Some(created), Some(updated)) = (origin.created_ms, origin.updated_ms)
89            && updated < created
90        {
91            return Err(Refusal::new(
92                "An origin's last change is not before it was written",
93            ));
94        }
95        Ok(origin)
96    }
97
98    /// [`Self::checked`] of an optional origin.
99    ///
100    /// # Errors
101    /// As [`Self::checked`].
102    pub fn checked_option(origin: Option<Self>) -> Result<Option<Self>, Refusal> {
103        origin.map(Self::checked).transpose()
104    }
105}