toasty_cli/config.rs
1use crate::migration::MigrationConfig;
2use anyhow::Result;
3use serde::{Deserialize, Serialize};
4use std::fs;
5use std::path::Path;
6
7/// Configuration for Toasty CLI operations.
8///
9/// Holds all settings that control how the CLI behaves. Currently this is
10/// limited to [`MigrationConfig`]. A `Config` can be built programmatically
11/// with the builder methods or loaded from a `Toasty.toml` file via
12/// [`Config::load`].
13///
14/// # Examples
15///
16/// ```
17/// use toasty_cli::{Config, MigrationConfig, MigrationPrefixStyle};
18///
19/// let config = Config::new()
20/// .migration(
21/// MigrationConfig::new()
22/// .path("db")
23/// .prefix_style(MigrationPrefixStyle::Timestamp),
24/// );
25/// assert_eq!(
26/// config.migration.get_migrations_dir(),
27/// std::path::PathBuf::from("db/migrations"),
28/// );
29/// ```
30#[derive(Debug, Default, Clone, Serialize, Deserialize)]
31pub struct Config {
32 /// Migration-related configuration
33 pub migration: MigrationConfig,
34}
35
36impl Config {
37 /// Create a new Config with default values
38 pub fn new() -> Self {
39 Self::default()
40 }
41
42 /// Load configuration from Toasty.toml in the project root
43 pub fn load() -> Result<Self> {
44 let path = Path::new("Toasty.toml");
45 let contents = fs::read_to_string(path)?;
46 let config: Config = toml::from_str(&contents)?;
47 Ok(config)
48 }
49
50 /// Set the migration configuration
51 pub fn migration(mut self, migration: MigrationConfig) -> Self {
52 self.migration = migration;
53 self
54 }
55}