antimatter 2.0.13

antimatter.io Rust library for data control
Documentation
use super::Session;
use crate::session::session::SessionError;
use crate::session::RUNTIME;
use antimatter_api::apis::policy_api::{self as api};
use antimatter_api::models::{
    DomainFactList, Fact, FactList, FactTuple, FactTypeDefinition, NewFact, NewFactTypeDefinition,
};

impl Session {
    /// Fetches the list of fact types in the session's domain.
    ///
    /// # Returns
    ///
    /// A `Result` containing a `DomainFactList` with a vector of fact types.
    pub fn list_fact_types(&mut self) -> Result<DomainFactList, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_list_fact_types(
                &conf,
                self.get_domain_id().as_str(),
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Fetches the list of facts in the session's domain for the given fact
    /// type.
    ///
    /// Arguments:
    ///
    /// * `fact_type` - A &str containing the fact type.
    ///
    /// # Returns
    ///
    /// A `Result` containing a `FactList` with a vector of facts of the given
    /// fact type.
    pub fn list_facts(&mut self, fact_type: &str) -> Result<FactList, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_list_facts(
                &conf,
                self.get_domain_id().as_str(),
                fact_type,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Creates a new fact type for the session's domain.
    ///
    /// Arguments:
    ///
    /// * `fact_type` - A &str containing the fact type.
    /// * `arguments` - A NewFactTypeDefinition containing the new fact type's
    ///     configuration.
    pub fn add_fact_type(
        &mut self,
        fact_type: &str,
        arguments: NewFactTypeDefinition,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_put_fact_type(
                &conf,
                self.get_domain_id().as_str(),
                fact_type,
                arguments,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Creates a new fact of fact type for the session's domain.
    ///
    /// Arguments:
    ///
    /// * `fact_type` - A &str containing the fact type.
    /// * `arguments` - A NewFact containing the new fact's configuration.
    pub fn add_fact(&mut self, fact_type: &str, arguments: NewFact) -> Result<Fact, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_upsert_fact(
                &conf,
                self.get_domain_id().as_str(),
                fact_type,
                arguments,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Fetches the fact type's information in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `fact_type` - A &str containing the fact type.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `FactTypeDefinition` with information about the
    /// fact type.
    pub fn get_fact_type(&mut self, fact_type: &str) -> Result<FactTypeDefinition, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_get_fact_type(
                &conf,
                self.get_domain_id().as_str(),
                fact_type,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Fetches the fact of fact type's information in the session's domain.
    ///
    /// Arguments:
    ///
    /// * `fact_type` - A &str containing the fact type.
    /// * `fact_id` - A &str containing the fact.
    ///
    /// Returns:
    ///
    /// A `Result` containing a `Fact` with information about the fact.
    pub fn get_fact(&mut self, fact_type: &str, fact_id: &str) -> Result<Fact, SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_get_fact_by_id(
                &conf,
                self.get_domain_id().as_str(),
                fact_type,
                fact_id,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }

    /// Delete the fact type from the session's domain.
    ///
    /// Arguments:
    ///
    /// * `fact_type` - A &str containing the fact type.
    /// * `confirm` - A &str containing the confirmation string (this is the
    ///     fact type).
    pub fn delete_fact_type(&mut self, fact_type: &str, confirm: &str) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        RUNTIME
            .block_on(api::domain_delete_fact_type(
                &conf,
                self.get_domain_id().as_str(),
                fact_type,
                confirm,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(())
    }

    /// Delete the fact of fact type from the session's domain. This is
    /// performed either by the fact's ID (single), or by matching fact
    /// arguments (many).
    ///
    /// Arguments:
    ///
    /// * `fact_type` - A &str containing the fact type.
    /// * `arguments` - A Option<Vec<String>> containing the arguments to match
    ///     a fact on.
    /// * `fact_id` - A Option<&str> containing the fact ID.
    pub fn delete_fact(
        &mut self,
        fact_type: &str,
        arguments: Option<Vec<String>>,
        fact_id: Option<&str>,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        if fact_id.is_none() && arguments.is_some() {
            self.delete_fact_by_tuple(
                fact_type,
                FactTuple {
                    arguments: arguments.unwrap(),
                },
            )
            .map_err(|e| SessionError::APIError(format!("{}", e)))
        } else {
            RUNTIME
                .block_on(api::domain_delete_fact_by_id(
                    &conf,
                    self.get_domain_id().as_str(),
                    fact_type,
                    fact_id.unwrap_or_default(),
                ))
                .map_err(|e| SessionError::APIError(format!("{}", e)))
        }
    }

    /// Delete all fact of fact type from the session's domain.
    ///
    /// Arguments:
    ///
    /// * `fact_type` - A &str containing the fact type.
    pub fn delete_all_facts(&mut self, fact_type: &str) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        let fact_list = self
            .list_facts(fact_type)
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        for matching_fact in fact_list.facts.iter() {
            let _ = RUNTIME
                .block_on(api::domain_delete_fact_by_id(
                    &conf,
                    self.get_domain_id().as_str(),
                    fact_type,
                    matching_fact.id.as_str(),
                ))
                .map_err(|e| SessionError::APIError(format!("{}", e)))?;
        }

        Ok(())
    }

    /// Delete a fact of fact type from the session's domain that matches the supplied tuple.
    ///
    /// Arguments:
    ///
    /// * `fact_type`  - A &str containing the fact type.
    /// * `fact_tuple` - A list of &str containing the fact arguments.
    pub fn delete_fact_by_tuple(
        &mut self,
        fact_type: &str,
        fact_tuple: FactTuple,
    ) -> Result<(), SessionError> {
        let conf = self.get_configuration()?;

        let res = RUNTIME
            .block_on(api::domain_delete_fact_by_tuple(
                &conf,
                self.get_domain_id().as_str(),
                fact_type,
                fact_tuple,
            ))
            .map_err(|e| SessionError::APIError(format!("{}", e)))?;

        Ok(res)
    }
}