ignis-db 0.2.0

A comprehensive PostgreSQL ORM with macro-driven schema management
Documentation
use crate::builders::traits::{
    ConditionalBuilder, InspectableBuilder, SchemaBuilder, ValidatableBuilder,
};
use crate::builders::validation::RoleBuildError;
use crate::schema::Role;

/// Builder for constructing database roles
#[derive(Debug, Clone)]
pub struct RoleBuilder {
    name: Option<String>,
    login: bool,
    password: Option<String>,
    superuser: bool,
    createdb: bool,
    createrole: bool,
    inherit: bool,
    replication: bool,
    bypassrls: bool,
    connection_limit: Option<i32>,
    valid_until: Option<String>,
    in_roles: Vec<String>,
    in_groups: Vec<String>,
    admin_role: Option<String>,
    member_of: Vec<String>,
    comment: Option<String>,
}

impl RoleBuilder {
    /// Create a new role builder
    pub fn new() -> Self {
        Self {
            name: None,
            login: false,
            password: None,
            superuser: false,
            createdb: false,
            createrole: false,
            inherit: true,
            replication: false,
            bypassrls: false,
            connection_limit: None,
            valid_until: None,
            in_roles: Vec::new(),
            in_groups: Vec::new(),
            admin_role: None,
            member_of: Vec::new(),
            comment: None,
        }
    }

    /// Set the role name
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the role name (mutable reference version for conditional building)
    pub fn set_name(&mut self, name: impl Into<String>) {
        self.name = Some(name.into());
    }

    /// Enable login for this role
    pub fn login(mut self, login: bool) -> Self {
        self.login = login;
        self
    }

    /// Set the role name (mutable reference version for conditional building)
    pub fn set_login(&mut self, login: bool) {
        self.login = login;
    }

    /// Set the password for this role
    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.password = Some(password.into());
        self
    }

    /// Set the password (mutable reference version for conditional building)
    pub fn set_password(&mut self, password: impl Into<String>) {
        self.password = Some(password.into());
    }

    /// Make this role a superuser
    pub fn superuser(mut self, superuser: bool) -> Self {
        self.superuser = superuser;
        self
    }

    /// Set superuser status (mutable reference version for conditional building)
    pub fn set_superuser(&mut self, superuser: bool) {
        self.superuser = superuser;
    }

    /// Allow this role to create databases
    pub fn createdb(mut self, createdb: bool) -> Self {
        self.createdb = createdb;
        self
    }

    /// Set createdb permission (mutable reference version for conditional building)
    pub fn set_createdb(&mut self, createdb: bool) {
        self.createdb = createdb;
    }

    /// Allow this role to create other roles
    pub fn createrole(mut self, createrole: bool) -> Self {
        self.createrole = createrole;
        self
    }

    /// Set createrole permission (mutable reference version for conditional building)
    pub fn set_createrole(&mut self, createrole: bool) {
        self.createrole = createrole;
    }

    /// Set inheritance behavior
    pub fn inherit(mut self, inherit: bool) -> Self {
        self.inherit = inherit;
        self
    }

    /// Set inheritance (mutable reference version for conditional building)
    pub fn set_inherit(&mut self, inherit: bool) {
        self.inherit = inherit;
    }

    /// Allow this role to initiate streaming replication
    pub fn replication(mut self, replication: bool) -> Self {
        self.replication = replication;
        self
    }

    /// Set replication permission (mutable reference version for conditional building)
    pub fn set_replication(&mut self, replication: bool) {
        self.replication = replication;
    }

    /// Allow this role to bypass row-level security
    pub fn bypassrls(mut self, bypassrls: bool) -> Self {
        self.bypassrls = bypassrls;
        self
    }

    /// Set bypassrls permission (mutable reference version for conditional building)
    pub fn set_bypassrls(&mut self, bypassrls: bool) {
        self.bypassrls = bypassrls;
    }

    /// Set connection limit for this role
    pub fn connection_limit(mut self, limit: i32) -> Self {
        self.connection_limit = Some(limit);
        self
    }

    /// Set connection limit (mutable reference version for conditional building)
    pub fn set_connection_limit(&mut self, limit: i32) {
        self.connection_limit = Some(limit);
    }

    /// Set when this role's password expires
    pub fn valid_until(mut self, valid_until: impl Into<String>) -> Self {
        self.valid_until = Some(valid_until.into());
        self
    }

    /// Set valid_until (mutable reference version for conditional building)
    pub fn set_valid_until(&mut self, valid_until: impl Into<String>) {
        self.valid_until = Some(valid_until.into());
    }

    /// Add roles that this role is a member of
    pub fn in_role(mut self, role: impl Into<String>) -> Self {
        self.in_roles.push(role.into());
        self
    }

    /// Add multiple roles that this role is a member of
    pub fn in_roles(mut self, roles: Vec<String>) -> Self {
        self.in_roles.extend(roles);
        self
    }

    /// Add roles (mutable reference version for conditional building)
    pub fn add_in_role(&mut self, role: impl Into<String>) {
        self.in_roles.push(role.into());
    }

    /// Add groups that this role is a member of
    pub fn in_group(mut self, group: impl Into<String>) -> Self {
        self.in_groups.push(group.into());
        self
    }

    /// Add multiple groups that this role is a member of
    pub fn in_groups(mut self, groups: Vec<String>) -> Self {
        self.in_groups.extend(groups);
        self
    }

    /// Add groups (mutable reference version for conditional building)
    pub fn add_in_group(&mut self, group: impl Into<String>) {
        self.in_groups.push(group.into());
    }

    /// Set the admin role for this role
    pub fn admin_role(mut self, admin_role: impl Into<String>) -> Self {
        self.admin_role = Some(admin_role.into());
        self
    }

    /// Set admin role (mutable reference version for conditional building)
    pub fn set_admin_role(&mut self, admin_role: impl Into<String>) {
        self.admin_role = Some(admin_role.into());
    }

    /// Add roles that are members of this role
    pub fn member_of(mut self, role: impl Into<String>) -> Self {
        self.member_of.push(role.into());
        self
    }

    /// Add multiple roles that are members of this role
    pub fn members_of(mut self, roles: Vec<String>) -> Self {
        self.member_of.extend(roles);
        self
    }

    /// Add members (mutable reference version for conditional building)
    pub fn add_member(&mut self, role: impl Into<String>) {
        self.member_of.push(role.into());
    }

    /// Set a comment for this role
    pub fn comment(mut self, comment: impl Into<String>) -> Self {
        self.comment = Some(comment.into());
        self
    }

    /// Set comment (mutable reference version for conditional building)
    pub fn set_comment(&mut self, comment: impl Into<String>) {
        self.comment = Some(comment.into());
    }
}

impl SchemaBuilder for RoleBuilder {
    type Output = Role;
    type Error = RoleBuildError;

    fn build(self) -> Result<Self::Output, Self::Error> {
        let name = self.name.ok_or(RoleBuildError::MissingName)?;

        let mut role = Role::new(name);

        // Set basic attributes
        role.login = self.login;
        role.superuser = self.superuser;
        role.createdb = self.createdb;
        role.createrole = self.createrole;
        role.inherit = self.inherit;
        role.replication = self.replication;

        // Set optional attributes
        if let Some(password) = self.password {
            role = role.with_password(password);
        }
        if let Some(limit) = self.connection_limit {
            role = role.with_connection_limit(limit);
        }
        if let Some(valid_until) = self.valid_until {
            role = role.with_valid_until(valid_until);
        }

        // Add role memberships
        role.member_of = self.in_roles;
        role.member_of.extend(self.in_groups);
        role.members = self.member_of;

        Ok(role)
    }
}

impl ValidatableBuilder for RoleBuilder {
    fn is_valid(&self) -> bool {
        self.name.is_some()
            && (!self.login || self.password.is_some())
            && self.connection_limit.map_or(true, |limit| limit >= 0)
    }

    fn validation_errors(&self) -> Vec<String> {
        let mut errors = Vec::new();

        if self.name.is_none() {
            errors.push("Role name is required".to_string());
        }

        if self.login && self.password.is_none() {
            errors.push("Login role must have a password".to_string());
        }

        if let Some(limit) = self.connection_limit {
            if limit < 0 {
                errors.push(format!("Invalid connection limit: {}", limit));
            }
        }

        errors
    }
}

impl ConditionalBuilder for RoleBuilder {
    // The trait provides default implementations for when() and when_else()
    // No additional implementation needed
}

impl InspectableBuilder for RoleBuilder {
    fn debug_info(&self) -> String {
        format!(
            "RoleBuilder {{ name: {:?}, login: {}, superuser: {}, createdb: {}, createrole: {}, inherit: {}, replication: {}, bypassrls: {}, connection_limit: {:?}, valid_until: {:?}, in_roles: {:?}, in_groups: {:?}, admin_role: {:?}, member_of: {:?}, comment: {:?} }}",
            self.name,
            self.login,
            self.superuser,
            self.createdb,
            self.createrole,
            self.inherit,
            self.replication,
            self.bypassrls,
            self.connection_limit,
            self.valid_until,
            self.in_roles,
            self.in_groups,
            self.admin_role,
            self.member_of,
            self.comment
        )
    }

    fn summary(&self) -> String {
        let name = self.name.as_deref().unwrap_or("unnamed");
        let mut attrs = Vec::new();

        if self.login {
            attrs.push("LOGIN");
        }
        if self.superuser {
            attrs.push("SUPERUSER");
        }
        if self.createdb {
            attrs.push("CREATEDB");
        }
        if self.createrole {
            attrs.push("CREATEROLE");
        }
        if self.inherit {
            attrs.push("INHERIT");
        }
        if self.replication {
            attrs.push("REPLICATION");
        }
        if self.bypassrls {
            attrs.push("BYPASSRLS");
        }

        let attrs_str = if attrs.is_empty() {
            "no special attributes".to_string()
        } else {
            attrs.join(", ")
        };

        format!("Role '{}' with {}", name, attrs_str)
    }
}