You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.2 KiB
Rust

use std::{num::NonZeroU32, path::Path};
use std::{fs, io};
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Config {
#[serde(rename="form")]
pub forms: Vec<Form>,
pub mailer: EmailConfig,
pub rate_limit: RateConfig,
}
#[derive(Deserialize)]
pub struct Form {
pub slug: String,
pub name: String,
pub recipient_email: String,
pub subject: String,
pub fields: Vec<Field>,
}
#[derive(Deserialize)]
pub struct Field {
pub name: String,
pub pattern: Option<String>,
pub required: bool,
}
#[derive(Deserialize)]
pub struct EmailConfig {
pub from_address: String,
pub host: String,
pub port: u16,
pub implicit_tls: bool,
pub username: String,
pub password: String
}
#[derive(Deserialize)]
pub struct RateConfig {
pub burst_max: NonZeroU32,
pub replenish_seconds: u32,
}
impl Config {
pub fn parse(path: &Path) -> Result<Self, io::Error> {
let config_str = fs::read_to_string(path)?;
match toml::from_str(&config_str) {
Ok(config) => return Ok(config),
// TODO: Print warning about parse error
Err(_err) => Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid syntax for config file")),
}
}
}