Skip to main content

defuse_core/
accounts.rs

1use std::{borrow::Cow, collections::BTreeSet};
2
3use serde::{Deserialize, Serialize};
4use serde_with::{base64::Base64, serde_as};
5
6use crate::{AccountIdRef, Nonce, Salt, public_key::PublicKey};
7
8#[must_use = "make sure to `.emit()` this event"]
9#[cfg_attr(feature = "schemars-v0_8", derive(::schemars::JsonSchema))]
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct AccountEvent<'a, T> {
12    pub account_id: Cow<'a, AccountIdRef>,
13
14    #[serde(flatten)]
15    pub event: T,
16}
17
18impl<T> AccountEvent<'_, T> {
19    pub fn into_owned(self) -> AccountEvent<'static, T> {
20        AccountEvent {
21            account_id: Cow::Owned(self.account_id.into_owned()),
22            event: self.event,
23        }
24    }
25}
26
27impl<'a, T> AccountEvent<'a, T> {
28    #[inline]
29    pub fn new(account_id: impl Into<Cow<'a, AccountIdRef>>, event: T) -> Self {
30        Self {
31            account_id: account_id.into(),
32            event,
33        }
34    }
35}
36
37#[must_use = "make sure to `.emit()` this event"]
38#[cfg_attr(feature = "schemars-v0_8", derive(::schemars::JsonSchema))]
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct PublicKeyEvent<'a> {
41    pub public_key: Cow<'a, PublicKey>,
42}
43
44#[serde_as]
45#[cfg_attr(feature = "schemars-v0_8", derive(::schemars::JsonSchema))]
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct NonceEvent {
48    #[serde_as(as = "Base64")]
49    pub nonce: Nonce,
50}
51
52impl NonceEvent {
53    #[inline]
54    pub const fn new(nonce: Nonce) -> Self {
55        Self { nonce }
56    }
57}
58
59#[must_use = "make sure to `.emit()` this event"]
60#[cfg_attr(feature = "schemars-v0_8", derive(::schemars::JsonSchema))]
61#[derive(Serialize, Deserialize, Debug, Clone)]
62pub struct SaltRotationEvent {
63    pub current: Salt,
64    pub invalidated: BTreeSet<Salt>,
65}