Files

50 lines
1.1 KiB
Rust
Raw Permalink Normal View History

use serde::{Deserialize, Serialize};
2025-01-21 19:01:37 +01:00
use std::fs::read_to_string;
use std::io;
use std::path::PathBuf;
#[derive(Deserialize, Serialize, Debug, Clone)]
2025-01-21 19:01:37 +01:00
pub struct ServerConfig {
pub host: String,
2025-02-28 01:25:03 +08:00
pub origin: Option<String>,
2025-01-21 19:01:37 +01:00
pub port: u16,
}
2025-03-09 11:32:05 +01:00
impl Default for ServerConfig {
fn default() -> Self {
ServerConfig {
host: "localhost".to_string(),
origin: None,
port: 3000,
}
}
}
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
2025-01-21 19:01:37 +01:00
pub struct Config {
pub server: ServerConfig,
}
impl Config {
pub fn get() -> io::Result<Config> {
let config_file = read_to_string(PathBuf::from_iter([".tuono", "config", "config.json"]))?;
serde_json::from_str(&config_file)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
}
2025-03-09 11:32:05 +01:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_default() {
let config = Config::default();
assert_eq!(config.server.host, "localhost".to_string());
assert_eq!(config.server.origin, None);
assert_eq!(config.server.port, 3000);
}
}