Skip to content

Commit

Permalink
fix: remove min_level spread throughout the codebase (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Aug 8, 2024
1 parent c206ec2 commit c5c8330
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 59 deletions.
5 changes: 1 addition & 4 deletions crates/cli/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ macro_rules! emit_error {
range: None,
source: None,
});
$emitter.emit(&log, $min_level).unwrap();
$emitter.emit(&log).unwrap();
return $emitter;
}
}
Expand Down Expand Up @@ -189,7 +189,6 @@ where
let cache_ref = &cache;

let mut interactive = arg.interactive;
let min_level = &arg.visibility;

let (found_count, disk_paths) = match my_input {
ApplyInput::Disk(ref my_input) => {
Expand Down Expand Up @@ -240,7 +239,6 @@ where
vec![log, done_file],
details,
arg.dry_run,
min_level,
arg.format,
&mut interactive,
None,
Expand Down Expand Up @@ -312,7 +310,6 @@ where
message,
details,
arg.dry_run,
min_level,
arg.format,
&mut interactive,
pg,
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub(crate) async fn run_apply(
ranges,
args.apply_migration_args,
flags,
args.apply_pattern_args.visibility,
)
.await;
}
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/src/commands/apply_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub(crate) async fn run_apply_migration(
ranges: Option<Vec<marzano_util::diff::FileDiff>>,
arg: ApplyMigrationArgs,
flags: &GlobalFormatFlags,
min_level: marzano_messenger::emit::VisibilityLevels,
) -> Result<()> {
let input = arg.get_payload()?;

Expand All @@ -75,6 +76,7 @@ pub(crate) async fn run_apply_migration(
false,
None,
None,
min_level,
)
.await?;

Expand Down
11 changes: 6 additions & 5 deletions crates/cli/src/commands/apply_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ pub(crate) async fn run_apply_pattern(
interactive,
Some(&pattern),
root_path.as_ref(),
*min_level,
)
.await?;

Expand Down Expand Up @@ -380,7 +381,7 @@ pub(crate) async fn run_apply_pattern(
range: None,
source: None,
});
emitter.emit(&log, min_level).unwrap();
emitter.emit(&log).unwrap();
emitter.flush().await?;
if format.is_always_ok().0 {
return Ok(());
Expand Down Expand Up @@ -463,7 +464,7 @@ pub(crate) async fn run_apply_pattern(
found: 0,
reason: AllDoneReason::NoInputPaths,
});
emitter.emit(&all_done, min_level).unwrap();
emitter.emit(&all_done).unwrap();
emitter.flush().await?;

return Ok(());
Expand Down Expand Up @@ -511,7 +512,7 @@ pub(crate) async fn run_apply_pattern(
},
};
emitter
.emit(&MatchResult::AnalysisLog(log.clone()), min_level)
.emit(&MatchResult::AnalysisLog(log.clone()))
.unwrap();
emitter.flush().await?;
match format.is_always_ok() {
Expand All @@ -526,7 +527,7 @@ pub(crate) async fn run_apply_pattern(
};
for warn in compilation_warnings.clone().into_iter() {
emitter
.emit(&MatchResult::AnalysisLog(warn.into()), min_level)
.emit(&MatchResult::AnalysisLog(warn.into()))
.unwrap();
}

Expand Down Expand Up @@ -568,7 +569,7 @@ pub(crate) async fn run_apply_pattern(
reason: AllDoneReason::AllMatchesFound,
});

emitter.emit(&all_done, min_level).unwrap();
emitter.emit(&all_done).unwrap();

emitter.flush().await?;

Expand Down
13 changes: 5 additions & 8 deletions crates/cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use marzano_core::{
};
use marzano_gritmodule::{config::ResolvedGritDefinition, utils::extract_path};
use marzano_language::target_language::{expand_paths, PatternLanguage};
use marzano_messenger::emit::FlushableMessenger as _;
use marzano_messenger::emit::{FlushableMessenger as _, VisibilityLevels};
use marzano_util::cache::GritCache;
use marzano_util::rich_path::RichPath;
use marzano_util::{finder::get_input_files, rich_path::RichFile};
Expand All @@ -25,7 +25,7 @@ use std::{
};
use tokio::try_join;

use marzano_messenger::emit::{Messager, VisibilityLevels};
use marzano_messenger::emit::Messager;

#[cfg(feature = "server")]
use cli_server::check::CheckMessenger;
Expand Down Expand Up @@ -251,6 +251,7 @@ pub(crate) async fn run_check(
false,
None,
root_path.as_ref(),
VisibilityLevels::Supplemental,
)
.await?;

Expand Down Expand Up @@ -300,9 +301,7 @@ pub(crate) async fn run_check(
};
let rewrite_with_reason = rewrite_with_reason.as_ref();
let message = rewrite_with_reason.unwrap_or(&result.result);
emitter
.emit(message, &VisibilityLevels::Supplemental)
.unwrap();
emitter.emit(message).unwrap();
}
}
let safe_total_file_count = std::cmp::min(total_file_count, i32::MAX as usize) as i32;
Expand All @@ -311,9 +310,7 @@ pub(crate) async fn run_check(
found: 0,
reason: AllDoneReason::AllMatchesFound,
});
emitter
.emit(&all_done, &VisibilityLevels::Supplemental)
.unwrap();
emitter.emit(&all_done).unwrap();

emitter.flush().await?;

Expand Down
9 changes: 5 additions & 4 deletions crates/cli/src/commands/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ pub(crate) async fn run_parse(
bail!("Only JSONL output is supported for parse command");
}

let mut emitter = JSONLineMessenger::new(io::stdout(), OutputMode::default());
let visibility = VisibilityLevels::Hidden;

let mut emitter = JSONLineMessenger::new(io::stdout(), OutputMode::default(), visibility);

let parse_input = ParseInput {
pattern_body: pattern_body.to_owned().unwrap_or_default(),
Expand All @@ -54,11 +56,10 @@ pub(crate) async fn run_parse(
let lang: TargetLanguage = PatternLanguage::get_language(&parse_input.pattern_body)
.unwrap_or_default()
.try_into()?;
let visibility = VisibilityLevels::Hidden;

if let Some(body) = pattern_body {
let result = parse_one_pattern(body, None).await?;
emitter.emit(&result, &visibility)?;
emitter.emit(&result)?;
}

for path in parse_input.paths {
Expand All @@ -75,7 +76,7 @@ pub(crate) async fn run_parse(
};
MatchResult::InputFile(input_file)
};
emitter.emit(&match_result, &visibility)?;
emitter.emit(&match_result)?;
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/src/commands/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use marzano_gritmodule::fetcher::KeepFetcherKind;
use marzano_gritmodule::patterns_directory::PatternsDirectory;
use marzano_gritmodule::searcher::find_grit_modules_dir;
use marzano_gritmodule::utils::is_pattern_name;
use marzano_messenger::emit::ApplyDetails;
use marzano_messenger::emit::{ApplyDetails, VisibilityLevels};
use serde::{Deserialize, Serialize};
use std::env::current_dir;
use std::io::{stdin, Read};
Expand Down Expand Up @@ -316,6 +316,7 @@ pub(crate) async fn run_plumbing(
verbose: true,
},
&parent,
VisibilityLevels::default(),
)
.await
}
Expand Down
17 changes: 15 additions & 2 deletions crates/cli/src/jsonl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,36 @@ use std::{
use anyhow::anyhow;
use marzano_core::{api::MatchResult, compact_api::compact};

use marzano_messenger::{emit::Messager, output_mode::OutputMode};
use marzano_messenger::{
emit::{Messager, VisibilityLevels},
output_mode::OutputMode,
};

pub struct JSONLineMessenger<'a> {
writer: Arc<Mutex<Box<dyn Write + Send + 'a>>>,
mode: OutputMode,
min_level: VisibilityLevels,
}

impl<'a> JSONLineMessenger<'a> {
pub fn new<W: Write + Send + 'static>(writer: W, mode: OutputMode) -> Self {
pub fn new<W: Write + Send + 'static>(
writer: W,
mode: OutputMode,
min_level: VisibilityLevels,
) -> Self {
Self {
writer: Arc::new(Mutex::new(Box::new(writer))),
mode,
min_level,
}
}
}

impl<'a> Messager for JSONLineMessenger<'a> {
fn get_min_level(&self) -> VisibilityLevels {
self.min_level
}

fn raw_emit(&mut self, item: &MatchResult) -> anyhow::Result<()> {
let mut writer = self
.writer
Expand Down
30 changes: 23 additions & 7 deletions crates/cli/src/messenger_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ pub enum MessengerVariant<'a> {
}

impl<'a> Messager for MessengerVariant<'a> {
fn get_min_level(&self) -> VisibilityLevels {
match self {
MessengerVariant::Formatted(m) => m.get_min_level(),
MessengerVariant::Transformed(m) => m.get_min_level(),
MessengerVariant::JsonLine(m) => m.get_min_level(),
#[cfg(feature = "remote_redis")]
MessengerVariant::Redis(m) => m.get_min_level(),
#[cfg(feature = "remote_pubsub")]
MessengerVariant::GooglePubSub(m) => m.get_min_level(),
#[cfg(feature = "server")]
MessengerVariant::Combined(m) => m.get_min_level(),
}
}

fn raw_emit(&mut self, message: &marzano_core::api::MatchResult) -> anyhow::Result<()> {
match self {
MessengerVariant::Formatted(m) => m.raw_emit(message),
Expand Down Expand Up @@ -140,16 +154,13 @@ impl<'a> WorkflowMessenger for MessengerVariant<'a> {
&mut self,
message: &marzano_messenger::workflows::WorkflowMatchResult,
) -> anyhow::Result<()> {
// This is meant to match what we do in the CLI server
let level = VisibilityLevels::Debug;

match self {
MessengerVariant::Formatted(_)
| MessengerVariant::Transformed(_)
| MessengerVariant::JsonLine(_) => {
// For local emitters,, we will also apply rewrites
self.emit(&message.result, &level)?;
self.apply_rewrite(&message.result, &level)?;
self.emit(&message.result)?;
self.apply_rewrite(&message.result)?;
Ok(())
}
#[cfg(feature = "remote_redis")]
Expand Down Expand Up @@ -237,6 +248,7 @@ pub async fn create_emitter<'a>(
interactive: bool,
pattern: Option<&str>,
_root_path: Option<&PathBuf>,
min_level: VisibilityLevels,
) -> Result<MessengerVariant<'a>> {
let writer: Option<Box<dyn Write + Send>> = if let Some(output_file) = output_file {
let file = fs_err::File::create(output_file)?;
Expand All @@ -252,15 +264,19 @@ pub async fn create_emitter<'a>(
mode,
interactive,
pattern.unwrap_or_default().to_string(),
min_level,
)
.into(),
OutputFormat::Json => {
bail!("JSON output is not supported for apply_pattern");
}
OutputFormat::Transformed => TransformedMessenger::new(writer).into(),
OutputFormat::Jsonl => {
let jsonl =
JSONLineMessenger::new(writer.unwrap_or_else(|| Box::new(io::stdout())), mode);
let jsonl = JSONLineMessenger::new(
writer.unwrap_or_else(|| Box::new(io::stdout())),
mode,
min_level,
);
jsonl.into()
}
#[cfg(feature = "remote_redis")]
Expand Down
14 changes: 12 additions & 2 deletions crates/cli/src/result_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{
};

use crate::ux::{format_result_diff, indent};
use marzano_messenger::emit::Messager;
use marzano_messenger::emit::{Messager, VisibilityLevels};

#[derive(Debug)]
pub enum FormattedResult {
Expand Down Expand Up @@ -302,7 +302,7 @@ pub struct FormattedMessager<'a> {
total_rejected: usize,
total_supressed: usize,
input_pattern: String,

min_level: VisibilityLevels,
workflow_done: bool,
}

Expand All @@ -312,6 +312,7 @@ impl<'a> FormattedMessager<'_> {
mode: OutputMode,
interactive: bool,
input_pattern: String,
min_level: VisibilityLevels,
) -> FormattedMessager<'a> {
FormattedMessager {
writer: writer.map(|w| Arc::new(Mutex::new(w))),
Expand All @@ -321,12 +322,17 @@ impl<'a> FormattedMessager<'_> {
total_rejected: 0,
total_supressed: 0,
input_pattern,
min_level,
workflow_done: false,
}
}
}

impl Messager for FormattedMessager<'_> {
fn get_min_level(&self) -> VisibilityLevels {
self.min_level
}

fn raw_emit(&mut self, message: &MatchResult) -> anyhow::Result<()> {
if self.interactive && !(self.mode == OutputMode::None) {
if let MatchResult::AllDone(item) = message {
Expand Down Expand Up @@ -441,6 +447,10 @@ impl<'a> TransformedMessenger<'_> {
}

impl Messager for TransformedMessenger<'_> {
fn get_min_level(&self) -> VisibilityLevels {
VisibilityLevels::Primary
}

fn raw_emit(&mut self, message: &MatchResult) -> anyhow::Result<()> {
match message {
MatchResult::PatternInfo(_)
Expand Down
6 changes: 2 additions & 4 deletions crates/cli/src/ux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use marzano_core::{
api::{EnforcementLevel, MatchResult},
fs::extract_ranges,
};
use marzano_gritmodule::{
config::ResolvedGritDefinition, testing::SampleTestResult,
};
use marzano_gritmodule::{config::ResolvedGritDefinition, testing::SampleTestResult};
use similar::{ChangeTag, TextDiff};
use std::{collections::HashMap};
use std::collections::HashMap;

use crate::analyze::{extract_rewritten_content, group_checks};

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/test_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use marzano_language::target_language::TargetLanguage;
use self::pattern_compiler::src_to_problem_libs;
use crate::api::FileMatchResult;
use crate::{
api::{MatchResult},
api::MatchResult,
test_utils::{run_on_test_files, SyntheticFile},
};

Expand Down
Loading

0 comments on commit c5c8330

Please sign in to comment.