test-shisho-policy 0.1.0

This is the SDK of Shisho Cloud policies to write your custom policies in Rust.
Documentation
use serde::Deserialize;
use std::collections::HashMap;

pub mod data_entry;
pub mod github;
pub mod severity;
use data_entry::*;

/// The current version of Shisho Cloud API
pub const API_VERSION: &str = "decision.api.shisho.dev/v1beta";

/// This is a specified object format for Shisho Cloud and has to be returned.
#[derive(Deserialize, Debug, Default)]
pub struct Decision<T> {
    pub header: DecisionHeader,
    pub entries: Vec<DecisionEntry<T>>,
}

/// this is the metadata of the policy. Some parameters are predefined by Shisho Cloud.
#[derive(Deserialize, Debug, Default)]
pub struct DecisionHeader {
    /// [DEFINED] this specifies the current version of Shisho Policy API. It is basically defined by Shisho Cloud.
    pub api_version: String,

    /// [EDITABLE] this is kind of policy.
    pub kind: String,

    /// [RESERVED] the identifier, `policy_report_id`(`policyReportId` in a GraphQL query) coming from `Input Data` should be set. This is editable but is reserved for a specific purpose.
    pub subject: String,

    /// [EDITABLE] This is the policy status and either `allow` or `deny` have to be set. If all risks are not found, this will be set as `allow` and if one of risks exists, this will be set as `deny`
    #[serde(rename = "type")]
    pub decision_type: DecisionType,

    /// [EDITABLE] this is a string vector for labeling your policy. Please use any labels describing your preferred state such as "temporary_policy", or "dev-team-2"
    pub labels: Vec<String>,

    /// [EDITABLE] these are annnotations. Some predefined annotations are set and if you want your custom annotations, please add yours.
    pub annotations: HashMap<String, String>,
}

/// this incidate the decising type = a decision result
#[derive(Deserialize, Debug, Default)]
pub enum DecisionType {
    /// It should be selected when all policy states are valid 
    #[default]
    Allow = 1,

    /// It should be selected when one of policy states is invalid
    Deny = 2,
}

#[derive(Deserialize, Debug, Default)]
pub struct DecisionHeaderBuilder {
    h: DecisionHeader,
}

impl DecisionHeaderBuilder {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn with_api_version(mut self) -> Self {
        self.h.api_version = API_VERSION.to_string();
        self
    }

    pub fn with_kind(mut self, kind: &str) -> Self {
        self.h.kind = kind.to_string();
        self
    }

    pub fn with_subject(mut self, subject: Subject) -> Self {
        self.h.subject = subject.value;
        self
    }

    pub fn with_decision_type(mut self, decision_type: DecisionType) -> Self {
        self.h.decision_type = decision_type;
        self
    }

    pub fn with_labels(mut self, labels: Vec<String>) -> Self {
        self.h.labels = labels;
        self
    }

    pub fn with_annotations(mut self, annotations: HashMap<String, String>) -> Self {
        self.h.annotations = annotations;
        self
    }

    pub fn build(self) -> DecisionHeader {
        self.h
    }
}

#[derive(Deserialize, Debug, Default)]
pub struct Subject {
    value: String,
}

impl Subject {
    pub fn new(subject: &str) -> Subject {
        Subject {
            value: subject.to_string(),
        }
    }
}