backbone_bucket/application/service/
content_hash_service.rs1use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateContentHashDto, UpdateContentHashDto};
11use crate::domain::entity::ContentHash;
12use crate::infrastructure::persistence::ContentHashRepository;
13
14pub type ContentHashService = GenericCrudService<
29 ContentHash,
30 CreateContentHashDto,
31 UpdateContentHashDto,
32 ContentHashRepository,
33>;
34
35pub struct ContentHashRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<ContentHash> for ContentHashRowRules {
43 async fn check(
44 &self,
45 ctx: &backbone_core::write_guard::WriteCtx<'_, ContentHash>,
46 ) -> backbone_core::write_guard::GuardOutcome {
47 use backbone_core::row_rule as r;
48 use backbone_core::violation::Violation;
49 use backbone_core::write_guard::{GuardOutcome, WriteKind};
50 let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
51 let Some(after) = ctx.after.filter(|_| create || update) else {
52 return GuardOutcome::allow();
53 };
54 let Ok(row) = serde_json::to_value(after) else {
55 return GuardOutcome::allow();
56 };
57 let row = &row;
58 let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
59 let shadow = Vec::new();
60 let mut refuse = Vec::new();
61 if (create || (update && r::changed(old.as_ref(), row, &["hash"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "hash")), r::not(r::is_empty(r::trim(r::field(row, "hash")))))) {
63 refuse.push(Violation::new("hash", "required", "hash is required"));
64 }
65 if (create || (update && r::changed(old.as_ref(), row, &["hash"]))) && !r::holds(&r::eq(r::length(r::field(row, "hash")), r::Val::Num(64.0_f64))) {
67 refuse.push(Violation::new("hash", "length", "hash must be exactly 64 characters").with_param("length", 64_i64));
68 }
69 if (create || (update && r::changed(old.as_ref(), row, &["size_bytes"]))) && !r::holds(&r::gt(r::field(row, "size_bytes"), r::Val::Num(0.0_f64))) {
71 refuse.push(Violation::new("size_bytes", "positive", "size_bytes must be positive"));
72 }
73 if (create || (update && r::changed(old.as_ref(), row, &["storage_key"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "storage_key")), r::not(r::is_empty(r::trim(r::field(row, "storage_key")))))) {
75 refuse.push(Violation::new("storage_key", "required", "storage_key is required"));
76 }
77 if (create || (update && r::changed(old.as_ref(), row, &["storage_key"]))) && !r::holds(&r::le(r::length(r::field(row, "storage_key")), r::Val::Num(1024.0_f64))) {
79 refuse.push(Violation::new("storage_key", "max_length", "storage_key must be at most 1024 characters").with_param("max", 1024_i64));
80 }
81 if (create || (update && r::changed(old.as_ref(), row, &["reference_count"]))) && !r::holds(&r::ge(r::field(row, "reference_count"), r::Val::Num(1.0_f64))) {
83 refuse.push(Violation::new("reference_count", "min", "reference_count must be at least 1").with_param("min", 1_i64));
84 }
85 if (create || (update && r::changed(old.as_ref(), row, &["fingerprint"]))) && !r::holds(&r::le(r::length(r::field(row, "fingerprint")), r::Val::Num(128.0_f64))) {
87 refuse.push(Violation::new("fingerprint", "max_length", "fingerprint must be at most 128 characters").with_param("max", 128_i64));
88 }
89 GuardOutcome { refuse, shadow }
90 }
91}
92