runique/migration/hooks/
mod.rs1#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum HookType {
9 BeforeSave,
10 AfterSave,
11 BeforeDelete,
12 AfterDelete,
13}
14
15#[derive(Debug, Clone)]
17pub struct Hook {
18 pub hook_type: HookType,
19 pub slot: u8,
20 pub handler_path: String, }
22
23#[derive(Debug, Clone, Default)]
25pub struct HooksDef {
26 pub hooks: Vec<Hook>,
27 pub file_path: Option<String>, }
29
30impl HooksDef {
31 pub fn new() -> Self {
33 Self::default()
34 }
35
36 pub fn from_file(path: impl Into<String>) -> Self {
38 Self {
39 hooks: Vec::new(),
40 file_path: Some(path.into()),
41 }
42 }
43
44 pub fn add(mut self, hook_type: HookType, slot: u8, handler: impl Into<String>) -> Self {
48 self.hooks.push(Hook {
49 hook_type,
50 slot,
51 handler_path: handler.into(),
52 });
53 self.hooks.sort_by_key(|h| h.slot);
55 self
56 }
57
58 pub fn before_save(self, slot: u8, handler: impl Into<String>) -> Self {
60 self.add(HookType::BeforeSave, slot, handler)
61 }
62
63 pub fn after_save(self, slot: u8, handler: impl Into<String>) -> Self {
65 self.add(HookType::AfterSave, slot, handler)
66 }
67
68 pub fn before_delete(self, slot: u8, handler: impl Into<String>) -> Self {
70 self.add(HookType::BeforeDelete, slot, handler)
71 }
72
73 pub fn after_delete(self, slot: u8, handler: impl Into<String>) -> Self {
75 self.add(HookType::AfterDelete, slot, handler)
76 }
77}