use crate::builders::traits::{
ConditionalBuilder, InspectableBuilder, SchemaBuilder, ValidatableBuilder,
};
use crate::builders::validation::RoleBuildError;
use crate::schema::Role;
#[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 {
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,
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn set_name(&mut self, name: impl Into<String>) {
self.name = Some(name.into());
}
pub fn login(mut self, login: bool) -> Self {
self.login = login;
self
}
pub fn set_login(&mut self, login: bool) {
self.login = login;
}
pub fn password(mut self, password: impl Into<String>) -> Self {
self.password = Some(password.into());
self
}
pub fn set_password(&mut self, password: impl Into<String>) {
self.password = Some(password.into());
}
pub fn superuser(mut self, superuser: bool) -> Self {
self.superuser = superuser;
self
}
pub fn set_superuser(&mut self, superuser: bool) {
self.superuser = superuser;
}
pub fn createdb(mut self, createdb: bool) -> Self {
self.createdb = createdb;
self
}
pub fn set_createdb(&mut self, createdb: bool) {
self.createdb = createdb;
}
pub fn createrole(mut self, createrole: bool) -> Self {
self.createrole = createrole;
self
}
pub fn set_createrole(&mut self, createrole: bool) {
self.createrole = createrole;
}
pub fn inherit(mut self, inherit: bool) -> Self {
self.inherit = inherit;
self
}
pub fn set_inherit(&mut self, inherit: bool) {
self.inherit = inherit;
}
pub fn replication(mut self, replication: bool) -> Self {
self.replication = replication;
self
}
pub fn set_replication(&mut self, replication: bool) {
self.replication = replication;
}
pub fn bypassrls(mut self, bypassrls: bool) -> Self {
self.bypassrls = bypassrls;
self
}
pub fn set_bypassrls(&mut self, bypassrls: bool) {
self.bypassrls = bypassrls;
}
pub fn connection_limit(mut self, limit: i32) -> Self {
self.connection_limit = Some(limit);
self
}
pub fn set_connection_limit(&mut self, limit: i32) {
self.connection_limit = Some(limit);
}
pub fn valid_until(mut self, valid_until: impl Into<String>) -> Self {
self.valid_until = Some(valid_until.into());
self
}
pub fn set_valid_until(&mut self, valid_until: impl Into<String>) {
self.valid_until = Some(valid_until.into());
}
pub fn in_role(mut self, role: impl Into<String>) -> Self {
self.in_roles.push(role.into());
self
}
pub fn in_roles(mut self, roles: Vec<String>) -> Self {
self.in_roles.extend(roles);
self
}
pub fn add_in_role(&mut self, role: impl Into<String>) {
self.in_roles.push(role.into());
}
pub fn in_group(mut self, group: impl Into<String>) -> Self {
self.in_groups.push(group.into());
self
}
pub fn in_groups(mut self, groups: Vec<String>) -> Self {
self.in_groups.extend(groups);
self
}
pub fn add_in_group(&mut self, group: impl Into<String>) {
self.in_groups.push(group.into());
}
pub fn admin_role(mut self, admin_role: impl Into<String>) -> Self {
self.admin_role = Some(admin_role.into());
self
}
pub fn set_admin_role(&mut self, admin_role: impl Into<String>) {
self.admin_role = Some(admin_role.into());
}
pub fn member_of(mut self, role: impl Into<String>) -> Self {
self.member_of.push(role.into());
self
}
pub fn members_of(mut self, roles: Vec<String>) -> Self {
self.member_of.extend(roles);
self
}
pub fn add_member(&mut self, role: impl Into<String>) {
self.member_of.push(role.into());
}
pub fn comment(mut self, comment: impl Into<String>) -> Self {
self.comment = Some(comment.into());
self
}
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);
role.login = self.login;
role.superuser = self.superuser;
role.createdb = self.createdb;
role.createrole = self.createrole;
role.inherit = self.inherit;
role.replication = self.replication;
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);
}
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 {
}
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)
}
}