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

key.rs65 lines, 2.7 KiBHistoryRawDownload
1//! Deterministic wire keys shared by the per-sitting exam tables and the
2//! image-slot tables.
3//
4// With natural composite primary keys these are no longer storage ids — a
5// row is keyed by its component columns. They are the **HTTP edge** form only:
6// when a sitting key or a slot key crosses a path or body, it travels as one
7// underscore-joined string, and these helpers are its single spelling.
8
9/// The wire key for a `(scope, user, seq)` triple, where `scope` is whatever
10/// the row hangs off — an exam for attempts and results, a question for
11/// answers and drawings. The first sitting keeps the historical
12/// `{scope}_{user}` shape — rows written before per-attempt history existed
13/// stay addressable, unchanged, forever — so only later sittings carry their
14/// number. That bare-`seq == 1` case is migration law, not a style choice:
15/// change it and every pre-history row becomes unreachable. UUID strings
16/// contain only `-` and hex, so `_` is an unambiguous joiner.
17pub fn sitting(scope: &str, user: &str, seq: i64) -> String {
18 if seq == 1 {
19 format!("{scope}_{user}")
20 } else {
21 format!("{scope}_{user}_{seq}")
22 }
23}
24
25/// The wire key for one image slot on `owner` — `{owner}_q` for the thing's
26/// own illustration, `{owner}_{choice id}` for one option's picture. A UUID is
27/// never `"q"` and never contains `_`, so the two shapes can never collide —
28/// the rule behind every image table's slot keys, spelled once.
29pub fn slot(owner: &str, slot: Option<&str>) -> String {
30 format!("{owner}_{}", slot.unwrap_or("q"))
31}
32
33#[cfg(test)]
34mod tests {
35 use super::{sitting, slot};
36
37 const QUESTION: &str = "0198f1a2-3b4c-7d5e-8f90-1a2b3c4d5e6f";
38 const STUDENT: &str = "0198f1a2-3b4c-7d5e-8f90-aa2b3c4d5e6f";
39
40 #[test]
41 fn a_slot_key_never_collides_with_the_illustration() {
42 assert_eq!(slot(QUESTION, None), format!("{QUESTION}_q"));
43 assert_eq!(
44 slot(QUESTION, Some(STUDENT)),
45 format!("{QUESTION}_{STUDENT}")
46 );
47 // A choice id is a UUID, so it can never be the literal "q".
48 assert_ne!(slot(QUESTION, Some(STUDENT)), slot(QUESTION, None));
49 }
50
51 #[test]
52 fn the_first_sitting_stays_bare_and_later_ones_are_numbered() {
53 // Byte-for-byte the shape the four sitting-keyed tables wrote before
54 // this helper existed; a pre-history row is keyed by the bare pair.
55 assert_eq!(
56 sitting(QUESTION, STUDENT, 1),
57 format!("{QUESTION}_{STUDENT}")
58 );
59 assert_eq!(
60 sitting(QUESTION, STUDENT, 2),
61 format!("{QUESTION}_{STUDENT}_2")
62 );
63 assert_eq!(sitting("Q", "U", 11), "Q_U_11");
64 }
65}