diff --git a/crates/pollard-cli/src/io.rs b/crates/pollard-cli/src/io.rs index 0c2f2d7c..54206d48 100644 --- a/crates/pollard-cli/src/io.rs +++ b/crates/pollard-cli/src/io.rs @@ -1,17 +1,8 @@ use serde::Serialize; use std::path::PathBuf; -pub use pollard_session::{DiffStyle, Session, Style}; - -pub trait Report { - fn render(&self, style: &Style, no_color: bool, depth: u8); - fn get_dir(&self, session: &Session) -> PathBuf; - fn make_path(&self, session: &Session) -> PathBuf; -} - -pub fn hashed_path(dir: &PathBuf, content: &str, label: &str) -> PathBuf { - dir.join(format!("{}.{label}.json", pollard_core::Hash::of(content))) -} +pub use pollard_core::io::{DiffStyle, Render, Style, hashed_path}; +pub use pollard_session::{Report, Session}; #[derive(Debug, Clone, Serialize)] pub enum Action { @@ -79,16 +70,7 @@ fn color(code: &str, text: &str, no_color: bool) -> String { } } -impl Report for Action { - fn get_dir(&self, session: &Session) -> PathBuf { - session.report_dir.join("action") - } - - fn make_path(&self, session: &Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "action") - } - +impl Render for Action { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -140,6 +122,17 @@ impl Report for Action { } } +impl Report for Action { + fn get_dir(&self, session: &Session) -> PathBuf { + session.report_dir.join("action") + } + + fn make_path(&self, session: &Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "action") + } +} + pub struct SessionReport { json: String, debug: String, @@ -154,6 +147,15 @@ impl SessionReport { } } +impl Render for SessionReport { + fn render(&self, style: &Style, _no_color: bool, _depth: u8) { + match style { + Style::Json => println!("{}", self.json), + _ => println!("{}", self.debug), + } + } +} + impl Report for SessionReport { fn get_dir(&self, session: &Session) -> PathBuf { session.report_dir.join("session") @@ -162,11 +164,4 @@ impl Report for SessionReport { fn make_path(&self, session: &Session) -> PathBuf { hashed_path(&self.get_dir(session), &self.json, "session") } - - fn render(&self, style: &Style, _no_color: bool, _depth: u8) { - match style { - Style::Json => println!("{}", self.json), - _ => println!("{}", self.debug), - } - } } diff --git a/crates/pollard-cli/src/main.rs b/crates/pollard-cli/src/main.rs index 699be9fb..27b3c8f5 100644 --- a/crates/pollard-cli/src/main.rs +++ b/crates/pollard-cli/src/main.rs @@ -4,7 +4,7 @@ mod steps; use clap::{CommandFactory, Parser, Subcommand}; use clap_complete::Shell; -use io::{Action, Report}; +use io::{Action, Render, Report}; use pollard_core::Hash; use pollard_session::{PartialSession, Session, SessionSkipped, discover_config, read_config}; use std::path::PathBuf; diff --git a/crates/pollard-cli/src/mutate/apply.rs b/crates/pollard-cli/src/mutate/apply.rs index 3cc9c5ea..c2b2df9d 100644 --- a/crates/pollard-cli/src/mutate/apply.rs +++ b/crates/pollard-cli/src/mutate/apply.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, Report, Style, hashed_path}; +use crate::io::{Action, Render, Report, Style, hashed_path}; use crate::mutate::find_mutated; use pollard_core::config::LanguageId; use pollard_core::{Hash, SourceFile}; @@ -42,16 +42,7 @@ pub struct ApplyReport { pub mutated_hash: Hash, } -impl Report for ApplyReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session.report_dir.join("mutate").join("apply") - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "applied") - } - +impl Render for ApplyReport { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -81,3 +72,15 @@ impl Report for ApplyReport { } } } + +impl Report for ApplyReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("mutate").join("apply") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "applied") + } + +} diff --git a/crates/pollard-cli/src/mutate/generate.rs b/crates/pollard-cli/src/mutate/generate.rs index 00d4d77f..5d57c4ae 100644 --- a/crates/pollard-cli/src/mutate/generate.rs +++ b/crates/pollard-cli/src/mutate/generate.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, Report, Style, hashed_path}; +use crate::io::{Action, Render, Report, Style, hashed_path}; use crate::steps::expand_glob; use pollard_core::config::LanguageId; use pollard_core::languages::javascript::JavaScript; @@ -63,16 +63,7 @@ pub struct GenerateReport { pub replacement: String, } -impl Report for GenerateReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session.report_dir.join("mutate").join("generate") - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "mutations") - } - +impl Render for GenerateReport { fn render(&self, style: &Style, _no_color: bool, _depth: u8) { match style { Style::Json => { @@ -112,3 +103,15 @@ impl Report for GenerateReport { } } } + +impl Report for GenerateReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("mutate").join("generate") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "mutations") + } + +} diff --git a/crates/pollard-cli/src/mutate/view.rs b/crates/pollard-cli/src/mutate/view.rs index ed5901bc..985b0817 100644 --- a/crates/pollard-cli/src/mutate/view.rs +++ b/crates/pollard-cli/src/mutate/view.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, DiffStyle, Report, Style, hashed_path}; +use crate::io::{Action, DiffStyle, Render, Report, Style, hashed_path}; use std::path::PathBuf; use crate::mutate::find_mutated; use pollard_core::config::LanguageId; @@ -33,6 +33,17 @@ pub struct ViewReport { pub diff_style: DiffStyle, } +impl Render for ViewReport { + fn render(&self, _style: &Style, no_color: bool, _depth: u8) { + match self.diff_style { + DiffStyle::Unified => render_unified(&self.old, &self.new, &self.path, no_color), + DiffStyle::SideBySide => { + render_side_by_side(&self.old, &self.new, &self.path, no_color) + } + } + } +} + impl Report for ViewReport { fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { session.report_dir.join("mutate").join("view") @@ -43,14 +54,6 @@ impl Report for ViewReport { hashed_path(&self.get_dir(session), &content, "diff") } - fn render(&self, _style: &Style, no_color: bool, _depth: u8) { - match self.diff_style { - DiffStyle::Unified => render_unified(&self.old, &self.new, &self.path, no_color), - DiffStyle::SideBySide => { - render_side_by_side(&self.old, &self.new, &self.path, no_color) - } - } - } } fn color(code: &str, text: &str, no_color: bool) -> String { diff --git a/crates/pollard-cli/src/steps/apply_mutant_to_workspace.rs b/crates/pollard-cli/src/steps/apply_mutant_to_workspace.rs index 9654e7b3..e501157a 100644 --- a/crates/pollard-cli/src/steps/apply_mutant_to_workspace.rs +++ b/crates/pollard-cli/src/steps/apply_mutant_to_workspace.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, Report, Style, hashed_path}; +use crate::io::{Action, Render, Report, Style, hashed_path}; use pollard_session::Session; use crate::steps::{color, find_workspace, read_plan, read_workspace_manifest}; use pollard_core::Hash; @@ -66,19 +66,7 @@ pub struct ApplyMutantToWorkspaceReport { pub mutated_hash: Hash, } -impl Report for ApplyMutantToWorkspaceReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session - .report_dir - .join("step") - .join("apply-mutant-to-workspace") - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "applied") - } - +impl Render for ApplyMutantToWorkspaceReport { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -108,3 +96,18 @@ impl Report for ApplyMutantToWorkspaceReport { } } } + +impl Report for ApplyMutantToWorkspaceReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session + .report_dir + .join("step") + .join("apply-mutant-to-workspace") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "applied") + } + +} diff --git a/crates/pollard-cli/src/steps/cleanup.rs b/crates/pollard-cli/src/steps/cleanup.rs index 5309d5f9..e02c5210 100644 --- a/crates/pollard-cli/src/steps/cleanup.rs +++ b/crates/pollard-cli/src/steps/cleanup.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, Report, Style, hashed_path}; +use crate::io::{Action, Render, Report, Style, hashed_path}; use pollard_session::Session; use crate::steps::{color, expand_glob}; use serde::Serialize; @@ -51,16 +51,7 @@ pub struct CleanupReport { pub manifest_count: usize, } -impl Report for CleanupReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session.report_dir.join("step").join("cleanup") - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "cleanup") - } - +impl Render for CleanupReport { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -85,3 +76,15 @@ impl Report for CleanupReport { } } } + +impl Report for CleanupReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("step").join("cleanup") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "cleanup") + } + +} diff --git a/crates/pollard-cli/src/steps/create_workspaces.rs b/crates/pollard-cli/src/steps/create_workspaces.rs index f932e4e0..6011571b 100644 --- a/crates/pollard-cli/src/steps/create_workspaces.rs +++ b/crates/pollard-cli/src/steps/create_workspaces.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, Report, Style, hashed_path}; +use crate::io::{Action, Render, Report, Style, hashed_path}; use pollard_session::Session; use crate::steps::{color, content_id}; use pollard_core::config::Vcs; @@ -63,16 +63,7 @@ pub struct CreateWorkspacesReport { pub manifest: PathBuf, } -impl Report for CreateWorkspacesReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session.report_dir.join("step").join("create-workspaces") - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "workspaces") - } - +impl Render for CreateWorkspacesReport { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -104,3 +95,15 @@ impl Report for CreateWorkspacesReport { } } } + +impl Report for CreateWorkspacesReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("step").join("create-workspaces") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "workspaces") + } + +} diff --git a/crates/pollard-cli/src/steps/derive_mutants.rs b/crates/pollard-cli/src/steps/derive_mutants.rs index e1f13b6b..ebe9e091 100644 --- a/crates/pollard-cli/src/steps/derive_mutants.rs +++ b/crates/pollard-cli/src/steps/derive_mutants.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, Report, Style, hashed_path}; +use crate::io::{Action, Render, Report, Style, hashed_path}; use pollard_session::Session; use crate::steps::{color, content_id}; use pollard_core::config::LanguageId; @@ -77,16 +77,7 @@ pub struct DeriveMutantsReport { pub count: usize, } -impl Report for DeriveMutantsReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session.report_dir.join("step").join("derive-mutants") - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "plan") - } - +impl Render for DeriveMutantsReport { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -112,3 +103,15 @@ impl Report for DeriveMutantsReport { } } } + +impl Report for DeriveMutantsReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("step").join("derive-mutants") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "plan") + } + +} diff --git a/crates/pollard-cli/src/steps/find_files.rs b/crates/pollard-cli/src/steps/find_files.rs index c9323d15..b869dbd4 100644 --- a/crates/pollard-cli/src/steps/find_files.rs +++ b/crates/pollard-cli/src/steps/find_files.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, Report, Style, hashed_path}; +use crate::io::{Action, Render, Report, Style, hashed_path}; use pollard_session::Session; use crate::steps::expand_glob; use serde::Serialize; @@ -21,16 +21,7 @@ pub struct FindFilesReport { pub files: Vec, } -impl Report for FindFilesReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session.report_dir.join("step").join("find-files") - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "files") - } - +impl Render for FindFilesReport { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -58,3 +49,15 @@ impl Report for FindFilesReport { } } } + +impl Report for FindFilesReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("step").join("find-files") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "files") + } + +} diff --git a/crates/pollard-cli/src/steps/mod.rs b/crates/pollard-cli/src/steps/mod.rs index 2e773084..e64fa8e8 100644 --- a/crates/pollard-cli/src/steps/mod.rs +++ b/crates/pollard-cli/src/steps/mod.rs @@ -7,7 +7,7 @@ pub mod reset_workspace; pub mod setup_workspace; pub mod test_workspace; -use crate::io::{Report, Style, hashed_path}; +use crate::io::{Render, Report, Style, hashed_path}; use pollard_session::Session; use pollard_core::plan::{Plan, WorkspaceManifest}; use serde::Serialize; @@ -140,16 +140,7 @@ pub struct CommandReport { pub result: Result, } -impl Report for CommandReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session.report_dir.join("step").join(&self.step) - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "command") - } - +impl Render for CommandReport { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -188,3 +179,15 @@ impl Report for CommandReport { } } } + +impl Report for CommandReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("step").join(&self.step) + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "command") + } + +} diff --git a/crates/pollard-cli/src/steps/reset_workspace.rs b/crates/pollard-cli/src/steps/reset_workspace.rs index 5875ce77..8497d522 100644 --- a/crates/pollard-cli/src/steps/reset_workspace.rs +++ b/crates/pollard-cli/src/steps/reset_workspace.rs @@ -1,4 +1,4 @@ -use crate::io::{Action, Report, Style, hashed_path}; +use crate::io::{Action, Render, Report, Style, hashed_path}; use pollard_session::Session; use crate::steps::{color, find_workspace, read_workspace_manifest}; use pollard_core::config::Vcs; @@ -50,16 +50,7 @@ pub struct ResetWorkspaceReport { pub rev: String, } -impl Report for ResetWorkspaceReport { - fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { - session.report_dir.join("step").join("reset-workspace") - } - - fn make_path(&self, session: &pollard_session::Session) -> PathBuf { - let content = serde_json::to_string(self).expect("failed to serialize"); - hashed_path(&self.get_dir(session), &content, "reset") - } - +impl Render for ResetWorkspaceReport { fn render(&self, style: &Style, no_color: bool, _depth: u8) { match style { Style::Json => { @@ -81,3 +72,15 @@ impl Report for ResetWorkspaceReport { } } } + +impl Report for ResetWorkspaceReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("step").join("reset-workspace") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "reset") + } + +} diff --git a/crates/pollard-core/src/io.rs b/crates/pollard-core/src/io.rs new file mode 100644 index 00000000..d5398b8f --- /dev/null +++ b/crates/pollard-core/src/io.rs @@ -0,0 +1,27 @@ +use crate::Hash; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +#[derive(Debug, Clone, Serialize, Deserialize, clap::ValueEnum)] +#[serde(rename_all = "lowercase")] +pub enum Style { + Plain, + Pretty, + Json, + Markdown, +} + +#[derive(Debug, Clone, Serialize, Deserialize, clap::ValueEnum)] +#[serde(rename_all = "kebab-case")] +pub enum DiffStyle { + Unified, + SideBySide, +} + +pub trait Render { + fn render(&self, style: &Style, no_color: bool, depth: u8); +} + +pub fn hashed_path(dir: &PathBuf, content: &str, label: &str) -> PathBuf { + dir.join(format!("{}.{label}.json", Hash::of(content))) +} diff --git a/crates/pollard-core/src/lib.rs b/crates/pollard-core/src/lib.rs index ed370912..67c45818 100644 --- a/crates/pollard-core/src/lib.rs +++ b/crates/pollard-core/src/lib.rs @@ -1,4 +1,5 @@ pub mod config; +pub mod io; pub mod languages; pub mod plan; diff --git a/crates/pollard-session/src/lib.rs b/crates/pollard-session/src/lib.rs index 1715ccbc..ef6b6ea8 100644 --- a/crates/pollard-session/src/lib.rs +++ b/crates/pollard-session/src/lib.rs @@ -1,6 +1,7 @@ mod config; pub use config::{discover_config, read_config, ConfigError, SEARCH_PATHS}; +pub use pollard_core::io::{DiffStyle, Render, Style, hashed_path}; use pollard_core::config::{LanguageId, Ordering, Vcs}; use pollard_session_derive::Settings; @@ -26,20 +27,9 @@ impl Session { } } -#[derive(Debug, Clone, Serialize, serde::Deserialize, clap::ValueEnum)] -#[serde(rename_all = "lowercase")] -pub enum Style { - Plain, - Pretty, - Json, - Markdown, -} - -#[derive(Debug, Clone, Serialize, serde::Deserialize, clap::ValueEnum)] -#[serde(rename_all = "kebab-case")] -pub enum DiffStyle { - Unified, - SideBySide, +pub trait Report: Render { + fn get_dir(&self, session: &Session) -> PathBuf; + fn make_path(&self, session: &Session) -> PathBuf; } #[derive(Debug, Clone, Serialize, Settings)]