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.

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