Compare commits

...

3 Commits
v0.8.0 ... main

@ -1,6 +1,6 @@
[package]
name = "embed-nu"
version = "0.8.0"
version = "0.9.1"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/Trivernis/embed-nu"
@ -10,12 +10,12 @@ authors = ["trivernis <trivernis@proton.me>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
miette = "5.9.0"
nu-cmd-lang = "0.80.0"
nu-command = "0.80.0"
nu-engine = "0.80.0"
nu-parser = "0.80.0"
nu-protocol = "0.80.0"
paste = "1.0.12"
miette = "7.1.0"
nu-cmd-lang = "0.90.1"
nu-command = "0.90.1"
nu-engine = "0.90.1"
nu-parser = "0.90.1"
nu-protocol = "0.90.1"
paste = "1.0.14"
rusty-value = { version = "0.6.0", features = ["derive"] }
thiserror = "1.0.40"
thiserror = "1.0.57"

@ -0,0 +1,79 @@
/// Copy of the nushell print command with a slight adjustment for pipelines
/// Source: https://github.com/nushell/nushell/blob/98525043edd20abb62da09726d75816d09d68f1e/crates/nu-cli/src/print.rs
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::NewEmpty;
#[derive(Clone)]
pub struct PrintCommand;
impl Command for PrintCommand {
fn name(&self) -> &str {
"print"
}
fn signature(&self) -> Signature {
Signature::build("print")
.input_output_types(vec![
(Type::Nothing, Type::Nothing),
(Type::Any, Type::Nothing),
])
.allow_variants_without_examples(true)
.rest("rest", SyntaxShape::Any, "the values to print")
.switch(
"no-newline",
"print without inserting a newline for the line ending",
Some('n'),
)
.switch("stderr", "print to stderr instead of stdout", Some('e'))
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Print the given values to stdout."
}
fn extra_usage(&self) -> &str {
r#"Unlike `echo`, this command does not return any value (`print | describe` will return "nothing").
Since this command has no output, there is no point in piping it with other commands.
`print` may be used inside blocks of code (e.g.: hooks) to display text during execution without interfering with the pipeline.
When used inside a pipeline it passes the input forward as output without interfering with it.
"#
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
let to_stderr = call.has_flag(engine_state, stack, "stderr")?;
let input_val = input.into_value(Span::empty());
// This will allow for easy printing of pipelines as well
if !args.is_empty() {
for arg in args {
arg.into_pipeline_data()
.print(engine_state, stack, no_newline, to_stderr)?;
}
} else if !input_val.is_nothing() {
input_val.clone().into_pipeline_data().print(
engine_state,
stack,
no_newline,
to_stderr,
)?;
}
Ok(input_val.into_pipeline_data())
}
}

@ -2,7 +2,7 @@ use nu_cmd_lang::*;
use nu_command::*;
use nu_protocol::engine::{EngineState, StateWorkingSet};
use crate::error::CrateResult;
use crate::{commands::PrintCommand, error::CrateResult};
macro_rules! bind_commands {
($engine_state:expr, $( $command:expr),* $(,)? ) => {
@ -22,7 +22,6 @@ pub fn bind_core_commands(engine_state: &mut EngineState) -> CrateResult<()> {
Const,
Continue,
Def,
DefEnv,
Describe,
Do,
Echo,
@ -30,7 +29,6 @@ pub fn bind_core_commands(engine_state: &mut EngineState) -> CrateResult<()> {
ExportAlias,
ExportCommand,
ExportDef,
ExportDefEnv,
ExportExtern,
ExportUse,
Extern,
@ -44,6 +42,7 @@ pub fn bind_core_commands(engine_state: &mut EngineState) -> CrateResult<()> {
Hide,
HideEnv,
If,
PrintCommand,
Ignore,
Overlay,
OverlayUse,
@ -68,10 +67,10 @@ pub fn bind_debug_commands(engine_state: &mut EngineState) -> CrateResult<()> {
engine_state,
Ast,
Debug,
DebugInfo,
Explain,
Inspect,
Metadata,
Profile,
TimeIt,
View,
ViewFiles,
@ -97,7 +96,6 @@ pub fn bind_filter_commands(engine_state: &mut EngineState) -> CrateResult<()> {
DropColumn,
DropNth,
Each,
EachWhile,
Empty,
Enumerate,
Every,
@ -110,6 +108,7 @@ pub fn bind_filter_commands(engine_state: &mut EngineState) -> CrateResult<()> {
GroupBy,
Headers,
Insert,
Items,
Join,
SplitBy,
Take,
@ -127,12 +126,6 @@ pub fn bind_filter_commands(engine_state: &mut EngineState) -> CrateResult<()> {
Reject,
Rename,
Reverse,
Roll,
RollDown,
RollUp,
RollLeft,
RollRight,
Rotate,
Select,
Shuffle,
Skip,
@ -146,7 +139,6 @@ pub fn bind_filter_commands(engine_state: &mut EngineState) -> CrateResult<()> {
UniqBy,
Upsert,
Update,
UpdateCells,
Values,
Where,
Window,
@ -224,20 +216,15 @@ pub fn bind_string_commands(engine_state: &mut EngineState) -> CrateResult<()> {
Encode,
DecodeBase64,
EncodeBase64,
DecodeHex,
EncodeHex,
DetectColumns,
Format,
FileSize,
Parse,
Size,
Split,
SplitChars,
SplitColumn,
SplitRow,
SplitWords,
Str,
StrCamelCase,
StrCapitalize,
StrContains,
StrDistance,
@ -246,35 +233,15 @@ pub fn bind_string_commands(engine_state: &mut EngineState) -> CrateResult<()> {
StrJoin,
StrReplace,
StrIndexOf,
StrKebabCase,
StrLength,
StrPascalCase,
StrReverse,
StrScreamingSnakeCase,
StrSnakeCase,
StrStartsWith,
StrSubstring,
StrTrim,
StrTitleCase,
StrUpcase
}
}
pub fn bind_bit_commands(engine_state: &mut EngineState) -> CrateResult<()> {
bind_commands! {
engine_state,
Bits,
BitsAnd,
BitsNot,
BitsOr,
BitsXor,
BitsRotateLeft,
BitsRotateRight,
BitsShiftLeft,
BitsShiftRight,
}
}
pub fn bind_byte_commands(engine_state: &mut EngineState) -> CrateResult<()> {
bind_commands! {
engine_state,
@ -297,10 +264,10 @@ pub fn bind_file_system_commands(engine_state: &mut EngineState) -> CrateResult<
bind_commands! {
engine_state,
Cd,
Cp,
UCp,
Ls,
Mkdir,
Mv,
UMv,
Open,
Rm,
Save,
@ -314,8 +281,6 @@ pub fn bind_platform_commands(engine_state: &mut EngineState) -> CrateResult<()>
bind_commands! {
engine_state,
Ansi,
AnsiGradient,
AnsiStrip,
Clear,
Du,
Input,
@ -357,14 +322,12 @@ pub fn bind_format_commands(engine_state: &mut EngineState) -> CrateResult<()> {
FromSsv,
FromToml,
FromTsv,
FromUrl,
FromXlsx,
FromXml,
FromYaml,
FromYml,
To,
ToCsv,
ToHtml,
ToJson,
ToMd,
ToNuon,
@ -384,7 +347,6 @@ pub fn bind_viewer_commands(engine_state: &mut EngineState) -> CrateResult<()> {
engine_state,
Griddle,
Table,
Explore,
}
}
@ -392,12 +354,10 @@ pub fn bind_conversion_commands(engine_state: &mut EngineState) -> CrateResult<(
bind_commands! {
engine_state,
Fill,
Fmt,
Into,
IntoBool,
IntoBinary,
IntoDatetime,
IntoDecimal,
IntoDuration,
IntoFilesize,
IntoInt,
@ -437,23 +397,6 @@ pub fn bind_math_commands(engine_state: &mut EngineState) -> CrateResult<()> {
MathStddev,
MathSum,
MathVariance,
MathSin,
MathCos,
MathTan,
MathSinH,
MathCosH,
MathTanH,
MathArcSin,
MathArcCos,
MathArcTan,
MathArcSinH,
MathArcCosH,
MathArcTanH,
MathPi,
MathTau,
MathEuler,
MathExp,
MathLn,
MathLog,
}
}
@ -483,9 +426,7 @@ pub fn bind_random_commands(engine_state: &mut EngineState) -> CrateResult<()> {
Random,
RandomBool,
RandomChars,
RandomDecimal,
RandomDice,
RandomInteger,
RandomUuid,
}
}

@ -54,7 +54,6 @@ impl ContextBuilder {
path,
system,
string,
bit,
byte,
file_system,
platform,

@ -12,7 +12,7 @@ pub enum CrateError {
#[error("Parse Error {0:?}")]
#[diagnostic()]
NuParseErrors(#[related] Vec<ParseError>),
NuParseErrors(Vec<ParseError>),
#[error("Could not find the function {0}")]
#[diagnostic()]

@ -1,5 +1,5 @@
use nu_protocol::{
ast::{Expr, Expression},
ast::{Expr, Expression, RecordItem},
Span, Value,
};
@ -42,16 +42,13 @@ impl ValueIntoExpression for Value {
Value::Duration { val, .. } => Expr::Int(val),
Value::Date { val, .. } => Expr::DateTime(val),
Value::String { val, .. } => Expr::String(val),
Value::Record {
mut cols, mut vals, ..
} => {
let mut entries = Vec::new();
for _ in 0..cols.len() {
let col = cols.remove(0).into_expression();
let val = vals.remove(0).into_expression();
entries.push((col, val));
}
Value::Record { val, .. } => {
let entries = val
.into_iter()
.map(|(col, val)| {
RecordItem::Pair(col.into_expression(), val.into_expression())
})
.collect();
Expr::Record(entries)
}
@ -61,7 +58,7 @@ impl ValueIntoExpression for Value {
}
Value::Block { val, .. } => Expr::Block(val),
Value::Nothing { .. } => Expr::Nothing,
Value::Error { error } => Expr::String(error.to_string()),
Value::Error { error, .. } => Expr::String(error.to_string()),
Value::Binary { val, .. } => Expr::Binary(val),
Value::CellPath { val, .. } => Expr::CellPath(val),
_ => Expr::Nothing,

@ -1,4 +1,4 @@
use nu_protocol::{Span, Value};
use nu_protocol::{Record, Span, Value};
use rusty_value::{Fields, HashableValue, RustyValue};
use crate::utils::NewEmpty;
@ -48,7 +48,7 @@ impl RustyIntoValue for Vec<Value> {
fn into_value(self) -> Value {
Value::List {
vals: self,
span: Span::empty(),
internal_span: Span::empty(),
}
}
}
@ -61,7 +61,7 @@ impl RustyIntoValue for rusty_value::Value {
if let Fields::Unit = &s.fields {
Value::String {
val: s.name,
span: Span::empty(),
internal_span: Span::empty(),
}
} else {
s.fields.into_value()
@ -71,7 +71,7 @@ impl RustyIntoValue for rusty_value::Value {
if let Fields::Unit = &e.fields {
Value::String {
val: e.variant,
span: Span::empty(),
internal_span: Span::empty(),
}
} else {
e.fields.into_value()
@ -86,9 +86,8 @@ impl RustyIntoValue for rusty_value::Value {
vals.push(val.into_value());
}
Value::Record {
cols,
vals,
span: Span::empty(),
val: Record::from_raw_cols_vals_unchecked(cols, vals),
internal_span: Span::empty(),
}
}
rusty_value::Value::List(l) => {
@ -96,11 +95,11 @@ impl RustyIntoValue for rusty_value::Value {
Value::List {
vals,
span: Span::empty(),
internal_span: Span::empty(),
}
}
rusty_value::Value::None => Value::Nothing {
span: Span::empty(),
internal_span: Span::empty(),
},
}
}
@ -113,15 +112,15 @@ impl RustyIntoValue for rusty_value::Primitive {
rusty_value::Primitive::Float(f) => f.into_value(),
rusty_value::Primitive::String(val) => Value::String {
val,
span: Span::empty(),
internal_span: Span::empty(),
},
rusty_value::Primitive::Char(val) => Value::String {
val: val.to_string(),
span: Span::empty(),
internal_span: Span::empty(),
},
rusty_value::Primitive::Bool(val) => Value::Bool {
val,
span: Span::empty(),
internal_span: Span::empty(),
},
rusty_value::Primitive::OsString(osstr) => osstr.to_string_lossy().into_value(),
}
@ -140,9 +139,8 @@ impl RustyIntoValue for rusty_value::Fields {
vals.push(v.into_value());
}
Value::Record {
cols,
vals,
span: Span::empty(),
val: Record::from_raw_cols_vals_unchecked(cols, vals),
internal_span: Span::empty(),
}
}
rusty_value::Fields::Unnamed(unnamed) => {
@ -158,12 +156,12 @@ impl RustyIntoValue for rusty_value::Fields {
} else {
Value::List {
vals,
span: Span::empty(),
internal_span: Span::empty(),
}
}
}
rusty_value::Fields::Unit => Value::Nothing {
span: Span::empty(),
internal_span: Span::empty(),
},
}
}
@ -187,7 +185,7 @@ impl RustyIntoValue for rusty_value::Integer {
};
Value::Int {
val,
span: Span::empty(),
internal_span: Span::empty(),
}
}
}
@ -201,7 +199,7 @@ impl RustyIntoValue for rusty_value::Float {
};
Value::Float {
val,
span: Span::empty(),
internal_span: Span::empty(),
}
}
}

@ -1,5 +1,6 @@
#![doc=include_str!("../README.md")]
pub(crate) mod argument;
pub mod commands;
pub(crate) mod context;
pub(crate) mod error;
pub(crate) mod into_expression;

@ -15,10 +15,20 @@ fn it_evals_strings() {
ctx.print_pipeline(pipeline).unwrap()
}
#[test]
fn it_evals_print() {
let mut ctx = get_context();
ctx.eval_raw(
r#"print "Hello World from this eval using print""#,
PipelineData::empty(),
)
.unwrap();
}
#[test]
fn it_reports_parse_errors() {
let mut ctx = get_context();
let eval_result = ctx.eval_raw(r#"let a = 1 | 2 | 3"#, PipelineData::empty());
let eval_result = ctx.eval_raw(r#"let a = 1 || 2"#, PipelineData::empty());
assert!(eval_result.is_err());
}

Loading…
Cancel
Save