diff --git a/Cargo.toml b/Cargo.toml index 061b43e..4657932 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,10 @@ name = "embed-nu" version = "0.1.0" edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/Trivernis/embed-nu" +description = "Embed the nu engine in your rust application" +authors = ["trivernis "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..32995eb --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +# embed-nu + +*embed-nu* can be used to call [nu](https://github.com/nushell/nushell) scripts and expressions +from within your rust application. This crate provides a wrapper around the nu engine to easily build +the nu execution context, parse scripts and call functions. As this crate includes nu as a dependency +calls to nu don't have the overhead of calling an external application. + +## Example Usage + +```rust +use embed_nu::{rusty_value::RustyValue, CommandGroupConfig, Context, NewEmpty, PipelineData}; + +fn main() { + let mut ctx = Context::builder() + .with_command_groups(CommandGroupConfig::default().all_groups(true)) + .add_parent_env_vars() + .build() + .unwrap(); + + // eval a nu expression + let pipeline = ctx + .eval_raw( + r#"echo "Hello World from this eval""#, + PipelineData::empty(), + ) + .unwrap(); + + // print the pipeline of this expression. In this case + // this pipeline contains the text of the echo expression + // as it's the last expressin + ctx.print_pipeline(pipeline).unwrap(); + + // this eval put's the function definition of hello into scope + ctx.eval_raw( + r#" + def hello [] { + echo "Hello World from this script"; + echo # dummy echo so I don't have to print the output + } + "#, + PipelineData::empty(), + ) + .unwrap(); + + // hello can now be called as a function + ctx.call_fn("hello", [] as [String; 0]).unwrap(); +} +``` + + +## Converting data into nu values + +This crate uses [rusty-value](https://github.com/Trivernis/rusty-value) to convert any rust +data type into nu values. + +```rust +use embed_nu::{rusty_value::RustyValue, IntoValue}; + + +// derive rusty value +#[derive(RustyValue)] +struct MyStruct { + foo: String, + bar: usize, +} + +fn main() { + let instance = MyStruct { + foo: String::from("foo"), + bar: 12 + }; + // convert this struct into a nu value + // this is also done implicitely when passing the value to the nu context + // as function arguments or variables + let value = instance.into_value(); + dbg!(value); +} +``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d953c1e..ab8bf88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![doc=include_str!("../README.md")] pub(crate) mod argument; pub(crate) mod context; pub(crate) mod error; diff --git a/tests/test_eval.rs b/tests/test_eval.rs index b4eef14..e76ae32 100644 --- a/tests/test_eval.rs +++ b/tests/test_eval.rs @@ -1,6 +1,4 @@ -use embed_nu::{CommandGroupConfig, Context, NewEmpty}; -use nu_protocol::PipelineData; -use rusty_value::RustyValue; +use embed_nu::{rusty_value::RustyValue, CommandGroupConfig, Context, NewEmpty, PipelineData}; #[test] fn it_evals_strings() {