mirror of
https://github.com/nushell/nushell.git
synced 2025-06-20 09:58:15 +02:00
Move config to async_stream
This commit is contained in:
parent
05ff102e09
commit
243df63978
@ -4,7 +4,6 @@ use crate::errors::ShellError;
|
|||||||
use crate::parser::hir::SyntaxShape;
|
use crate::parser::hir::SyntaxShape;
|
||||||
use crate::parser::registry::{self};
|
use crate::parser::registry::{self};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use std::iter::FromIterator;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub struct Config;
|
pub struct Config;
|
||||||
@ -64,6 +63,7 @@ pub fn config(
|
|||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let name_span = name.clone();
|
let name_span = name.clone();
|
||||||
|
|
||||||
|
let stream = async_stream! {
|
||||||
let configuration = if let Some(supplied) = load {
|
let configuration = if let Some(supplied) = load {
|
||||||
Some(supplied.item().clone())
|
Some(supplied.item().clone())
|
||||||
} else {
|
} else {
|
||||||
@ -78,62 +78,59 @@ pub fn config(
|
|||||||
.get(&key)
|
.get(&key)
|
||||||
.ok_or_else(|| ShellError::labeled_error("Missing key in config", "key", v.tag()))?;
|
.ok_or_else(|| ShellError::labeled_error("Missing key in config", "key", v.tag()))?;
|
||||||
|
|
||||||
let mut results = VecDeque::new();
|
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
Tagged {
|
Tagged {
|
||||||
item: Value::Table(list),
|
item: Value::Table(list),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
for l in list {
|
for l in list {
|
||||||
results.push_back(ReturnSuccess::value(l.clone()));
|
yield ReturnSuccess::value(l.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
x => results.push_back(ReturnSuccess::value(x.clone())),
|
x => yield ReturnSuccess::value(x.clone()),
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(results.to_output_stream());
|
|
||||||
}
|
}
|
||||||
|
else if let Some((key, value)) = set {
|
||||||
if let Some((key, value)) = set {
|
|
||||||
result.insert(key.to_string(), value.clone());
|
result.insert(key.to_string(), value.clone());
|
||||||
|
|
||||||
config::write(&result, &configuration)?;
|
config::write(&result, &configuration)?;
|
||||||
|
|
||||||
return Ok(stream![Value::Row(result.into()).tagged(value.tag())].from_input_stream());
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(value.tag()));
|
||||||
}
|
}
|
||||||
|
else if let Tagged { item: true, tag } = clear {
|
||||||
if let Tagged { item: true, tag } = clear {
|
|
||||||
result.clear();
|
result.clear();
|
||||||
|
|
||||||
config::write(&result, &configuration)?;
|
config::write(&result, &configuration)?;
|
||||||
|
|
||||||
return Ok(stream![Value::Row(result.into()).tagged(tag)].from_input_stream());
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(tag));
|
||||||
}
|
|
||||||
|
|
||||||
if let Tagged { item: true, tag } = path {
|
return;
|
||||||
|
}
|
||||||
|
else if let Tagged { item: true, tag } = path {
|
||||||
let path = config::default_path_for(&configuration)?;
|
let path = config::default_path_for(&configuration)?;
|
||||||
|
|
||||||
return Ok(stream![Value::Primitive(Primitive::Path(path)).tagged(tag)].from_input_stream());
|
yield ReturnSuccess::value(Value::Primitive(Primitive::Path(path)).tagged(tag));
|
||||||
}
|
}
|
||||||
|
else if let Some(v) = remove {
|
||||||
if let Some(v) = remove {
|
|
||||||
let key = v.to_string();
|
let key = v.to_string();
|
||||||
|
|
||||||
if result.contains_key(&key) {
|
if result.contains_key(&key) {
|
||||||
result.swap_remove(&key);
|
result.swap_remove(&key);
|
||||||
config::write(&result, &configuration)?;
|
config::write(&result, &configuration).unwrap();
|
||||||
} else {
|
} else {
|
||||||
return Err(ShellError::labeled_error(
|
yield Err(ShellError::labeled_error(
|
||||||
"Key does not exist in config",
|
"Key does not exist in config",
|
||||||
"key",
|
"key",
|
||||||
v.tag(),
|
v.tag(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let obj = VecDeque::from_iter(vec![Value::Row(result.into()).tagged(v.tag())]);
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(v.tag()));
|
||||||
return Ok(obj.from_input_stream());
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
yield ReturnSuccess::value(Value::Row(result.into()).tagged(name));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return Ok(vec![Value::Row(result.into()).tagged(name)].into());
|
Ok(stream.to_output_stream())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user