-
Notifications
You must be signed in to change notification settings - Fork 54
Add eprint function #611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add eprint function #611
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44c10b1
Basic impl
hulto 456fdbe
Move eprint to native function.
hulto 8afecac
Update test.
hulto e3d2934
Merge branch 'main' into 610-allow-users-to-emit-errors
hulto a60e256
Merge branch 'main' into 610-allow-users-to-emit-errors
hulto 4b2a5d9
Merge branch 'main' into 610-allow-users-to-emit-errors
hulto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,65 @@ | ||
| use crate::runtime::{messages::ReportErrorMessage, Environment}; | ||
| use anyhow::Result; | ||
|
|
||
| pub fn eprint(env: &Environment, message: String) -> Result<()> { | ||
| env.send(ReportErrorMessage { | ||
| id: env.id(), | ||
| error: message.clone(), | ||
| })?; | ||
|
|
||
| #[cfg(feature = "print_stdout")] | ||
| eprintln!("{}", message); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use crate::runtime::Message; | ||
| use pb::eldritch::Tome; | ||
| use std::collections::HashMap; | ||
|
|
||
| macro_rules! test_cases { | ||
| ($($name:ident: $value:expr,)*) => { | ||
| $( | ||
| #[tokio::test] | ||
| async fn $name() { | ||
| let tc: TestCase = $value; | ||
|
|
||
| // Run Eldritch (until finished) | ||
| let mut runtime = crate::start(tc.id, tc.tome).await; | ||
| runtime.finish().await; | ||
|
|
||
| // Read Messages | ||
| let mut found = false; | ||
| for msg in runtime.messages() { | ||
| if let Message::ReportError(m) = msg { | ||
| assert_eq!(tc.id, m.id); | ||
| assert_eq!(tc.want_error, m.error); | ||
| found = true; | ||
| } | ||
| } | ||
| assert!(found); | ||
| } | ||
| )* | ||
| } | ||
| } | ||
|
|
||
| struct TestCase { | ||
| pub id: i64, | ||
| pub tome: Tome, | ||
| pub want_error: String, | ||
| } | ||
|
|
||
| test_cases! { | ||
| one_error: TestCase{ | ||
| id: 123, | ||
| tome: Tome{ | ||
| eldritch: String::from(r#"eprint(message="Beep Boop an error occured")"#), | ||
| parameters: HashMap::new(), | ||
| file_names: Vec::new(), | ||
| }, | ||
| want_error: String::from("Beep Boop an error occured"), | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ use crate::{ | |
| regex::RegexLibrary, | ||
| report::ReportLibrary, | ||
| runtime::{ | ||
| eprint_impl, | ||
| messages::{reduce, Message, ReportErrorMessage, ReportFinishMessage, ReportStartMessage}, | ||
| Environment, | ||
| }, | ||
|
|
@@ -24,8 +25,7 @@ use starlark::{ | |
| eval::Evaluator, | ||
| starlark_module, | ||
| syntax::{AstModule, Dialect}, | ||
| values::dict::Dict, | ||
| values::AllocValue, | ||
| values::{dict::Dict, none::NoneType, AllocValue}, | ||
| }; | ||
| use std::sync::mpsc::{channel, Receiver}; | ||
| use tokio::task::JoinHandle; | ||
|
|
@@ -142,6 +142,16 @@ pub struct Runtime { | |
| rx: Receiver<Message>, | ||
| } | ||
|
|
||
| #[starlark_module] | ||
| fn error_handler(builder: &mut GlobalsBuilder) { | ||
| #[allow(unused_variables)] | ||
| fn eprint(starlark_eval: &mut Evaluator<'v, '_>, message: String) -> anyhow::Result<NoneType> { | ||
| let env = crate::runtime::Environment::from_extra(starlark_eval.extra)?; | ||
| eprint_impl::eprint(env, message)?; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love that we pass env here, greatly demonstrates the separation between eprint (starlark) and eprint_impl (native rust) |
||
| Ok(NoneType {}) | ||
| } | ||
| } | ||
|
|
||
| impl Runtime { | ||
| /* | ||
| * Globals available to eldritch code. | ||
|
|
@@ -177,6 +187,7 @@ impl Runtime { | |
| LibraryExtension::Typing, | ||
| ]) | ||
| .with(eldritch) | ||
| .with(error_handler) | ||
| .build() | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| mod drain; | ||
| mod environment; | ||
| mod eprint_impl; | ||
| mod eval; | ||
| pub mod messages; | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.