Skip to content

Commit

Permalink
[src/lib/types.rs] Create SerdeRegex so that the Regex type becomes…
Browse files Browse the repository at this point in the history
… serialisable & deserialisable
  • Loading branch information
SamuelMarks committed Aug 27, 2024
1 parent 24cda63 commit fbdfd89
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 23 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ path = "src/makers.rs"

[dependencies]
cargo_metadata = "^0.18"
ci_info = { version = "^0.14.14", features=["serde-1"] }
ci_info = { version = "^0.14.14", features = ["serde-1"] }
cliparser = "^0.1.2"
colored = "^2"
ctrlc = "^3"
Expand All @@ -56,7 +56,9 @@ duckscriptsdk = { version = "^0.9.3", default-features = false }
envmnt = "^0.10.4"
fern = "^0.6"
fsio = { version = "^0.4", features = ["temp-path"] }
git_info = { git = "https://github.com/SamuelMarks/git_info", branch = "serde", features = ["serde"] }
git_info = { git = "https://github.com/SamuelMarks/git_info", branch = "serde", features = [
"serde",
] }
glob = "^0.3.1"
home = "^0.5"
ignore = "^0.4"
Expand All @@ -69,7 +71,9 @@ petgraph = "^0.6.5"
regex = "^1.10"
run_script = "^0.10"
# rust_info = "^0.3.1"
rust_info = { git = "https://github.com/SamuelMarks/rust_info", branch = "serde", features = ["serde"] }
rust_info = { git = "https://github.com/SamuelMarks/rust_info", branch = "serde", features = [
"serde",
] }
semver = "^1"
serde = "^1"
serde_derive = "^1"
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cli_commands/diff_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::command;
use crate::error::CargoMakeError;
use crate::execution_plan::ExecutionPlanBuilder;
use crate::io::{create_file, delete_file};
use crate::types::{CliArgs, Config, CrateInfo, ExecutionPlan};
use crate::types::{CliArgs, Config, CrateInfo, ExecutionPlan, SerdeRegex};
use regex::Regex;
use std::fs::File;
use std::io;
Expand All @@ -32,7 +32,7 @@ pub(crate) fn run(
) -> Result<(), CargoMakeError> {
let skip_tasks_pattern = match cli_args.skip_tasks_pattern {
Some(ref pattern) => match Regex::new(pattern) {
Ok(reg) => Some(reg),
Ok(reg) => Some(SerdeRegex(reg)),
Err(_) => {
warn!("Invalid skip tasks pattern provided: {}", pattern);
None
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cli_commands/print_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::error::CargoMakeError;
use std::io;

use crate::execution_plan::ExecutionPlanBuilder;
use crate::types::{Config, CrateInfo, ExecutionPlan};
use crate::types::{Config, CrateInfo, ExecutionPlan, SerdeRegex};
use regex::Regex;

#[derive(Debug)]
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn print(
) -> Result<(), CargoMakeError> {
let skip_tasks_pattern_regex = match skip_tasks_pattern {
Some(ref pattern) => match Regex::new(pattern) {
Ok(reg) => Some(reg),
Ok(reg) => Some(SerdeRegex(reg)),
Err(_) => {
warn!("Invalid skip tasks pattern provided: {}", pattern);
None
Expand Down
12 changes: 6 additions & 6 deletions src/lib/execution_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use crate::logger;
use crate::profile;
use crate::proxy_task::create_proxy_task;
use crate::types::{
Config, CrateInfo, EnvValue, ExecutionPlan, ScriptValue, Step, Task, TaskIdentifier, Workspace,
Config, CrateInfo, EnvValue, ExecutionPlan, ScriptValue, SerdeRegex, Step, Task,
TaskIdentifier, Workspace,
};
use fsio::path::{get_basename, get_parent_directory};
use glob::Pattern;
use indexmap::IndexMap;
use regex::Regex;
use std::collections::HashSet;
use std::env;
use std::path::Path;
Expand Down Expand Up @@ -342,10 +342,10 @@ fn create_for_step(
task_names: &mut HashSet<String>,
root: bool,
allow_private: bool,
skip_tasks_pattern: Option<&Regex>,
skip_tasks_pattern: Option<&SerdeRegex>,
) -> Result<(), CargoMakeError> {
if let Some(skip_tasks_pattern_regex) = skip_tasks_pattern {
if skip_tasks_pattern_regex.is_match(&task.name) {
if skip_tasks_pattern_regex.0.is_match(&task.name) {
debug!("Skipping task: {} due to skip pattern.", &task.name);
return Ok(());
}
Expand Down Expand Up @@ -452,7 +452,7 @@ pub(crate) struct ExecutionPlanBuilder<'a> {
pub disable_workspace: bool,
pub allow_private: bool,
pub sub_flow: bool,
pub skip_tasks_pattern: Option<&'a Regex>,
pub skip_tasks_pattern: Option<&'a SerdeRegex>,
pub skip_init_end_tasks: bool,
}

Expand Down Expand Up @@ -507,7 +507,7 @@ impl<'a> ExecutionPlanBuilder<'a> {
}

let skip = match skip_tasks_pattern {
Some(pattern) => pattern.is_match(task),
Some(pattern) => pattern.0.is_match(task),
None => false,
};

Expand Down
8 changes: 5 additions & 3 deletions src/lib/execution_plan_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::*;
use crate::descriptor;
use crate::types::{ConfigSection, DependencyIdentifier, PlatformOverrideTask, TaskWatchOptions};
use crate::types::{
ConfigSection, DependencyIdentifier, PlatformOverrideTask, SerdeRegex, TaskWatchOptions,
};

#[test]
fn get_actual_task_name_not_found() {
Expand Down Expand Up @@ -1343,11 +1345,11 @@ fn create_with_dependencies_and_skip_filter() {
.tasks
.insert("task_dependency".to_string(), task_dependency);

let skip_filter = Regex::new("filtered.*").unwrap();
let skip_filter = regex::Regex::new("filtered.*").unwrap();

let execution_plan = ExecutionPlanBuilder {
allow_private: true,
skip_tasks_pattern: Some(&skip_filter),
skip_tasks_pattern: Some(&SerdeRegex(skip_filter)),
..ExecutionPlanBuilder::new(&config, "test")
}
.build()
Expand Down
6 changes: 3 additions & 3 deletions src/lib/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use crate::scriptengine;
use crate::time_summary;
use crate::types::{
CliArgs, Config, DeprecationInfo, EnvInfo, EnvValue, ExecutionPlan, FlowInfo, FlowState,
MaybeArray, RunTaskInfo, RunTaskName, RunTaskOptions, RunTaskRoutingInfo, Step, Task,
TaskWatchOptions,
MaybeArray, RunTaskInfo, RunTaskName, RunTaskOptions, RunTaskRoutingInfo, SerdeRegex, Step,
Task, TaskWatchOptions,
};

fn do_in_task_working_directory<F>(step: &Step, mut action: F) -> Result<(), CargoMakeError>
Expand Down Expand Up @@ -666,7 +666,7 @@ pub fn run(

let skip_tasks_pattern = match cli_args.skip_tasks_pattern {
Some(ref pattern) => match Regex::new(pattern) {
Ok(reg) => Some(reg),
Ok(reg) => Some(SerdeRegex(reg)),
Err(_) => {
warn!("Invalid skip tasks pattern provided: {}", pattern);
None
Expand Down
4 changes: 2 additions & 2 deletions src/lib/runner_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
use crate::test;
use crate::types::{
ConditionScriptValue, ConfigSection, CrateInfo, EnvFile, RunTaskDetails, ScriptValue,
TaskCondition,
SerdeRegex, TaskCondition,
};
use cfg_if::cfg_if;
use git_info::types::GitInfo;
Expand Down Expand Up @@ -81,7 +81,7 @@ fn run_flow_private_skipped() {
disable_on_error: false,
allow_private: false,
skip_init_end_tasks: false,
skip_tasks_pattern: Some(Regex::new("test").unwrap()),
skip_tasks_pattern: Some(SerdeRegex(Regex::new("test").unwrap())),
cli_arguments: None,
};

Expand Down
28 changes: 26 additions & 2 deletions src/lib/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::plugin::types::Plugins;
use ci_info::types::CiInfo;
use git_info::types::GitInfo;
use indexmap::{IndexMap, IndexSet};
use regex::Regex;
use rust_info::types::RustInfo;
use std::collections::HashMap;

Expand Down Expand Up @@ -308,6 +307,31 @@ pub struct EnvInfo {
pub ci_info: CiInfo,
}

#[derive(Clone, Debug)]
pub struct SerdeRegex(pub regex::Regex);

impl serde::Serialize for SerdeRegex {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let s = format!("{}", self.0);
serializer.serialize_str(&s)
}
}

impl<'de> serde::Deserialize<'de> for SerdeRegex {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(SerdeRegex(
regex::Regex::new(s.as_str()).map_err(serde::de::Error::custom)?,
))
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "diesel", derive(diesel::Insertable))]
/// Holds flow information
Expand All @@ -327,7 +351,7 @@ pub struct FlowInfo {
/// If true, the init and end tasks are skipped
pub skip_init_end_tasks: bool,
/// Skip tasks that match the provided pattern
pub skip_tasks_pattern: Option<Regex>,
pub skip_tasks_pattern: Option<SerdeRegex>,
/// additional command line arguments
pub cli_arguments: Option<Vec<String>>,
}
Expand Down

0 comments on commit fbdfd89

Please sign in to comment.