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

grade.rs89 lines, 3.4 KiBHistoryRawDownload
1//! The grade ladder: the validated position a class section — and, one layer
2//! up, its course blueprint — occupies on the school's single axis of years.
3//!
4//! It replaces the old free-text `ClassGrade`: "9", "10-A" and "anaokulu" were
5//! three spellings no query could order, group or blueprint against without a
6//! school-wide convention nobody enforced. The ladder is an integer
7//! `0..=12` — `0` is anaokulu, `1..=12` the school years — so "every section
8//! at one grade" is one equality and the blueprint is keyed by the same value
9//! the class carries.
10//!
11//! The wire format is the **integer** itself: every DTO carries
12//! `grade_level: 0..=12`, and the only strings are [`GradeLevel::label`]'s
13//! display/AI-scope labels (`"Anaokulu"`, `"1"`..`"12"`), which are rendered,
14//! never parsed back.
15//!
16//! The bounds live in [`crate::constant`] beside every other published limit
17//! (`GET /limits` serves them); this module owns the type.
18
19use crate::constant::{MAX_GRADE_LEVEL, MIN_GRADE_LEVEL};
20use crate::error::ValidationError;
21
22/// One rung of the grade ladder: `0` = anaokulu, `1..=12` = school years.
23///
24/// `sqlx(transparent)` over the `i16` is what makes the column a plain
25/// `SMALLINT` — the driver checks the type, the `CHECK
26/// (grade_level BETWEEN 0 AND 12)` in the DDL is the stored mirror of
27/// [`GradeLevel::new`], and nothing out of range can be constructed in Rust.
28#[derive(
29 Debug, Clone, Copy, PartialEq, Eq, sqlx::Type, serde::Serialize, serde::Deserialize,
30)]
31#[sqlx(transparent)]
32#[serde(transparent)]
33pub struct GradeLevel(i16);
34
35impl GradeLevel {
36 /// The rung `raw` names, or a validation error when it is off the ladder —
37 /// the `400` both the class and the blueprint write paths answer with.
38 pub fn new(raw: i16) -> Result<Self, ValidationError> {
39 if !(MIN_GRADE_LEVEL..=MAX_GRADE_LEVEL).contains(&raw) {
40 return Err(ValidationError::Invalid {
41 field: "grade_level",
42 reason: "must be 0 (anaokulu) through 12",
43 });
44 }
45 Ok(Self(raw))
46 }
47
48 /// Kindergarten, the ladder's floor.
49 pub fn anaokulu() -> Self {
50 Self(MIN_GRADE_LEVEL)
51 }
52
53 /// The integer the wire and the `grade_level` column carry.
54 pub fn get(self) -> i16 {
55 self.0
56 }
57
58 /// The display label: `"Anaokulu"` at the floor, the year number
59 /// everywhere else. The one spelling the AI scope (`sinif`) and every
60 /// human-facing surface see — an index is safe because the tuple field is
61 /// private and only [`GradeLevel::new`]/[`GradeLevel::anaokulu`] construct.
62 pub fn label(self) -> &'static str {
63 const LABELS: [&str; 13] = [
64 "Anaokulu", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
65 ];
66 LABELS[self.0 as usize]
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn the_ladder_is_bounded() {
76 assert!(GradeLevel::new(MIN_GRADE_LEVEL).is_ok());
77 assert!(GradeLevel::new(MAX_GRADE_LEVEL).is_ok());
78 assert!(GradeLevel::new(-1).is_err());
79 assert!(GradeLevel::new(MAX_GRADE_LEVEL + 1).is_err());
80 }
81
82 #[test]
83 fn anaokulu_is_the_floor_and_labels_are_stable() {
84 assert_eq!(GradeLevel::anaokulu().get(), 0);
85 assert_eq!(GradeLevel::anaokulu().label(), "Anaokulu");
86 assert_eq!(GradeLevel::new(1).unwrap().label(), "1");
87 assert_eq!(GradeLevel::new(12).unwrap().label(), "12");
88 }
89}