heroforge-core 0.2.2

Pure Rust core library for reading and writing Fossil SCM repositories
Documentation
//! User management builder.

use crate::error::{FossilError, Result};
use crate::repo::Repository;

/// Builder for user operations.
pub struct UsersBuilder<'a> {
    repo: &'a Repository,
}

impl<'a> UsersBuilder<'a> {
    pub(crate) fn new(repo: &'a Repository) -> Self {
        Self { repo }
    }

    /// List all users with their capabilities.
    pub fn list(&self) -> Result<Vec<(String, String)>> {
        self.repo.list_users_internal()
    }

    /// Get a specific user.
    pub fn get(&self, login: &str) -> Result<UserRef<'a>> {
        // Verify user exists
        self.repo.get_user_capabilities_internal(login)?;
        Ok(UserRef {
            repo: self.repo,
            login: login.to_string(),
        })
    }

    /// Start creating a new user.
    pub fn create(&self, login: &str) -> UserBuilder<'a> {
        UserBuilder {
            repo: self.repo,
            login: login.to_string(),
            password: String::new(),
            capabilities: None,
        }
    }
}

/// Reference to an existing user.
pub struct UserRef<'a> {
    repo: &'a Repository,
    login: String,
}

impl<'a> UserRef<'a> {
    /// Get the user's capabilities.
    pub fn capabilities(&self) -> Result<Option<String>> {
        self.repo.get_user_capabilities_internal(&self.login)
    }

    /// Set the user's capabilities.
    pub fn set_capabilities(&self, caps: &str) -> Result<()> {
        self.repo.set_user_capabilities_internal(&self.login, caps)
    }

    /// Get the login name.
    pub fn login(&self) -> &str {
        &self.login
    }
}

/// Builder for creating a new user.
pub struct UserBuilder<'a> {
    repo: &'a Repository,
    login: String,
    password: String,
    capabilities: Option<String>,
}

impl<'a> UserBuilder<'a> {
    /// Set the password.
    pub fn password(mut self, pwd: &str) -> Self {
        self.password = pwd.to_string();
        self
    }

    /// Set the capabilities string.
    ///
    /// Common capability codes:
    /// - `a` - Admin
    /// - `d` - Delete
    /// - `e` - Email
    /// - `i` - Check-in
    /// - `o` - Check-out
    /// - `r` - Read
    /// - `v` - Developer
    /// - `w` - Write ticket
    pub fn capabilities(mut self, caps: &str) -> Self {
        self.capabilities = Some(caps.to_string());
        self
    }

    /// Execute the user creation.
    pub fn execute(self) -> Result<()> {
        let caps = self.capabilities.ok_or_else(|| {
            FossilError::InvalidArtifact("user capabilities are required".to_string())
        })?;

        self.repo
            .create_user_internal(&self.login, &self.password, &caps)
    }
}