-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clippy
- Loading branch information
Showing
19 changed files
with
262 additions
and
952,304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! Global state for the nickel CLI. | ||
|
||
use nickel_lang_core::{ | ||
error::Error as CoreError, error::Reporter, eval::cache::lazy::CBNCache, files::Files, | ||
program::Program, | ||
}; | ||
|
||
use crate::{ | ||
cli::GlobalOptions, | ||
error::{Error, Warning}, | ||
input::{Prepare, PrepareError}, | ||
}; | ||
|
||
pub struct GlobalContext { | ||
pub opts: GlobalOptions, | ||
pub errors: Vec<Error>, | ||
pub warnings: Vec<Warning>, | ||
} | ||
|
||
impl GlobalContext { | ||
pub fn new(opts: GlobalOptions) -> Self { | ||
GlobalContext { | ||
opts, | ||
errors: Vec::new(), | ||
warnings: Vec::new(), | ||
} | ||
} | ||
|
||
// This is just an alias for the trait method, but having an inherent impl | ||
// makes type inference better because rust defaults to the inherit impl and | ||
// doesn't get confused between `Warning` and `Error`. | ||
pub fn report_result<T, E: Into<Error>>(&mut self, result: Result<T, E>) { | ||
<Self as Reporter<Error>>::report_result(self, result) | ||
} | ||
|
||
pub fn with_program< | ||
T, | ||
P: Prepare, | ||
F: FnOnce(&mut Program<CBNCache>) -> Result<T, CoreError>, | ||
>( | ||
&mut self, | ||
preparer: &P, | ||
f: F, | ||
) -> Option<T> { | ||
let result = match preparer.prepare(self) { | ||
Ok(mut prog) => f(&mut prog).map_err(|error| { | ||
Error::Program { | ||
error, | ||
files: prog.files(), | ||
} | ||
.into() | ||
}), | ||
Err(e) => Err(e), | ||
}; | ||
|
||
match result { | ||
Err(PrepareError::Error(error)) => { | ||
self.report(error); | ||
None | ||
} | ||
Err(PrepareError::EarlyReturn) => None, | ||
Ok(x) => Some(x), | ||
} | ||
} | ||
} | ||
|
||
impl Reporter<Error> for GlobalContext { | ||
fn report(&mut self, e: Error) { | ||
self.errors.push(e); | ||
} | ||
} | ||
|
||
impl Reporter<Warning> for GlobalContext { | ||
fn report(&mut self, w: Warning) { | ||
self.warnings.push(w); | ||
} | ||
} | ||
|
||
impl Reporter<(nickel_lang_core::error::Warning, Files)> for GlobalContext { | ||
fn report(&mut self, (warning, files): (nickel_lang_core::error::Warning, Files)) { | ||
self.warnings.push(Warning::Program { warning, files }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use codespan_reporting::diagnostic::Diagnostic; | ||
|
||
use crate::error::IntoDiagnostics; | ||
use crate::files::{FileId, Files}; | ||
use crate::position::TermPos; | ||
use crate::term::RichTerm; | ||
|
||
use super::{primary, secondary_term}; | ||
|
||
pub enum Warning { | ||
/// Applied a `fun label value => ...` (or match) as a contract directly, | ||
/// instead of using the constract constructors in the standard library. | ||
NakedFunctionContract { | ||
/// The position of the function that was used as a contract. | ||
func: RichTerm, | ||
/// The position of the thing that the contract was applied to. | ||
app_pos: TermPos, | ||
}, | ||
} | ||
|
||
impl IntoDiagnostics for Warning { | ||
fn into_diagnostics(self, files: &mut Files) -> Vec<Diagnostic<FileId>> { | ||
match self { | ||
Warning::NakedFunctionContract { func, app_pos } => { | ||
let mut labels = vec![]; | ||
if let Some(span) = app_pos.into_opt() { | ||
labels.push(primary(&span).with_message("applied to this term")); | ||
} | ||
|
||
labels.push(secondary_term(&func, files).with_message("this function")); | ||
|
||
vec![Diagnostic::warning() | ||
.with_message("plain functions as contracts are deprecated") | ||
.with_labels(labels) | ||
.with_notes(vec!["use one of the constructors in `std.contract` instead, like `std.contract.custom`".to_owned()]) | ||
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.