feat(config): improve label field fallback

This commit is contained in:
Federico Terzi 2021-07-31 22:27:52 +02:00
parent dcc2cd2708
commit e151364014

View File

@ -31,7 +31,7 @@ use crate::{counter::next_id, merge};
use anyhow::Result; use anyhow::Result;
use log::error; use log::error;
use regex::Regex; use regex::Regex;
use std::iter::FromIterator; use std::{iter::FromIterator, path::PathBuf};
use std::{collections::HashSet, path::Path}; use std::{collections::HashSet, path::Path};
use thiserror::Error; use thiserror::Error;
@ -42,6 +42,8 @@ const STANDARD_EXCLUDES: &[&str] = &["../match/**/_*.yml"];
pub(crate) struct ResolvedConfig { pub(crate) struct ResolvedConfig {
parsed: ParsedConfig, parsed: ParsedConfig,
source_path: Option<PathBuf>,
// Generated properties // Generated properties
id: i32, id: i32,
match_paths: Vec<String>, match_paths: Vec<String>,
@ -55,6 +57,7 @@ impl Default for ResolvedConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
parsed: Default::default(), parsed: Default::default(),
source_path: None,
id: 0, id: 0,
match_paths: Vec::new(), match_paths: Vec::new(),
filter_title: None, filter_title: None,
@ -70,7 +73,17 @@ impl Config for ResolvedConfig {
} }
fn label(&self) -> &str { fn label(&self) -> &str {
self.parsed.label.as_deref().unwrap_or("none") if let Some(label) = self.parsed.label.as_deref() {
return label;
}
if let Some(source_path) = self.source_path.as_ref() {
if let Some(source_path) = source_path.to_str() {
return source_path
}
}
"none"
} }
fn match_paths(&self) -> &[String] { fn match_paths(&self) -> &[String] {
@ -291,6 +304,7 @@ impl ResolvedConfig {
Ok(Self { Ok(Self {
parsed: config, parsed: config,
source_path: Some(path.to_owned()),
id: next_id(), id: next_id(),
match_paths, match_paths,
filter_title, filter_title,