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

117 lines
3.0KB

  1. use std::fs::File;
  2. use std::io::prelude::*;
  3. use std::path::Path;
  4. use std::collections::HashMap;
  5. use toml::{Value as Toml, self};
  6. use errors::{Result, ResultExt};
  7. // TODO: disable tag(s)/category(ies) page generation
  8. #[derive(Debug, PartialEq, Serialize, Deserialize)]
  9. pub struct Config {
  10. /// Title of the site
  11. pub title: String,
  12. /// Base URL of the site
  13. pub base_url: String,
  14. /// Description of the site
  15. pub description: Option<String>,
  16. /// The language used in the site. Defaults to "en"
  17. pub language_code: Option<String>,
  18. /// Whether to disable RSS generation, defaults to None (== generate RSS)
  19. pub disable_rss: Option<bool>,
  20. /// All user params set in [extra] in the config
  21. pub extra: Option<HashMap<String, Toml>>,
  22. }
  23. impl Config {
  24. /// Parses a string containing TOML to our Config struct
  25. /// Any extra parameter will end up in the extra field
  26. pub fn parse(content: &str) -> Result<Config> {
  27. let mut config: Config = match toml::from_str(content) {
  28. Ok(c) => c,
  29. Err(e) => bail!(e)
  30. };
  31. if config.language_code.is_none() {
  32. config.language_code = Some("en".to_string());
  33. }
  34. Ok(config)
  35. }
  36. /// Parses a config file from the given path
  37. pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
  38. let mut content = String::new();
  39. File::open(path)
  40. .chain_err(|| "No `config.toml` file found. Are you in the right directory?")?
  41. .read_to_string(&mut content)?;
  42. Config::parse(&content)
  43. }
  44. }
  45. #[cfg(test)]
  46. mod tests {
  47. use super::{Config};
  48. #[test]
  49. fn test_can_import_valid_config() {
  50. let config = r#"
  51. title = "My site"
  52. base_url = "https://replace-this-with-your-url.com"
  53. "#;
  54. let config = Config::parse(config).unwrap();
  55. assert_eq!(config.title, "My site".to_string());
  56. }
  57. #[test]
  58. fn test_errors_when_invalid_type() {
  59. let config = r#"
  60. title = 1
  61. base_url = "https://replace-this-with-your-url.com"
  62. "#;
  63. let config = Config::parse(config);
  64. assert!(config.is_err());
  65. }
  66. #[test]
  67. fn test_errors_when_missing_required_field() {
  68. let config = r#"
  69. title = ""
  70. "#;
  71. let config = Config::parse(config);
  72. assert!(config.is_err());
  73. }
  74. #[test]
  75. fn test_can_add_extra_values() {
  76. let config = r#"
  77. title = "My site"
  78. base_url = "https://replace-this-with-your-url.com"
  79. [extra]
  80. hello = "world"
  81. "#;
  82. let config = Config::parse(config);
  83. assert!(config.is_ok());
  84. assert_eq!(config.unwrap().extra.unwrap().get("hello").unwrap().as_str().unwrap(), "world");
  85. }
  86. #[test]
  87. fn test_language_defaults_to_en() {
  88. let config = r#"
  89. title = "My site"
  90. base_url = "https://replace-this-with-your-url.com""#;
  91. let config = Config::parse(config);
  92. assert!(config.is_ok());
  93. let config = config.unwrap();
  94. assert_eq!(config.language_code.unwrap(), "en");
  95. }
  96. }