]> git.proxmox.com Git - proxmox-perl-rs.git/blob - pve-rs/src/notify.rs
add PVE::RS::Notify module
[proxmox-perl-rs.git] / pve-rs / src / notify.rs
1 #[perlmod::package(name = "PVE::RS::Notify")]
2 mod export {
3 use anyhow::{bail, Error};
4 use perlmod::Value;
5
6 use std::sync::Mutex;
7
8 use proxmox_notify::Config;
9
10 pub struct NotificationConfig {
11 config: Mutex<Config>,
12 }
13
14 perlmod::declare_magic!(Box<NotificationConfig> : &NotificationConfig as "PVE::RS::Notify");
15
16 /// Support `dclone` so this can be put into the `ccache` of `PVE::Cluster`.
17 #[export(name = "STORABLE_freeze", raw_return)]
18 fn storable_freeze(
19 #[try_from_ref] this: &NotificationConfig,
20 cloning: bool,
21 ) -> Result<Value, Error> {
22 if !cloning {
23 bail!("freezing Notification config not supported!");
24 }
25
26 let mut cloned = Box::new(NotificationConfig {
27 config: Mutex::new(this.config.lock().unwrap().clone()),
28 });
29 let value = Value::new_pointer::<NotificationConfig>(&mut *cloned);
30 let _perl = Box::leak(cloned);
31 Ok(value)
32 }
33
34 /// Instead of `thaw` we implement `attach` for `dclone`.
35 #[export(name = "STORABLE_attach", raw_return)]
36 fn storable_attach(
37 #[raw] class: Value,
38 cloning: bool,
39 #[raw] serialized: Value,
40 ) -> Result<Value, Error> {
41 if !cloning {
42 bail!("STORABLE_attach called with cloning=false");
43 }
44 let data = unsafe { Box::from_raw(serialized.pv_raw::<NotificationConfig>()?) };
45 Ok(perlmod::instantiate_magic!(&class, MAGIC => data))
46 }
47
48 #[export(raw_return)]
49 fn parse_config(
50 #[raw] class: Value,
51 raw_config: &[u8],
52 raw_private_config: &[u8],
53 ) -> Result<Value, Error> {
54 let raw_config = std::str::from_utf8(raw_config)?;
55 let raw_private_config = std::str::from_utf8(raw_private_config)?;
56
57 Ok(perlmod::instantiate_magic!(&class, MAGIC => Box::new(
58 NotificationConfig {
59 config: Mutex::new(Config::new(raw_config, raw_private_config)?)
60 }
61 )))
62 }
63
64 #[export]
65 fn write_config(#[try_from_ref] this: &NotificationConfig) -> Result<(String, String), Error> {
66 Ok(this.config.lock().unwrap().write()?)
67 }
68
69 #[export]
70 fn digest(#[try_from_ref] this: &NotificationConfig) -> String {
71 let config = this.config.lock().unwrap();
72 hex::encode(config.digest())
73 }
74 }