Skip to content

Commit

Permalink
Use Rust magic to be less verbose creating strings from vectors of Pa…
Browse files Browse the repository at this point in the history
…thPattern.
  • Loading branch information
jacobotb committed Mar 6, 2024
1 parent cfbb3a1 commit 8236e77
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
14 changes: 2 additions & 12 deletions crates/bins/src/bin/datadog-static-analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,10 @@ fn print_configuration(configuration: &CliConfiguration) {
let ignore_paths_str = if configuration.path_config.ignore.is_empty() {
"no ignore path".to_string()
} else {
configuration
.path_config
.ignore
.iter()
.map(|p| p.clone().into())
.collect::<Vec<String>>()
.join(",")
configuration.path_config.ignore.join(",")
};
let only_paths_str = match &configuration.path_config.only {
Some(x) => x
.iter()
.map(|p| p.clone().into())
.collect::<Vec<String>>()
.join(","),
Some(x) => x.join(","),
None => "all paths".to_string(),
};

Expand Down
14 changes: 4 additions & 10 deletions crates/cli/src/model/cli_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,11 @@ impl CliConfiguration {
// println!("rules string: {}", rules_string.join("|"));
let full_config_string = format!(
"{}:{}:{}:{}::{}:{}",
self.path_config.ignore.join(","),
self.path_config
.ignore
.iter()
.map(|p| p.clone().into())
.collect::<Vec<String>>()
.join(","),
self.path_config.only.as_ref().map_or("".to_string(), |v| v
.iter()
.map(|p| p.clone().into())
.collect::<Vec<String>>()
.join(",")),
.only
.as_ref()
.map_or("".to_string(), |v| v.join(",")),
self.ignore_gitignore,
rules_string.join(","),
self.max_file_size_kb,
Expand Down
13 changes: 11 additions & 2 deletions crates/cli/src/model/config_file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use globset::{GlobBuilder, GlobMatcher};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt;
use std::path::PathBuf;
Expand All @@ -8,6 +9,8 @@ use crate::model::serialization::{deserialize_ruleconfigs, deserialize_rulesetco
use serde;
use serde::{Deserialize, Serialize};

// A pattern for an 'only' or 'ignore' field. The 'glob' field contains a precompiled glob pattern,
// while the 'prefix' field contains a path prefix.
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
#[serde(from = "String", into = "String")]
pub struct PathPattern {
Expand Down Expand Up @@ -72,7 +75,7 @@ struct RawConfigFile {
paths: PathConfig,
// For backwards compatibility. Its content will be added to paths.ignore.
#[serde(rename = "ignore-paths")]
ignore_paths: Option<Vec<String>>,
ignore_paths: Option<Vec<PathPattern>>,
// Ignore all the paths in the .gitignore file.
#[serde(rename = "ignore-gitignore")]
ignore_gitignore: Option<bool>,
Expand All @@ -88,7 +91,7 @@ impl From<RawConfigFile> for ConfigFile {
paths: {
let mut paths = value.paths;
if let Some(ignore) = value.ignore_paths {
paths.ignore.extend(ignore.iter().map(|p| p.clone().into()));
paths.ignore.extend(ignore);
}
paths
},
Expand Down Expand Up @@ -119,6 +122,12 @@ impl From<String> for PathPattern {
}
}

impl Borrow<str> for PathPattern {
fn borrow(&self) -> &str {
self.prefix.to_str().unwrap_or("")
}
}

impl From<PathPattern> for String {
fn from(value: PathPattern) -> Self {
value.prefix.display().to_string()
Expand Down

0 comments on commit 8236e77

Please sign in to comment.