Skip to main content

kmp_application/projection/
routing_projection_writer.rs

1use kmp_domain::{PortError, ProjectionMutation, ProjectionWriter};
2
3#[derive(Debug)]
4pub struct RoutingProjectionWriter<G, D> {
5    graph_writer: G,
6    detail_writer: D,
7}
8
9impl<G, D> RoutingProjectionWriter<G, D>
10where
11    G: ProjectionWriter + Send + Sync,
12    D: ProjectionWriter + Send + Sync,
13{
14    pub fn new(graph_writer: G, detail_writer: D) -> Self {
15        Self {
16            graph_writer,
17            detail_writer,
18        }
19    }
20}
21
22impl<G, D> ProjectionWriter for RoutingProjectionWriter<G, D>
23where
24    G: ProjectionWriter + Send + Sync,
25    D: ProjectionWriter + Send + Sync,
26{
27    async fn apply_mutations(&self, mutations: Vec<ProjectionMutation>) -> Result<(), PortError> {
28        let (graph_mutations, detail_mutations): (Vec<_>, Vec<_>) = mutations
29            .into_iter()
30            .partition(|mutation| !matches!(mutation, ProjectionMutation::UpsertNodeDetail(_)));
31
32        if !graph_mutations.is_empty() {
33            self.graph_writer.apply_mutations(graph_mutations).await?;
34        }
35        if !detail_mutations.is_empty() {
36            self.detail_writer.apply_mutations(detail_mutations).await?;
37        }
38
39        Ok(())
40    }
41}