forked from extern/nushell
Add general refactorings (#3996)
This commit is contained in:
@ -27,7 +27,7 @@ fn from_delimited_string_to_value(
|
||||
let mut rows = vec![];
|
||||
for row in reader.records() {
|
||||
let mut tagged_row = TaggedDictBuilder::new(&tag);
|
||||
for (value, header) in row?.iter().zip(headers.iter()) {
|
||||
for (value, header) in row?.iter().zip(&headers) {
|
||||
if let Ok(i) = value.parse::<i64>() {
|
||||
tagged_row.insert_value(header, UntaggedValue::int(i).into_value(&tag))
|
||||
} else if let Ok(f) = value.parse::<f64>() {
|
||||
|
@ -100,7 +100,7 @@ fn from_eml(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
dict.insert_untagged("To", headerfieldvalue_to_value(&tag, &to));
|
||||
}
|
||||
|
||||
for HeaderField { name, value } in eml.headers.iter() {
|
||||
for HeaderField { name, value } in &eml.headers {
|
||||
dict.insert_untagged(name, headerfieldvalue_to_value(&tag, value));
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ fn convert_yaml_value_to_nu_value(
|
||||
serde_yaml::Value::Mapping(t) => {
|
||||
let mut collected = TaggedDictBuilder::new(&tag);
|
||||
|
||||
for (k, v) in t.iter() {
|
||||
for (k, v) in t {
|
||||
// A ShellError that we re-use multiple times in the Mapping scenario
|
||||
let err_unexpected_map = ShellError::labeled_error(
|
||||
format!("Unexpected YAML:\nKey: {:?}\nValue: {:?}", k, v),
|
||||
@ -202,7 +202,7 @@ mod tests {
|
||||
expected: Ok(row!["value".to_owned() => string("{{ something }}")]),
|
||||
},
|
||||
];
|
||||
for tc in tt.into_iter() {
|
||||
for tc in tt {
|
||||
let actual = from_yaml_string_to_value(tc.input.to_owned(), Tag::default());
|
||||
if actual.is_err() {
|
||||
assert!(
|
||||
|
@ -20,7 +20,7 @@ fn from_value_to_delimited_string(
|
||||
let mut fields: VecDeque<String> = VecDeque::new();
|
||||
let mut values: VecDeque<String> = VecDeque::new();
|
||||
|
||||
for (k, v) in o.entries.iter() {
|
||||
for (k, v) in &o.entries {
|
||||
fields.push_back(k.clone());
|
||||
|
||||
values.push_back(to_string_tagged_value(v)?);
|
||||
@ -130,12 +130,14 @@ pub fn clone_tagged_value(v: &Value) -> Value {
|
||||
// NOTE: could this be useful more widely and implemented on Value ?
|
||||
fn to_string_tagged_value(v: &Value) -> Result<String, ShellError> {
|
||||
match &v.value {
|
||||
UntaggedValue::Primitive(Primitive::String(_))
|
||||
| UntaggedValue::Primitive(Primitive::Filesize(_))
|
||||
| UntaggedValue::Primitive(Primitive::Boolean(_))
|
||||
| UntaggedValue::Primitive(Primitive::Decimal(_))
|
||||
| UntaggedValue::Primitive(Primitive::FilePath(_))
|
||||
| UntaggedValue::Primitive(Primitive::Int(_)) => as_string(v),
|
||||
UntaggedValue::Primitive(
|
||||
Primitive::String(_)
|
||||
| Primitive::Filesize(_)
|
||||
| Primitive::Boolean(_)
|
||||
| Primitive::Decimal(_)
|
||||
| Primitive::FilePath(_)
|
||||
| Primitive::Int(_),
|
||||
) => as_string(v),
|
||||
UntaggedValue::Primitive(Primitive::Date(d)) => Ok(d.to_string()),
|
||||
UntaggedValue::Primitive(Primitive::Nothing) => Ok(String::new()),
|
||||
UntaggedValue::Table(_) => Ok(String::from("[Table]")),
|
||||
@ -153,7 +155,7 @@ fn merge_descriptors(values: &[Value]) -> Vec<Spanned<String>> {
|
||||
let mut seen: IndexSet<String> = indexset! {};
|
||||
for value in values {
|
||||
for desc in value.data_descriptors() {
|
||||
if !seen.contains(&desc[..]) {
|
||||
if !seen.contains(&desc) {
|
||||
seen.insert(desc.clone());
|
||||
ret.push(desc.spanned(value.tag.span));
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fmt::Write;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct HtmlThemes {
|
||||
@ -124,8 +125,8 @@ fn get_theme_from_asset_file(
|
||||
theme_tag: &Tag,
|
||||
) -> Result<HashMap<&'static str, String>, ShellError> {
|
||||
let theme_name = match theme {
|
||||
Some(s) => s.to_string(),
|
||||
None => "default".to_string(), // There is no theme named "default" so this will be HtmlTheme::default(), which is "nu_default".
|
||||
Some(s) => s.as_str(),
|
||||
None => "default", // There is no theme named "default" so this will be HtmlTheme::default(), which is "nu_default".
|
||||
};
|
||||
|
||||
// 228 themes come from
|
||||
@ -143,7 +144,7 @@ fn get_theme_from_asset_file(
|
||||
let th = asset
|
||||
.themes
|
||||
.iter()
|
||||
.find(|&n| n.name.to_lowercase() == *theme_name.to_lowercase().as_str()); // case insensitive search
|
||||
.find(|&n| n.name.to_lowercase() == theme_name.to_lowercase()); // case insensitive search
|
||||
|
||||
// If no theme is found by the name provided, ensure we return the default theme
|
||||
let default_theme = HtmlTheme::default();
|
||||
@ -248,11 +249,7 @@ fn get_list_of_theme_names() -> Vec<String> {
|
||||
_ => HtmlThemes::default(),
|
||||
};
|
||||
|
||||
let theme_names: Vec<String> = html_themes
|
||||
.themes
|
||||
.iter()
|
||||
.map(|n| n.name[..].to_string())
|
||||
.collect();
|
||||
let theme_names: Vec<String> = html_themes.themes.iter().map(|n| n.name.clone()).collect();
|
||||
|
||||
theme_names
|
||||
}
|
||||
@ -278,8 +275,8 @@ fn to_html(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let theme_names = get_list_of_theme_names();
|
||||
|
||||
// Put that list into the output string
|
||||
for s in theme_names.iter() {
|
||||
output_string.push_str(&format!("{}\n", s));
|
||||
for s in &theme_names {
|
||||
writeln!(&mut output_string, "{}", s).unwrap();
|
||||
}
|
||||
|
||||
output_string.push_str("\nScreenshots of themes can be found here:\n");
|
||||
@ -304,7 +301,8 @@ fn to_html(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
|
||||
// change the color of the page
|
||||
if !partial {
|
||||
output_string.push_str(&format!(
|
||||
write!(
|
||||
&mut output_string,
|
||||
r"<html><style>body {{ background-color:{};color:{}; }}</style><body>",
|
||||
color_hm
|
||||
.get("background")
|
||||
@ -312,9 +310,11 @@ fn to_html(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
color_hm
|
||||
.get("foreground")
|
||||
.expect("Error getting foreground color")
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
output_string.push_str(&format!(
|
||||
write!(
|
||||
&mut output_string,
|
||||
"<div style=\"background-color:{};color:{};\">",
|
||||
color_hm
|
||||
.get("background")
|
||||
@ -322,7 +322,8 @@ fn to_html(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
color_hm
|
||||
.get("foreground")
|
||||
.expect("Error getting foreground color")
|
||||
));
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let inner_value = match input.len() {
|
||||
|
@ -134,7 +134,7 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
|
||||
),
|
||||
UntaggedValue::Row(o) => {
|
||||
let mut m = serde_json::Map::new();
|
||||
for (k, v) in o.entries.iter() {
|
||||
for (k, v) in &o.entries {
|
||||
m.insert(k.clone(), value_to_json_value(v)?);
|
||||
}
|
||||
serde_json::Value::Object(m)
|
||||
@ -184,9 +184,7 @@ fn to_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
|
||||
if let Ok(pretty_u64) = pretty_value.as_u64() {
|
||||
if let Ok(serde_json_value) =
|
||||
serde_json::from_str::<serde_json::Value>(
|
||||
serde_json_string.as_str(),
|
||||
)
|
||||
serde_json::from_str::<serde_json::Value>(&serde_json_string)
|
||||
{
|
||||
let indentation_string = " ".repeat(pretty_u64 as usize);
|
||||
let serde_formatter =
|
||||
|
@ -236,7 +236,7 @@ fn get_output_string(
|
||||
));
|
||||
output_string.push(' ');
|
||||
} else {
|
||||
output_string.push_str(headers[i].as_str());
|
||||
output_string.push_str(&headers[i]);
|
||||
}
|
||||
|
||||
output_string.push('|');
|
||||
@ -275,7 +275,7 @@ fn get_output_string(
|
||||
output_string.push_str(&get_padded_string(row[i].clone(), column_widths[i], ' '));
|
||||
output_string.push(' ');
|
||||
} else {
|
||||
output_string.push_str(row[i].as_str());
|
||||
output_string.push_str(&row[i]);
|
||||
}
|
||||
|
||||
if !headers.is_empty() {
|
||||
|
@ -83,7 +83,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)
|
||||
@ -97,7 +97,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))
|
||||
|
@ -37,7 +37,7 @@ pub fn add_attributes<'a>(
|
||||
element: &mut quick_xml::events::BytesStart<'a>,
|
||||
attributes: &'a IndexMap<String, String>,
|
||||
) {
|
||||
for (k, v) in attributes.iter() {
|
||||
for (k, v) in attributes {
|
||||
element.push_attribute((k.as_str(), v.as_str()));
|
||||
}
|
||||
}
|
||||
@ -47,7 +47,7 @@ pub fn get_attributes(row: &Value) -> Option<IndexMap<String, String>> {
|
||||
if let Some(v) = r.entries.get("attributes") {
|
||||
if let UntaggedValue::Row(a) = &v.value {
|
||||
let mut h = IndexMap::new();
|
||||
for (k, v) in a.entries.iter() {
|
||||
for (k, v) in &a.entries {
|
||||
h.insert(k.clone(), v.convert_to_string());
|
||||
}
|
||||
return Some(h);
|
||||
@ -84,7 +84,7 @@ pub fn write_xml_events<W: Write>(
|
||||
) -> Result<(), ShellError> {
|
||||
match ¤t.value {
|
||||
UntaggedValue::Row(o) => {
|
||||
for (k, v) in o.entries.iter() {
|
||||
for (k, v) in &o.entries {
|
||||
let mut e = BytesStart::owned(k.as_bytes(), k.len());
|
||||
if !is_xml_row(v) {
|
||||
return Err(ShellError::labeled_error(
|
||||
|
@ -65,7 +65,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => {
|
||||
let mut out = vec![];
|
||||
|
||||
for member in path.iter() {
|
||||
for member in path {
|
||||
match &member.unspanned {
|
||||
UnspannedPathMember::String(string) => {
|
||||
out.push(serde_yaml::Value::String(string.clone()))
|
||||
@ -104,7 +104,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||
),
|
||||
UntaggedValue::Row(o) => {
|
||||
let mut m = serde_yaml::Mapping::new();
|
||||
for (k, v) in o.entries.iter() {
|
||||
for (k, v) in &o.entries {
|
||||
m.insert(
|
||||
serde_yaml::Value::String(k.clone()),
|
||||
value_to_yaml_value(v)?,
|
||||
|
Reference in New Issue
Block a user