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.

142 lines
3.6KB

  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. /// Get and parse the config.
  54. /// If it doesn't succeed, exit
  55. pub fn get_config() -> Config {
  56. match Config::from_file("config.toml") {
  57. Ok(c) => c,
  58. Err(e) => {
  59. println!("Failed to load config.toml");
  60. println!("Error: {}", e);
  61. ::std::process::exit(1);
  62. }
  63. }
  64. }
  65. #[cfg(test)]
  66. mod tests {
  67. use super::{Config};
  68. #[test]
  69. fn test_can_import_valid_config() {
  70. let config = r#"
  71. title = "My site"
  72. base_url = "https://replace-this-with-your-url.com"
  73. "#;
  74. let config = Config::parse(config).unwrap();
  75. assert_eq!(config.title, "My site".to_string());
  76. }
  77. #[test]
  78. fn test_errors_when_invalid_type() {
  79. let config = r#"
  80. title = 1
  81. base_url = "https://replace-this-with-your-url.com"
  82. "#;
  83. let config = Config::parse(config);
  84. assert!(config.is_err());
  85. }
  86. #[test]
  87. fn test_errors_when_missing_required_field() {
  88. let config = r#"
  89. title = ""
  90. "#;
  91. let config = Config::parse(config);
  92. assert!(config.is_err());
  93. }
  94. #[test]
  95. fn test_can_add_extra_values() {
  96. let config = r#"
  97. title = "My site"
  98. base_url = "https://replace-this-with-your-url.com"
  99. [extra]
  100. hello = "world"
  101. "#;
  102. let config = Config::parse(config);
  103. assert!(config.is_ok());
  104. assert_eq!(config.unwrap().extra.unwrap().get("hello").unwrap().as_str().unwrap(), "world");
  105. }
  106. #[test]
  107. fn test_language_defaults_to_en() {
  108. let config = r#"
  109. title = "My site"
  110. base_url = "https://replace-this-with-your-url.com""#;
  111. let config = Config::parse(config);
  112. assert!(config.is_ok());
  113. let config = config.unwrap();
  114. assert_eq!(config.language_code.unwrap(), "en");
  115. }
  116. }