forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

note.rs113 lines, 3.1 KiBHistoryRawDownload
1use uuid::Uuid;
2
3use crate::constant::{MAX_NOTE_CONTENT_LEN, MAX_NOTE_TITLE_LEN};
4use crate::domain::monotonic_id::next_uuid;
5use crate::domain::user::UserId;
6use crate::error::ValidationError;
7use crate::validate::{validate_optional, validate_required};
8
9/// Typed note row id. A UUIDv7 minted by the process-wide monotonic
10/// generator, so `id` order is mint order.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, sqlx::Type)]
12#[sqlx(transparent)]
13pub struct NoteId(Uuid);
14
15impl NoteId {
16 /// Minted from the process-wide monotonic generator, not a random v4:
17 /// notes list `id DESC` (newest first, [`crate::db::note::list_for`]),
18 /// and a random low half scrambles rows minted in the same millisecond.
19 pub fn generate() -> Self {
20 Self(next_uuid())
21 }
22
23 /// The inner uuid, for runtime-checked binds (Param/QueryBuilder) that
24 /// cannot take the newtype. Static `query!` binds take `self` directly.
25 pub fn uuid(&self) -> Uuid {
26 self.0
27 }
28
29 /// Parse a wire key. A key that parses as no UUID — a malformed path
30 /// segment — reads as the nil id, which matches no row: exactly the 404 a
31 /// dangling record key produced under the old store, without turning a
32 /// typo into a panic.
33 pub fn from_key(key: &str) -> Self {
34 Self(Uuid::parse_str(key).unwrap_or(Uuid::nil()))
35 }
36
37 /// The hyphenated wire form.
38 pub fn key(&self) -> String {
39 self.0.to_string()
40 }
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
44#[sqlx(transparent)]
45pub struct NoteTitle(String);
46
47impl NoteTitle {
48 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
49 validate_required("title", value, MAX_NOTE_TITLE_LEN)?;
50 Ok(Self(value.to_string()))
51 }
52
53 pub fn as_str(&self) -> &str {
54 &self.0
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq, sqlx::Type)]
59#[sqlx(transparent)]
60pub struct NoteContent(String);
61
62impl NoteContent {
63 pub fn try_new(value: &str) -> Result<Self, ValidationError> {
64 validate_optional("content", value, MAX_NOTE_CONTENT_LEN)?;
65 Ok(Self(value.to_string()))
66 }
67
68 pub fn as_str(&self) -> &str {
69 &self.0
70 }
71}
72
73/// Fields are crate-visible: [`crate::db::note`] mints the rows on create and
74/// reads the id when updating and cascading a delete.
75#[derive(Debug, Clone, sqlx::FromRow)]
76pub struct Note {
77 pub(crate) id: NoteId,
78 #[sqlx(rename = "app_user")]
79 pub(crate) user: UserId,
80 pub(crate) title: NoteTitle,
81 pub(crate) content: NoteContent,
82}
83
84impl Note {
85 pub fn get_id(&self) -> &NoteId {
86 &self.id
87 }
88
89 pub fn get_title(&self) -> &NoteTitle {
90 &self.title
91 }
92
93 pub fn get_content(&self) -> &NoteContent {
94 &self.content
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[tokio::test]
103 async fn title_is_required() {
104 assert_eq!(NoteTitle::try_new("hi").unwrap().as_str(), "hi");
105 assert!(NoteTitle::try_new(" ").is_err());
106 }
107
108 #[tokio::test]
109 async fn content_is_optional() {
110 assert_eq!(NoteContent::try_new("").unwrap().as_str(), "");
111 assert_eq!(NoteContent::try_new("body").unwrap().as_str(), "body");
112 }
113}