tablo_core/resource/
commit.rs1use topcoat::context::Cx;
4
5use super::{Action, Resource};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum Mutation {
11 Create,
12 Update,
13 Delete,
14 Action(&'static str),
17 Attach,
19 Detach,
21}
22
23#[derive(Debug, Clone)]
25pub struct Committed<M> {
26 mutation: Mutation,
27 records: Vec<M>,
28}
29
30impl<M> Committed<M> {
31 pub fn created(record: M) -> Self {
33 Self {
34 mutation: Mutation::Create,
35 records: vec![record],
36 }
37 }
38
39 pub fn updated(record: M) -> Self {
41 Self {
42 mutation: Mutation::Update,
43 records: vec![record],
44 }
45 }
46
47 pub fn deleted(records: Vec<M>) -> Self {
49 Self {
50 mutation: Mutation::Delete,
51 records,
52 }
53 }
54
55 pub fn acted<R: Resource<Model = M>, A: Action<R>>(records: Vec<M>) -> Self {
57 Self {
58 mutation: Mutation::Action(A::NAME),
59 records,
60 }
61 }
62
63 pub(crate) fn attached(owner: M) -> Self {
65 Self {
66 mutation: Mutation::Attach,
67 records: vec![owner],
68 }
69 }
70
71 pub(crate) fn detached(owner: M) -> Self {
73 Self {
74 mutation: Mutation::Detach,
75 records: vec![owner],
76 }
77 }
78
79 pub(crate) fn ran(name: &'static str) -> Self {
81 Self {
82 mutation: Mutation::Action(name),
83 records: Vec::new(),
84 }
85 }
86
87 pub fn mutation(&self) -> Mutation {
89 self.mutation
90 }
91
92 pub fn records(&self) -> &[M] {
94 &self.records
95 }
96}
97
98pub(crate) async fn run_after_commit<R: Resource>(cx: &Cx, committed: Committed<R::Model>) {
102 if let Err(error) = R::after_commit(cx, committed).await {
103 tracing::error!(
104 error = %error,
105 resource = std::any::type_name::<R>(),
106 "after_commit failed; the write stays committed"
107 );
108 }
109}