diff --git a/config.toml b/config.toml index b749b52..555b312 100644 --- a/config.toml +++ b/config.toml @@ -6,6 +6,6 @@ upload_path="./upload" #auth_token="" # OOPS_SERVER__AUTH_TOKEN= [paste] -pet_names = true +pet_names = { enabled = true, words = 2, separator = "-" } random = { enabled = false, length = 8 } default_extension = "txt" diff --git a/src/config.rs b/src/config.rs index a91bdb0..3e130c1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,21 +29,32 @@ pub struct ServerConfig { /// Paste configuration. #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct PasteConfig { - /// Use pet names instead of original file names. - pub pet_names: bool, + /// Pet names configuration. + pub pet_names: PetNamesConfig, /// Random string configuration. pub random: RandomConfig, /// Default file extension. pub default_extension: String, } +/// Pet names configuration. +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct PetNamesConfig { + /// Use pet names instead of original file names. + pub enabled: bool, + /// Count of words that pet name will include. + pub words: u8, + /// Separator between the words. + pub separator: String, +} + /// Random string configuration. #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct RandomConfig { /// Use random strings instead of original file names. pub enabled: bool, /// Length of the random string to generate. - pub length: Option, + pub length: usize, } impl Config { diff --git a/src/file.rs b/src/file.rs index 730a544..d6bd8b4 100644 --- a/src/file.rs +++ b/src/file.rs @@ -7,11 +7,11 @@ use std::io::{Result as IoResult, Write}; /// /// - If `file_name` does not have an extension, it is replaced with [`default_extension`]. /// - If `file_name` is "-", it is replaced with "stdin". -/// - If [`pet_names`] is `true`, `file_name` is replaced with a pet name. +/// - If [`pet_names.enabled`] is `true`, `file_name` is replaced with a pet name. /// - If [`random.enabled`] is `true`, `file_name` is replaced with a random string. /// /// [`default_extension`]: crate::config::PasteConfig::default_extension -/// [`pet_names`]: crate::config::PasteConfig::pet_names +/// [`pet_names.enabled`]: crate::config::PetNamesConfig::enabled /// [`random.enabled`]: crate::config::RandomConfig::enabled pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult { if file_name == "-" { @@ -20,14 +20,17 @@ pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult { - if config.paste.pet_names { - path.set_file_name(petname::petname(2, "-")); + if config.paste.pet_names.enabled { + path.set_file_name(petname::petname( + config.paste.pet_names.words, + &config.paste.pet_names.separator, + )); path.set_extension(extension); } else if config.paste.random.enabled { path.set_file_name( rand::thread_rng() .sample_iter(&Alphanumeric) - .take(config.paste.random.length.unwrap_or(8)) + .take(config.paste.random.length) .map(char::from) .collect::(), ); @@ -35,13 +38,16 @@ pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult { - if config.paste.pet_names { - path.set_file_name(petname::petname(2, "-")); + if config.paste.pet_names.enabled { + path.set_file_name(petname::petname( + config.paste.pet_names.words, + &config.paste.pet_names.separator, + )); } else if config.paste.random.enabled { path.set_file_name( rand::thread_rng() .sample_iter(&Alphanumeric) - .take(config.paste.random.length.unwrap_or(8)) + .take(config.paste.random.length) .map(char::from) .collect::(), );