stateset_embedded/
prepayments.rs1use stateset_core::{
4 ApplyPrepayment, CreatePrepayment, Prepayment, PrepaymentApplication, PrepaymentApplicationId,
5 PrepaymentFilter, PrepaymentId, Result,
6};
7use stateset_db::{Database, DatabaseCapability};
8use std::sync::Arc;
9
10pub struct Prepayments {
12 db: Arc<dyn Database>,
13}
14
15impl std::fmt::Debug for Prepayments {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 f.debug_struct("Prepayments").finish_non_exhaustive()
18 }
19}
20
21impl Prepayments {
22 pub(crate) fn new(db: Arc<dyn Database>) -> Self {
23 Self { db }
24 }
25
26 #[must_use]
28 pub fn is_supported(&self) -> bool {
29 self.db.supports_capability(DatabaseCapability::Prepayments)
30 }
31
32 fn ensure(&self) -> Result<()> {
33 self.db.ensure_capability(DatabaseCapability::Prepayments)
34 }
35
36 pub fn create(&self, input: CreatePrepayment) -> Result<Prepayment> {
38 self.ensure()?;
39 self.db.prepayments().create(input)
40 }
41
42 pub fn get(&self, id: PrepaymentId) -> Result<Option<Prepayment>> {
44 self.ensure()?;
45 self.db.prepayments().get(id)
46 }
47
48 pub fn list(&self, filter: PrepaymentFilter) -> Result<Vec<Prepayment>> {
50 self.ensure()?;
51 self.db.prepayments().list(filter)
52 }
53
54 pub fn apply(&self, id: PrepaymentId, input: ApplyPrepayment) -> Result<Prepayment> {
56 self.ensure()?;
57 self.db.prepayments().apply(id, input)
58 }
59
60 pub fn list_applications(&self, id: PrepaymentId) -> Result<Vec<PrepaymentApplication>> {
62 self.ensure()?;
63 self.db.prepayments().list_applications(id)
64 }
65
66 pub fn reverse_application(
68 &self,
69 id: PrepaymentId,
70 application_id: PrepaymentApplicationId,
71 ) -> Result<Prepayment> {
72 self.ensure()?;
73 self.db.prepayments().reverse_application(id, application_id)
74 }
75
76 pub fn refund(&self, id: PrepaymentId) -> Result<Prepayment> {
78 self.ensure()?;
79 self.db.prepayments().refund(id)
80 }
81}