1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::sync::Arc;

use anyhow::{anyhow, Result};
use async_trait::async_trait;
use gaia_stub::tco_tmiv::Tco;

use super::Hook;
use crate::tco_tmiv::tco;

#[derive(Clone)]
pub struct SanitizeHook {
    schema_set: Arc<tco::SchemaSet>,
}

impl SanitizeHook {
    pub fn new(schema_set: impl Into<Arc<tco::SchemaSet>>) -> Self {
        Self {
            schema_set: schema_set.into(),
        }
    }

    fn sanitize(&self, input: &Tco) -> Result<Tco> {
        let sanitized = self
            .schema_set
            .sanitize(input)
            .map_err(|msg| anyhow!("TCO validation error: {}", msg))?;
        Ok(sanitized)
    }
}

#[async_trait]
impl Hook<Arc<Tco>> for SanitizeHook {
    type Output = Arc<Tco>;

    async fn hook(&mut self, tco: Arc<Tco>) -> Result<Self::Output> {
        let sanitized = self.sanitize(&tco)?;
        Ok(Arc::new(sanitized))
    }
}