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.

158 lines
4.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. #[derive(Debug, PartialEq, Serialize, Deserialize)]
  8. pub struct Config {
  9. /// Title of the site
  10. pub title: String,
  11. /// Base URL of the site
  12. pub base_url: String,
  13. /// Whether to highlight all code blocks found in markdown files. Defaults to false
  14. pub highlight_code: Option<bool>,
  15. /// Description of the site
  16. pub description: Option<String>,
  17. /// The language used in the site. Defaults to "en"
  18. pub language_code: Option<String>,
  19. /// Whether to disable RSS generation, defaults to false (== generate RSS)
  20. pub disable_rss: Option<bool>,
  21. /// All user params set in [extra] in the config
  22. pub extra: Option<HashMap<String, Toml>>,
  23. }
  24. impl Config {
  25. /// Parses a string containing TOML to our Config struct
  26. /// Any extra parameter will end up in the extra field
  27. pub fn parse(content: &str) -> Result<Config> {
  28. let mut config: Config = match toml::from_str(content) {
  29. Ok(c) => c,
  30. Err(e) => bail!(e)
  31. };
  32. if config.language_code.is_none() {
  33. config.language_code = Some("en".to_string());
  34. }
  35. if config.highlight_code.is_none() {
  36. config.highlight_code = Some(false);
  37. }
  38. if config.disable_rss.is_none() {
  39. config.disable_rss = Some(false);
  40. }
  41. Ok(config)
  42. }
  43. /// Parses a config file from the given path
  44. pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
  45. let mut content = String::new();
  46. File::open(path)
  47. .chain_err(|| "No `config.toml` file found. Are you in the right directory?")?
  48. .read_to_string(&mut content)?;
  49. Config::parse(&content)
  50. }
  51. }
  52. impl Default for Config {
  53. /// Exists for testing purposes
  54. fn default() -> Config {
  55. Config {
  56. title: "".to_string(),
  57. base_url: "http://a-website.com/".to_string(),
  58. highlight_code: Some(true),
  59. description: None,
  60. language_code: Some("en".to_string()),
  61. disable_rss: Some(false),
  62. extra: None,
  63. }
  64. }
  65. }
  66. /// Get and parse the config.
  67. /// If it doesn't succeed, exit
  68. pub fn get_config() -> Config {
  69. match Config::from_file("config.toml") {
  70. Ok(c) => c,
  71. Err(e) => {
  72. println!("Failed to load config.toml");
  73. println!("Error: {}", e);
  74. ::std::process::exit(1);
  75. }
  76. }
  77. }
  78. #[cfg(test)]
  79. mod tests {
  80. use super::{Config};
  81. #[test]
  82. fn test_can_import_valid_config() {
  83. let config = r#"
  84. title = "My site"
  85. base_url = "https://replace-this-with-your-url.com"
  86. "#;
  87. let config = Config::parse(config).unwrap();
  88. assert_eq!(config.title, "My site".to_string());
  89. }
  90. #[test]
  91. fn test_errors_when_invalid_type() {
  92. let config = r#"
  93. title = 1
  94. base_url = "https://replace-this-with-your-url.com"
  95. "#;
  96. let config = Config::parse(config);
  97. assert!(config.is_err());
  98. }
  99. #[test]
  100. fn test_errors_when_missing_required_field() {
  101. let config = r#"
  102. title = ""
  103. "#;
  104. let config = Config::parse(config);
  105. assert!(config.is_err());
  106. }
  107. #[test]
  108. fn test_can_add_extra_values() {
  109. let config = r#"
  110. title = "My site"
  111. base_url = "https://replace-this-with-your-url.com"
  112. [extra]
  113. hello = "world"
  114. "#;
  115. let config = Config::parse(config);
  116. assert!(config.is_ok());
  117. assert_eq!(config.unwrap().extra.unwrap().get("hello").unwrap().as_str().unwrap(), "world");
  118. }
  119. #[test]
  120. fn test_language_defaults_to_en() {
  121. let config = r#"
  122. title = "My site"
  123. base_url = "https://replace-this-with-your-url.com""#;
  124. let config = Config::parse(config);
  125. assert!(config.is_ok());
  126. let config = config.unwrap();
  127. assert_eq!(config.language_code.unwrap(), "en");
  128. }
  129. }