Allow for different diagnostic severities in gen_diagnostic

This commit is contained in:
elkowar 2021-07-28 17:12:58 +02:00
parent b836549232
commit fcd54abf91
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
3 changed files with 29 additions and 11 deletions

View file

@ -1,6 +1,7 @@
use std::process::Command;
use anyhow::*;
use codespan_reporting::diagnostic::Severity;
use eww_shared_util::{Span, VarName};
use simplexpr::dynval::DynVal;
use yuck::{
@ -10,10 +11,12 @@ use yuck::{
use crate::error::DiagError;
pub fn create_script_var_failed_error(span: Span, var_name: &VarName) -> DiagError {
pub fn create_script_var_failed_warn(span: Span, var_name: &VarName, error_output: &str) -> DiagError {
DiagError::new(gen_diagnostic! {
msg = format!("Failed to compute value for `{}`", var_name),
kind = Severity::Warning,
msg = format!("The script for the `{}`-variable exited unsuccessfully", var_name),
label = span => "Defined here",
note = error_output,
})
}
@ -23,7 +26,9 @@ pub fn initial_value(var: &ScriptVarDefinition) -> Result<DynVal> {
VarSource::Function(f) => {
f().map_err(|err| anyhow!(err)).with_context(|| format!("Failed to compute initial value for {}", &var.name()))
}
VarSource::Shell(span, f) => run_command(f).map_err(|_| anyhow!(create_script_var_failed_error(*span, var.name()))),
VarSource::Shell(span, f) => {
run_command(f).map_err(|e| anyhow!(create_script_var_failed_warn(*span, var.name(), &e.to_string())))
}
},
ScriptVarDefinition::Listen(_) => Ok(DynVal::from_string(String::new())),
}
@ -34,7 +39,7 @@ pub fn run_command(cmd: &str) -> Result<DynVal> {
log::debug!("Running command: {}", cmd);
let command = Command::new("/bin/sh").arg("-c").arg(cmd).output()?;
if !command.status.success() {
bail!("Execution of `{}` failed", cmd);
bail!("Failed with output:\n{}", String::from_utf8(command.stderr)?);
}
let output = String::from_utf8(command.stdout)?;
let output = output.trim_matches('\n');

View file

@ -1,6 +1,9 @@
use std::collections::HashMap;
use crate::{app, config::create_script_var_failed_error};
use crate::{
app,
config::{create_script_var_failed_warn, script_var},
};
use anyhow::*;
use app::DaemonCommand;
@ -169,7 +172,7 @@ impl PollVarHandler {
fn run_poll_once(var: &PollScriptVar) -> Result<DynVal> {
match &var.command {
VarSource::Shell(span, x) => {
crate::config::script_var::run_command(x).map_err(|_| anyhow!(create_script_var_failed_error(*span, &var.name)))
script_var::run_command(x).map_err(|e| anyhow!(create_script_var_failed_warn(*span, &var.name, &e.to_string())))
}
VarSource::Function(x) => x().map_err(|e| anyhow!(e)),
}

View file

@ -20,12 +20,14 @@ fn span_to_secondary_label(span: Span) -> Label<usize> {
#[macro_export]
macro_rules! gen_diagnostic {
(
$(msg = $msg:expr)?
$(, label = $span:expr $(=> $label:expr)?)?
$(, note = $note:expr)? $(,)?
( $(kind = $kind:expr,)?
$(msg = $msg:expr)?
$(, label = $span:expr $(=> $label:expr)?)?
$(, note = $note:expr)? $(,)?
) => {
::codespan_reporting::diagnostic::Diagnostic::error()
::codespan_reporting::diagnostic::Diagnostic::new(gen_diagnostic! {
@macro_fallback $({$kind})? {::codespan_reporting::diagnostic::Severity::Error}
})
$(.with_message($msg.to_string()))?
$(.with_labels(vec![
::codespan_reporting::diagnostic::Label::primary($span.2, $span.0..$span.1)
@ -38,6 +40,14 @@ macro_rules! gen_diagnostic {
.with_message($msg.to_string())
$(.with_labels(vec![::codespan_reporting::diagnostic::Label::primary($span.2, $span.0..$span.1)]))?
}};
(@macro_fallback { $value:expr } { $fallback:expr }) => {
$value
};
(@macro_fallback { $fallback:expr }) => {
$fallback
};
}
pub trait DiagnosticExt: Sized {