Skip to main content

fabricatio_constants/
lib.rs

1//! # Fabricatio Constants
2//!
3//! A foundational constants crate for the Fabricatio ecosystem, providing application-wide constants, paths, and configuration variables.
4//!
5//! This crate centralizes all constants used across the Fabricatio project, including application names, configuration file paths, directory locations, and environment variable names. It ensures consistency and provides a single source of truth for these values.
6//!
7//! ## Features
8//!
9//! - **Application Constants**: Centralized definitions for application name, repository details, and core package names
10//! - **Path Management**: Automatic determination of user configuration directories across different operating systems
11//! - **Template Support**: Constants for template directory management and global configuration files
12//! - **Environment Variables**: Standardized names for configuration and logging variables
13//!
14//! ## Platform Support
15//!
16//! The crate automatically detects the user's operating system and provides appropriate paths:
17//!
18//! | Platform | Config Directory | Example |
19//! |----------|------------------|----------|
20//! | Linux | `$XDG_CONFIG_HOME` or `$HOME/.config` | `/home/alice/.config/fabricatio` |
21//! | macOS | `$HOME/Library/Application Support` | `/Users/Alice/Library/Application Support/fabricatio` |
22//! | Windows | `{FOLDERID_RoamingAppData}` | `C:\Users\Alice\AppData\Roaming\fabricatio` |
23//!
24//! ## Usage
25//!
26//! ```rust
27//! use fabricatio_constants::{NAME, ROAMING, GLOBAL_CONFIG_FILE};
28//!
29//! fn main() {
30//!     println!("Application: {}", NAME);
31//!     println!("Config directory: {:?}", ROAMING);
32//!     println!("Config file: {:?}", GLOBAL_CONFIG_FILE);
33//! }
34//! ```
35//!
36//! For more information, see the [README](https://github.com/Whth/fabricatio/blob/main/crates/fabricatio-constants/README.md).
37
38use directories_next::BaseDirs;
39use once_cell::sync::Lazy;
40use std::path::PathBuf;
41
42/// The application name used across the project.
43pub const NAME: &str = "fabricatio";
44
45/// The name of the core package used by the application.
46pub const CORE_PACKAGE_NAME: &str = "fabricatio_core";
47
48/// The default configuration file name used by the application.
49pub const CONFIG_FILE: &str = "fabricatio.toml";
50/// The GitHub repository owner for the application.
51pub const REPO_OWNER: &str = "Whth";
52/// The GitHub repository name for the application.
53pub const REPO_NAME: &str = NAME;
54
55/// Returns the path to the user's config directory based on the operating system.
56///
57/// |Platform | Value                                 | Example                          |
58/// | ------- | ------------------------------------- | -------------------------------- |
59/// | Linux   | `$XDG_CONFIG_HOME` or `$HOME`/.config/<APPNAME> | /home/alice/.config/app              |
60/// | macOS   | `$HOME`/Library/Application Support/<APPNAME>   | /Users/Alice/Library/Application Support/app |
61/// | Windows | `{FOLDERID_RoamingAppData}\<APPNAME>`           | C:\Users\Alice\AppData\Roaming\app   |
62///
63/// # Arguments
64/// * `app_name` - The name of the application used when constructing the directory path.
65///
66/// # Returns
67/// An `Option<PathBuf>` representing the roaming directory if available.
68fn get_roaming_dir(app_name: &str) -> Option<PathBuf> {
69    BaseDirs::new().map(|dirs| dirs.config_dir().join(app_name))
70}
71
72/// A global static instance of the user's roaming configuration directory for the application.
73pub static ROAMING: Lazy<PathBuf> =
74    Lazy::new(|| get_roaming_dir(NAME).expect("Failed to get roaming directory"));
75/// The name of the templates' directory.
76pub const TEMPLATES_DIRNAME: &str = "templates";
77/// A global static instance of the templates directory located within the roaming configuration directory.
78pub static TEMPLATES: Lazy<PathBuf> = Lazy::new(|| ROAMING.join(TEMPLATES_DIRNAME));
79/// A global static instance of the global configuration file path, constructed by joining
80/// the roaming directory with the application-specific configuration file name.
81pub static GLOBAL_CONFIG_FILE: Lazy<PathBuf> = Lazy::new(|| ROAMING.join(CONFIG_FILE));
82
83/// The name of the logger variable used by the application.
84pub const LOGGER_VARNAME: &str = "logger";
85/// The name of the configuration variable used by the application.
86pub const CONFIG_VARNAME: &str = "CONFIG";
87/// The key used to store the Python source code path.
88pub const PY_SOURCE_KEY: &str = "py_source";