1use crate::data::identifier::DamlIdentifier;
2use crate::grpc_protobuf::com::daml::ledger::api::v1::{Filters, InclusiveFilters, TransactionFilter};
3use std::collections::hash_map::HashMap;
4use std::ops::Not;
5
6#[derive(Debug, Eq, PartialEq, Clone, Default)]
7pub struct DamlFilters {
8 template_ids: Vec<DamlIdentifier>,
9}
10
11impl DamlFilters {
12 pub fn new() -> Self {
13 Self::default()
14 }
15
16 pub fn template_ids(&self) -> &[DamlIdentifier] {
17 &self.template_ids
18 }
19}
20
21impl From<DamlFilters> for Filters {
22 fn from(daml_filters: DamlFilters) -> Self {
23 Filters {
24 inclusive: daml_filters.template_ids.is_empty().not().then(|| InclusiveFilters {
25 template_ids: daml_filters.template_ids.into_iter().map(Into::into).collect(),
26 }),
27 }
28 }
29}
30
31#[derive(Debug, Eq, PartialEq, Clone, Default)]
32pub struct DamlTransactionFilter {
33 filters_by_party: HashMap<String, DamlFilters>,
34}
35
36impl DamlTransactionFilter {
37 pub const fn filters_by_party(&self) -> &HashMap<String, DamlFilters> {
38 &self.filters_by_party
39 }
40
41 pub fn for_parties<P, S>(parties: P) -> Self
42 where
43 P: Into<Vec<S>>,
44 S: Into<String>,
45 {
46 Self {
47 filters_by_party: parties.into().into_iter().map(|p| (p.into(), DamlFilters::new())).collect(),
48 }
49 }
50}
51
52impl From<DamlTransactionFilter> for TransactionFilter {
53 fn from(daml_transaction_filter: DamlTransactionFilter) -> Self {
54 TransactionFilter {
55 filters_by_party: daml_transaction_filter
56 .filters_by_party
57 .into_iter()
58 .map(|(k, v)| (k, Filters::from(v)))
59 .collect(),
60 }
61 }
62}