use std::fmt::{Display, Error, Formatter}; use std::num::NonZeroUsize; use std::path::Path; #[derive(Clone, Debug, PartialEq, Hash, Eq)] pub struct FpmPoolConfig { max_children: NonZeroUsize, custom: Option>, } impl Display for FpmPoolConfig { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { match &self.custom { None => write!(f, "pm.max_children = {}", self.max_children), Some(custom) => write!(f, "pm.max_children = {}\n{}", self.max_children, custom), } } } impl From for FpmPoolConfig { fn from(max_children: usize) -> Self { Self { max_children: NonZeroUsize::try_from(max_children).unwrap(), custom: None, } } } impl FpmPoolConfig { pub fn new(max_children: NonZeroUsize, custom: impl Into>) -> Self { Self { max_children, custom: Some(custom.into()), } } } pub fn fpm_pool_config, S: AsRef>( user_name: U, socket_path: S, config: &FpmPoolConfig, ) -> String { format!( "[{0}] user = {0} group = www-data listen = {1} listen.owner = www-data pm = ondemand catch_workers_output = yes env[PATH] = /usr/local/bin:/usr/bin:/bin {2} ", user_name.as_ref(), socket_path.as_ref().to_str().unwrap(), config ) } #[cfg(test)] mod test { use super::fpm_pool_config; #[test] fn test_fpm_pool_config() { assert_eq!( fpm_pool_config("user", "socket", &5.into()), r"[user] user = user group = www-data listen = socket listen.owner = www-data pm = ondemand catch_workers_output = yes env[PATH] = /usr/local/bin:/usr/bin:/bin pm.max_children = 5 " ); } }