Add Diagnostic.try_amend() to simplify error handling (#3701)

This commit is contained in:
Jonathan Plasse 2023-03-24 22:10:11 +01:00 committed by GitHub
parent 1bac206995
commit dc4d7619ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 192 additions and 300 deletions

View file

@ -1,3 +1,5 @@
use anyhow::Result;
use log::error;
use rustpython_parser::ast::Location;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
@ -45,6 +47,14 @@ impl Diagnostic {
self
}
pub fn try_amend(&mut self, func: impl FnOnce() -> Result<Fix>) -> &mut Self {
match func() {
Ok(fix) => self.fix = Some(fix),
Err(err) => error!("Failed to create fix: {}", err),
}
self
}
pub fn parent(&mut self, parent: Location) -> &mut Self {
self.parent = Some(parent);
self