Add general refactorings (#3996)

This commit is contained in:
Marcin Puc
2021-09-10 00:44:22 +02:00
committed by GitHub
parent ae9f4135c0
commit 51c74eebd0
165 changed files with 540 additions and 615 deletions

View File

@ -45,7 +45,7 @@ pub fn select_fields(obj: &Value, fields: &[String], tag: impl Into<Tag>) -> Val
let descs = obj.data_descriptors();
for column_name in fields {
match descs.iter().find(|d| *d == column_name) {
match descs.iter().find(|&d| d == column_name) {
None => out.insert_untagged(column_name, UntaggedValue::nothing()),
Some(desc) => out.insert_value(desc.clone(), obj.get_data(desc).borrow().clone()),
}

View File

@ -110,7 +110,7 @@ impl InlineShape {
pub fn from_dictionary(dictionary: &Dictionary) -> InlineShape {
let mut map = IndexMap::new();
for (key, value) in dictionary.entries.iter() {
for (key, value) in &dictionary.entries {
let column = Column::String(key.clone());
map.insert(column, InlineShape::from_value(value));
}
@ -119,11 +119,7 @@ impl InlineShape {
}
pub fn from_table<'a>(table: impl IntoIterator<Item = &'a Value>) -> InlineShape {
let mut vec = vec![];
for item in table.into_iter() {
vec.push(InlineShape::from_value(item))
}
let vec = table.into_iter().map(InlineShape::from_value).collect();
InlineShape::Table(vec)
}
@ -150,7 +146,7 @@ impl InlineShape {
match value.into() {
UntaggedValue::Primitive(p) => InlineShape::from_primitive(p),
UntaggedValue::Row(row) => InlineShape::from_dictionary(row),
UntaggedValue::Table(table) => InlineShape::from_table(table.iter()),
UntaggedValue::Table(table) => InlineShape::from_table(table),
UntaggedValue::Error(_) => InlineShape::Error,
UntaggedValue::Block(_) => InlineShape::Block,
#[cfg(feature = "dataframe")]
@ -261,10 +257,9 @@ impl InlineShape {
}
}
} else {
let doc = (DbgDocBldr::primitive(format!("{}", bytesize))
+ DbgDocBldr::space()
+ DbgDocBldr::kind("B"))
.group();
let doc =
(DbgDocBldr::primitive(bytesize) + DbgDocBldr::space() + DbgDocBldr::kind("B"))
.group();
(doc.clone(), InlineShape::render_doc(&doc))
}
}
@ -284,8 +279,8 @@ impl PrettyDebug for FormatInlineShape {
match &self.shape {
InlineShape::Nothing => DbgDocBldr::blank(),
InlineShape::Int(int) => DbgDocBldr::primitive(format!("{}", int)),
InlineShape::BigInt(int) => DbgDocBldr::primitive(format!("{}", int)),
InlineShape::Int(int) => DbgDocBldr::primitive(int),
InlineShape::BigInt(int) => DbgDocBldr::primitive(int),
InlineShape::Decimal(decimal) => DbgDocBldr::description(format_primitive(
&Primitive::Decimal(decimal.clone()),
None,

View File

@ -47,7 +47,7 @@ pub fn convert_toml_value_to_nu_value(v: &toml::Value, tag: impl Into<Tag>) -> V
toml::Value::Table(t) => {
let mut collected = TaggedDictBuilder::new(&tag);
for (k, v) in t.iter() {
for (k, v) in t {
collected.insert_value(k.clone(), convert_toml_value_to_nu_value(v, &tag));
}
@ -127,7 +127,7 @@ fn helper(v: &Value) -> Result<toml::Value, ShellError> {
}
UntaggedValue::Row(o) => {
let mut m = toml::map::Map::new();
for (k, v) in o.entries.iter() {
for (k, v) in &o.entries {
m.insert(k.clone(), helper(v)?);
}
toml::Value::Table(m)
@ -141,7 +141,7 @@ pub fn value_to_toml_value(v: &Value) -> Result<toml::Value, ShellError> {
match &v.value {
UntaggedValue::Row(o) => {
let mut m = toml::map::Map::new();
for (k, v) in o.entries.iter() {
for (k, v) in &o.entries {
m.insert(k.clone(), helper(v)?);
}
Ok(toml::Value::Table(m))
@ -185,10 +185,7 @@ pub fn default_path() -> Result<PathBuf, ShellError> {
pub fn default_path_for(file: &Option<PathBuf>) -> Result<PathBuf, ShellError> {
let mut filename = config_path()?;
let file: &Path = file
.as_ref()
.map(AsRef::as_ref)
.unwrap_or_else(|| "config.toml".as_ref());
let file: &Path = file.as_deref().unwrap_or_else(|| "config.toml".as_ref());
filename.push(file);
Ok(filename)

View File

@ -39,6 +39,6 @@ pub fn read_trusted() -> Result<Trusted, ShellError> {
let mut doc = String::new();
file.read_to_string(&mut doc)?;
let allowed = toml::de::from_str(doc.as_str()).unwrap_or_else(|_| Trusted::new());
let allowed = toml::de::from_str(&doc).unwrap_or_else(|_| Trusted::new());
Ok(allowed)
}

View File

@ -17,11 +17,7 @@ pub fn default_history_path() -> PathBuf {
pub fn history_path(config: &NuConfig) -> Option<PathBuf> {
config
.var("history-path")
.map(|custom_path| match custom_path.as_string() {
Ok(path) => Some(PathBuf::from(path)),
Err(_) => None,
})
.flatten()
.and_then(|custom_path| custom_path.as_string().map(PathBuf::from).ok())
}
/// Get history path in config or default

View File

@ -44,7 +44,7 @@ impl DictionaryExt for Dictionary {
let result = self
.entries
.iter()
.find(|(desc_name, _)| *desc_name == name.item)?
.find(|&(desc_name, _)| desc_name == name.item)?
.1;
Some(
@ -58,7 +58,7 @@ impl DictionaryExt for Dictionary {
fn get_mut_data_by_key(&mut self, name: &str) -> Option<&mut Value> {
self.entries
.iter_mut()
.find(|(desc_name, _)| *desc_name == name)
.find(|&(desc_name, _)| desc_name == name)
.map_or_else(|| None, |x| Some(x.1))
}

View File

@ -27,7 +27,7 @@ pub fn group(
let mut out = TaggedDictBuilder::new(&tag);
for (k, v) in groups.iter() {
for (k, v) in &groups {
out.insert_untagged(k, UntaggedValue::table(v));
}

View File

@ -138,7 +138,7 @@ pub fn inner_max(data: &[Value]) -> Result<Value, ShellError> {
.value
.clone();
for value in data.iter() {
for value in data {
if let Ok(greater_than) =
crate::value::compare_values(Operator::GreaterThan, &value.value, &biggest)
{
@ -245,7 +245,7 @@ pub fn evaluate(
if let Some(ref evaluator) = evaluator {
let mut evaluations = vec![];
for set in values.iter() {
for set in values {
evaluations.push(evaluator(idx, set)?);
}

View File

@ -51,7 +51,7 @@ pub fn split(
let mut out = TaggedDictBuilder::new(&tag);
for (k, v) in splits.into_iter() {
for (k, v) in splits {
out.insert_untagged(k, UntaggedValue::row(v));
}