diff --git a/crates/nu-command/src/filesystem/mod.rs b/crates/nu-command/src/filesystem/mod.rs index 1bdcc206f1..ab32215db0 100644 --- a/crates/nu-command/src/filesystem/mod.rs +++ b/crates/nu-command/src/filesystem/mod.rs @@ -25,5 +25,4 @@ pub use rm::Rm; pub use save::Save; pub use start::Start; pub use touch::Touch; -pub use util::BufferedReader; pub use watch::Watch; diff --git a/crates/nu-command/src/filesystem/open.rs b/crates/nu-command/src/filesystem/open.rs index 74a49a168f..b94b4a25ae 100644 --- a/crates/nu-command/src/filesystem/open.rs +++ b/crates/nu-command/src/filesystem/open.rs @@ -1,7 +1,7 @@ -use crate::filesystem::util::BufferedReader; use nu_engine::{eval_block, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::util::BufferedReader; use nu_protocol::{ Category, Example, PipelineData, RawStream, ShellError, Signature, Spanned, SyntaxShape, Type, Value, diff --git a/crates/nu-command/src/filesystem/util.rs b/crates/nu-command/src/filesystem/util.rs index 1ef1523b24..eaba9d46a8 100644 --- a/crates/nu-command/src/filesystem/util.rs +++ b/crates/nu-command/src/filesystem/util.rs @@ -7,7 +7,6 @@ use nu_protocol::ShellError; use dialoguer::Input; use std::error::Error; -use std::io::{BufRead, BufReader, Read}; #[derive(Default)] pub struct FileStructure { @@ -133,37 +132,3 @@ fn get_interactive_confirmation(prompt: String) -> Result> Ok(false) } } - -pub struct BufferedReader { - pub input: BufReader, -} - -impl BufferedReader { - pub fn new(input: BufReader) -> Self { - Self { input } - } -} - -impl Iterator for BufferedReader { - type Item = Result, ShellError>; - - fn next(&mut self) -> Option { - let buffer = self.input.fill_buf(); - match buffer { - Ok(s) => { - let result = s.to_vec(); - - let buffer_len = s.len(); - - if buffer_len == 0 { - None - } else { - self.input.consume(buffer_len); - - Some(Ok(result)) - } - } - Err(e) => Some(Err(ShellError::IOError(e.to_string()))), - } - } -} diff --git a/crates/nu-command/src/network/fetch.rs b/crates/nu-command/src/network/fetch.rs index 3b5c989f15..969f4414a2 100644 --- a/crates/nu-command/src/network/fetch.rs +++ b/crates/nu-command/src/network/fetch.rs @@ -1,9 +1,8 @@ -use crate::BufferedReader; - use base64::encode; use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::util::BufferedReader; use nu_protocol::RawStream; use nu_protocol::{ diff --git a/crates/nu-command/src/network/post.rs b/crates/nu-command/src/network/post.rs index 441e342211..5e70eb4e02 100644 --- a/crates/nu-command/src/network/post.rs +++ b/crates/nu-command/src/network/post.rs @@ -1,9 +1,9 @@ use crate::formats::value_to_json_value; -use crate::BufferedReader; use base64::encode; use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::util::BufferedReader; use nu_protocol::RawStream; use reqwest::{blocking::Response, StatusCode}; use std::path::PathBuf; diff --git a/crates/nu-protocol/src/lib.rs b/crates/nu-protocol/src/lib.rs index 6739adf7f4..b4490575c2 100644 --- a/crates/nu-protocol/src/lib.rs +++ b/crates/nu-protocol/src/lib.rs @@ -13,6 +13,7 @@ mod signature; pub mod span; mod syntax_shape; mod ty; +pub mod util; mod value; mod variable; @@ -29,5 +30,6 @@ pub use signature::*; pub use span::*; pub use syntax_shape::*; pub use ty::*; +pub use util::BufferedReader; pub use value::*; pub use variable::*; diff --git a/crates/nu-protocol/src/util.rs b/crates/nu-protocol/src/util.rs new file mode 100644 index 0000000000..26383d1cef --- /dev/null +++ b/crates/nu-protocol/src/util.rs @@ -0,0 +1,36 @@ +use crate::ShellError; +use std::io::{BufRead, BufReader, Read}; + +pub struct BufferedReader { + pub input: BufReader, +} + +impl BufferedReader { + pub fn new(input: BufReader) -> Self { + Self { input } + } +} + +impl Iterator for BufferedReader { + type Item = Result, ShellError>; + + fn next(&mut self) -> Option { + let buffer = self.input.fill_buf(); + match buffer { + Ok(s) => { + let result = s.to_vec(); + + let buffer_len = s.len(); + + if buffer_len == 0 { + None + } else { + self.input.consume(buffer_len); + + Some(Ok(result)) + } + } + Err(e) => Some(Err(ShellError::IOError(e.to_string()))), + } + } +} diff --git a/src/main.rs b/src/main.rs index 4c0bd57cc4..7ad0fdf74f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,13 +15,14 @@ use nu_cli::{ evaluate_commands, evaluate_file, evaluate_repl, gather_parent_env_vars, get_init_cwd, report_error, report_error_new, }; -use nu_command::{create_default_context, BufferedReader}; +use nu_command::create_default_context; use nu_engine::{get_full_help, CallExt}; use nu_parser::{escape_for_script_arg, escape_quote_string, parse}; use nu_path::canonicalize_with; use nu_protocol::{ ast::{Call, Expr, Expression, PipelineElement}, engine::{Command, EngineState, Stack, StateWorkingSet}, + util::BufferedReader, Category, Example, IntoPipelineData, PipelineData, RawStream, ShellError, Signature, Spanned, SyntaxShape, Value, };