Allow for different diagnostic severities in gen_diagnostic
This commit is contained in:
parent
b836549232
commit
fcd54abf91
3 changed files with 29 additions and 11 deletions
|
@ -1,6 +1,7 @@
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
use anyhow::*;
|
use anyhow::*;
|
||||||
|
use codespan_reporting::diagnostic::Severity;
|
||||||
use eww_shared_util::{Span, VarName};
|
use eww_shared_util::{Span, VarName};
|
||||||
use simplexpr::dynval::DynVal;
|
use simplexpr::dynval::DynVal;
|
||||||
use yuck::{
|
use yuck::{
|
||||||
|
@ -10,10 +11,12 @@ use yuck::{
|
||||||
|
|
||||||
use crate::error::DiagError;
|
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! {
|
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",
|
label = span => "Defined here",
|
||||||
|
note = error_output,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +26,9 @@ pub fn initial_value(var: &ScriptVarDefinition) -> Result<DynVal> {
|
||||||
VarSource::Function(f) => {
|
VarSource::Function(f) => {
|
||||||
f().map_err(|err| anyhow!(err)).with_context(|| format!("Failed to compute initial value for {}", &var.name()))
|
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())),
|
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);
|
log::debug!("Running command: {}", cmd);
|
||||||
let command = Command::new("/bin/sh").arg("-c").arg(cmd).output()?;
|
let command = Command::new("/bin/sh").arg("-c").arg(cmd).output()?;
|
||||||
if !command.status.success() {
|
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 = String::from_utf8(command.stdout)?;
|
||||||
let output = output.trim_matches('\n');
|
let output = output.trim_matches('\n');
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
use std::collections::HashMap;
|
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 anyhow::*;
|
||||||
use app::DaemonCommand;
|
use app::DaemonCommand;
|
||||||
|
|
||||||
|
@ -169,7 +172,7 @@ impl PollVarHandler {
|
||||||
fn run_poll_once(var: &PollScriptVar) -> Result<DynVal> {
|
fn run_poll_once(var: &PollScriptVar) -> Result<DynVal> {
|
||||||
match &var.command {
|
match &var.command {
|
||||||
VarSource::Shell(span, x) => {
|
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)),
|
VarSource::Function(x) => x().map_err(|e| anyhow!(e)),
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,14 @@ fn span_to_secondary_label(span: Span) -> Label<usize> {
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! gen_diagnostic {
|
macro_rules! gen_diagnostic {
|
||||||
(
|
( $(kind = $kind:expr,)?
|
||||||
$(msg = $msg:expr)?
|
$(msg = $msg:expr)?
|
||||||
$(, label = $span:expr $(=> $label:expr)?)?
|
$(, label = $span:expr $(=> $label:expr)?)?
|
||||||
$(, note = $note: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_message($msg.to_string()))?
|
||||||
$(.with_labels(vec![
|
$(.with_labels(vec![
|
||||||
::codespan_reporting::diagnostic::Label::primary($span.2, $span.0..$span.1)
|
::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_message($msg.to_string())
|
||||||
$(.with_labels(vec![::codespan_reporting::diagnostic::Label::primary($span.2, $span.0..$span.1)]))?
|
$(.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 {
|
pub trait DiagnosticExt: Sized {
|
||||||
|
|
Loading…
Add table
Reference in a new issue