| Crates.io | moosicbox_config |
| lib.rs | moosicbox_config |
| version | 0.1.4 |
| created_at | 2024-10-04 03:58:22.239612+00 |
| updated_at | 2025-07-21 19:11:48.166212+00 |
| description | MoosicBox configuration package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1396070 |
| size | 72,175 |
Basic configuration utilities for MoosicBox applications.
The MoosicBox Config package provides:
api feature)db feature)git clone https://github.com/MoosicBox/MoosicBox.git
cd MoosicBox
cargo build --release --package moosicbox_config
[dependencies]
moosicbox_config = { path = "../config" }
# Optional: Enable database functionality
moosicbox_config = {
path = "../config",
features = ["db"]
}
# Optional: Enable API endpoints
moosicbox_config = {
path = "../config",
features = ["api"]
}
use moosicbox_config::{
AppType, get_config_dir_path, get_profile_dir_path,
make_config_dir_path, make_profile_dir_path
};
fn setup_directories() -> Result<(), Box<dyn std::error::Error>> {
// Get configuration directory path
if let Some(config_dir) = get_config_dir_path() {
println!("Config directory: {:?}", config_dir);
}
// Create configuration directory if it doesn't exist
if let Some(config_dir) = make_config_dir_path() {
println!("Config directory created: {:?}", config_dir);
}
// Get profile-specific directory
if let Some(profile_dir) = get_profile_dir_path(AppType::Server, "default") {
println!("Profile directory: {:?}", profile_dir);
}
// Create profile directory
if let Some(profile_dir) = make_profile_dir_path(AppType::Server, "default") {
println!("Profile directory created: {:?}", profile_dir);
}
Ok(())
}
use moosicbox_config::set_root_dir;
use std::path::PathBuf;
fn configure_root_directory() {
// Set custom root directory (before any other operations)
let custom_root = PathBuf::from("/opt/moosicbox");
set_root_dir(custom_root);
// Now all config paths will be relative to /opt/moosicbox
}
use moosicbox_config::{upsert_profile, delete_profile, get_profiles};
use switchy_database::config::ConfigDatabase;
async fn manage_profiles(db: &ConfigDatabase) -> Result<(), Box<dyn std::error::Error>> {
// Create or get existing profile
let profile = upsert_profile(db, "my_profile").await?;
println!("Profile: {} (ID: {})", profile.name, profile.id);
// Get all profiles
let profiles = get_profiles(db).await?;
for profile in profiles {
println!("Found profile: {}", profile.name);
}
// Delete profile
let deleted_profiles = delete_profile(db, "my_profile").await?;
println!("Deleted {} profiles", deleted_profiles.len());
Ok(())
}
use moosicbox_config::{get_server_identity, get_or_init_server_identity};
use switchy_database::config::ConfigDatabase;
async fn manage_server_identity(db: &ConfigDatabase) -> Result<(), Box<dyn std::error::Error>> {
// Get existing server identity
if let Some(identity) = get_server_identity(db).await? {
println!("Server identity: {}", identity);
}
// Get or create server identity
let identity = get_or_init_server_identity(db).await?;
println!("Server identity: {}", identity);
Ok(())
}
// Path management
pub fn set_root_dir(path: PathBuf);
pub fn get_config_dir_path() -> Option<PathBuf>;
pub fn get_app_config_dir_path(app_type: AppType) -> Option<PathBuf>;
pub fn get_profiles_dir_path(app_type: AppType) -> Option<PathBuf>;
pub fn get_profile_dir_path(app_type: AppType, profile: &str) -> Option<PathBuf>;
pub fn get_cache_dir_path() -> Option<PathBuf>;
// Directory creation
pub fn make_config_dir_path() -> Option<PathBuf>;
pub fn make_profile_dir_path(app_type: AppType, profile: &str) -> Option<PathBuf>;
pub fn make_cache_dir_path() -> Option<PathBuf>;
// Profile management
pub async fn upsert_profile(db: &ConfigDatabase, name: &str) -> Result<models::Profile, DatabaseFetchError>;
pub async fn delete_profile(db: &ConfigDatabase, name: &str) -> Result<Vec<models::Profile>, DatabaseFetchError>;
pub async fn get_profiles(db: &ConfigDatabase) -> Result<Vec<models::Profile>, DatabaseFetchError>;
// Server identity
pub async fn get_server_identity(db: &ConfigDatabase) -> Result<Option<String>, DatabaseError>;
pub async fn get_or_init_server_identity(db: &ConfigDatabase) -> Result<String, GetOrInitServerIdentityError>;
#[derive(Copy, Clone, Debug)]
pub enum AppType {
App, // Application configuration
Server, // Server configuration
Local, // Local configuration
}
api: Enable REST API endpoints for configuration managementdb: Enable database functionality for profiles and server identitytest: Enable test utilities for temporary directories~/.local/moosicbox if not setmake_* functionsdb feature)home crate for default root directory resolutionAll database operations return Result types with appropriate error handling: