cleanup nu-command, remove redundant code (#5208)

This commit is contained in:
Michael Angerman 2022-04-15 23:16:46 -07:00 committed by GitHub
parent cb3276fb3b
commit 1bad40726d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 93 deletions

View file

@ -16,7 +16,8 @@ pub use glob::Glob;
pub use ls::Ls;
pub use mkdir::Mkdir;
pub use mv::Mv;
pub use open::{BufferedReader, Open};
pub use open::Open;
pub use rm::Rm;
pub use save::Save;
pub use touch::Touch;
pub use util::BufferedReader;

View file

@ -1,3 +1,4 @@
use crate::filesystem::util::BufferedReader;
use nu_engine::{eval_block, get_full_help, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
@ -7,7 +8,7 @@ use nu_protocol::{
};
use rusqlite::types::ValueRef;
use rusqlite::{Connection, Row};
use std::io::{BufRead, BufReader, Read, Seek};
use std::io::{BufReader, Read, Seek};
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
@ -296,40 +297,6 @@ fn permission_denied(dir: impl AsRef<Path>) -> bool {
}
}
pub struct BufferedReader<R: Read> {
input: BufReader<R>,
}
impl<R: Read> BufferedReader<R> {
pub fn new(input: BufReader<R>) -> Self {
Self { input }
}
}
impl<R: Read> Iterator for BufferedReader<R> {
type Item = Result<Vec<u8>, ShellError>;
fn next(&mut self) -> Option<Self::Item> {
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()))),
}
}
}
#[cfg(test)]
mod test {
use super::*;

View file

@ -7,6 +7,7 @@ use nu_protocol::ShellError;
use dialoguer::Input;
use std::error::Error;
use std::io::{BufRead, BufReader, Read};
#[derive(Default)]
pub struct FileStructure {
@ -120,3 +121,37 @@ pub fn get_interactive_confirmation(prompt: String) -> Result<bool, Box<dyn Erro
Ok(false)
}
}
pub struct BufferedReader<R: Read> {
pub input: BufReader<R>,
}
impl<R: Read> BufferedReader<R> {
pub fn new(input: BufReader<R>) -> Self {
Self { input }
}
}
impl<R: Read> Iterator for BufferedReader<R> {
type Item = Result<Vec<u8>, ShellError>;
fn next(&mut self) -> Option<Self::Item> {
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()))),
}
}
}

View file

@ -1,3 +1,5 @@
use crate::BufferedReader;
use base64::encode;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
@ -10,7 +12,7 @@ use nu_protocol::{
use reqwest::blocking::Response;
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read};
use std::io::BufReader;
use reqwest::StatusCode;
use std::path::PathBuf;
@ -338,34 +340,6 @@ fn helper(
}
}
pub struct BufferedReader<R: Read> {
input: BufReader<R>,
}
impl<R: Read> Iterator for BufferedReader<R> {
type Item = Result<Vec<u8>, ShellError>;
fn next(&mut self) -> Option<Self::Item> {
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()))),
}
}
}
fn response_to_buffer(
response: Response,
engine_state: &EngineState,

View file

@ -1,4 +1,5 @@
use crate::formats::value_to_json_value;
use crate::BufferedReader;
use base64::encode;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
@ -12,7 +13,7 @@ use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read};
use std::io::BufReader;
#[derive(Clone)]
pub struct SubCommand;
@ -392,33 +393,6 @@ fn helper(
}
}
pub struct BufferedReader<R: Read> {
input: BufReader<R>,
}
impl<R: Read> Iterator for BufferedReader<R> {
type Item = Result<Vec<u8>, ShellError>;
fn next(&mut self) -> Option<Self::Item> {
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()))),
}
}
}
fn response_to_buffer(
response: Response,
engine_state: &EngineState,