From f751a2c89aa43afb1ee219dfa9e5152140b047a0 Mon Sep 17 00:00:00 2001 From: Martin Pedersen Date: Thu, 9 Nov 2023 16:23:01 +0000 Subject: [PATCH] fix partial config defaults --- src/config.rs | 69 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6048b55..9144d67 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,13 +6,30 @@ use std::fmt; use std::path::PathBuf; use tower_lsp::lsp_types::DiagnosticSeverity; +fn default_vcl_paths() -> Vec { + vec!["./".into()] +} + +fn default_vmod_paths() -> Vec { + vec![ + // ubuntu, debian + "/usr/lib/x86_64-linux-gnu/varnish/vmods/".into(), + // fedora, centos + "/usr/lib64/varnish/vmods/".into(), + // freebsd + "/usr/local/lib/varnish/vmods".into(), + // arch + "/usr/lib/varnish/vmods".into(), + ] +} + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Config { #[serde(default)] pub main_vcl: Option, - #[serde(default)] + #[serde(default = "default_vcl_paths")] pub vcl_paths: Vec, - #[serde(default)] + #[serde(default = "default_vmod_paths")] pub vmod_paths: Vec, #[serde(default)] pub vcc_paths: Vec, @@ -22,22 +39,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { - Config { - main_vcl: None, - vcl_paths: vec!["./".into()], - vmod_paths: vec![ - // ubuntu, debian - "/usr/lib/x86_64-linux-gnu/varnish/vmods/".into(), - // fedora, centos - "/usr/lib64/varnish/vmods/".into(), - // freebsd - "/usr/local/lib/varnish/vmods".into(), - // arch - "/usr/lib/varnish/vmods".into(), - ], - vcc_paths: vec![], - lint: Default::default(), - } + toml::from_str::("").unwrap() } } @@ -143,6 +145,37 @@ impl Default for LintConfig { mod tests { use super::*; + fn assert_config_defaults(config: Config) { + assert_eq!( + config.vcl_paths, + vec![PathBuf::from("./")], + "vcl_paths should default to ./" + ); + assert!( + !config.vmod_paths.is_empty(), + "vmod_paths should not be empty" + ); + } + + #[test] + fn partial_config_with_defaults() { + let toml_str = r#" + main_vcl = "martin.vcl" + vcc_paths = ["./vcc-files"] + "#; + + let config: Config = toml::from_str(toml_str).unwrap(); + println!("{:?}", config); + assert_config_defaults(config); + } + + #[test] + fn empty_config_with_defaults() { + let config = Config::default(); + println!("{:?}", config); + assert_config_defaults(config); + } + #[test] fn can_parse_lint_config() { let toml_str = r#"