Minor code tweaks

This commit is contained in:
Axel Liljencrantz 2022-09-14 00:01:56 +02:00
parent b3f6434330
commit 169b84a534
17 changed files with 57 additions and 53 deletions

View File

@ -374,10 +374,10 @@ mod tests {
Argument::named("list_val", Value::string("a"), Location::new(0, 0)), Argument::named("list_val", Value::string("a"), Location::new(0, 0)),
Argument::named( Argument::named(
"list_val", "list_val",
Value::List(List::new( List::new(
ValueType::String, ValueType::String,
vec![Value::string("b"), Value::string("c")], [Value::string("b"), Value::string("c")],
)), ).into(),
Location::new(0, 0), Location::new(0, 0),
), ),
Argument::named("list_val", Value::string("d"), Location::new(0, 0)), Argument::named("list_val", Value::string("d"), Location::new(0, 0)),

View File

@ -638,7 +638,7 @@ impl Closure {
if let Some(unnamed_name) = unnamed_name { if let Some(unnamed_name) = unnamed_name {
context.env.redeclare( context.env.redeclare(
unnamed_name.string.as_ref(), unnamed_name.string.as_ref(),
Value::List(List::new(ValueType::Any, unnamed)), List::new(ValueType::Any, unnamed).into(),
)?; )?;
} else if !unnamed.is_empty() { } else if !unnamed.is_empty() {
return argument_error_legacy("No target for unnamed arguments"); return argument_error_legacy("No target for unnamed arguments");

View File

@ -42,10 +42,10 @@ macro_rules! dump_to {
} }
impl List { impl List {
pub fn new(cell_type: ValueType, cells: Vec<Value>) -> List { pub fn new(cell_type: ValueType, cells: impl Into<Vec<Value>>) -> List {
List { List {
cell_type, cell_type,
cells: Arc::from(Mutex::new(cells)), cells: Arc::from(Mutex::new(cells.into())),
} }
} }
@ -251,3 +251,9 @@ impl PartialOrd for List {
Some(Ordering::Equal) Some(Ordering::Equal)
} }
} }
impl Into<Value> for List {
fn into(self) -> Value {
Value::List(self)
}
}

View File

@ -3,6 +3,11 @@ use crossbeam::bounded;
use crossbeam::Sender; use crossbeam::Sender;
use crossbeam::Receiver; use crossbeam::Receiver;
use std::thread; use std::thread;
use crate::lang::printer::PrinterMessage::*;
use std::thread::JoinHandle;
use termion::terminal_size;
use std::cmp::max;
use crate::lang::ast::location::Location;
enum PrinterMessage { enum PrinterMessage {
Ping, Ping,
@ -12,12 +17,6 @@ enum PrinterMessage {
// Lines(Vec<String>), // Lines(Vec<String>),
} }
use crate::lang::printer::PrinterMessage::*;
use std::thread::JoinHandle;
use termion::terminal_size;
use std::cmp::max;
use crate::lang::ast::location::Location;
#[derive(Clone)] #[derive(Clone)]
pub struct Printer { pub struct Printer {
source: Option<(String, Location)>, source: Option<(String, Location)>,

View File

@ -19,7 +19,7 @@ impl Serializable<List> for List {
if let element::Element::List(l) = elements[id].element.as_ref().unwrap() { if let element::Element::List(l) = elements[id].element.as_ref().unwrap() {
let element_type = let element_type =
ValueType::deserialize(l.element_type as usize, elements, state)?; ValueType::deserialize(l.element_type as usize, elements, state)?;
let list = List::new(element_type, vec![]); let list = List::new(element_type, []);
state.lists.insert(id, list.clone()); state.lists.insert(id, list.clone());
for el_id in &l.elements { for el_id in &l.elements {

View File

@ -206,7 +206,7 @@ pub fn declare(root: &Scope) -> CrushResult<()> {
"control", "control",
"Commands for flow control, (loops, etc)", "Commands for flow control, (loops, etc)",
Box::new(move |env| { Box::new(move |env| {
let path = List::new(ValueType::File, vec![]); let path = List::new(ValueType::File, []);
to_crush_error(env::var("PATH").map(|v| { to_crush_error(env::var("PATH").map(|v| {
let mut dirs: Vec<Value> = v let mut dirs: Vec<Value> = v
.split(':') .split(':')

View File

@ -21,7 +21,7 @@ fn make_env() -> Value {
} }
fn make_arguments() -> Value { fn make_arguments() -> Value {
Value::List(List::new(ValueType::String, env::args().map(|a| {Value::string(a)}).collect())) List::new(ValueType::String, env::args().map(|a| {Value::string(a)}).collect::<Vec<_>>()).into()
} }

View File

@ -516,7 +516,7 @@ fn deserialize(iter: &mut dbus::arg::Iter) -> CrushResult<Value> {
} else { } else {
ValueType::Any ValueType::Any
}; };
Value::List(List::new(list_type, res)) List::new(list_type, res).into()
} }
} }
ArgType::Variant => { ArgType::Variant => {
@ -548,7 +548,7 @@ fn deserialize(iter: &mut dbus::arg::Iter) -> CrushResult<Value> {
Err(e) => if e.is_eof() { break; } else { return Err(e); }, Err(e) => if e.is_eof() { break; } else { return Err(e); },
} }
} }
Value::List(List::new(ValueType::Any, res)) List::new(ValueType::Any, res).into()
} }
ArgType::ObjectPath => panic!("unimplemented"), ArgType::ObjectPath => panic!("unimplemented"),
ArgType::Signature => panic!("unimplemented"), ArgType::Signature => panic!("unimplemented"),
@ -685,13 +685,13 @@ fn service_call(mut context: CommandContext) -> CrushResult<()> {
let dbus = DBusThing::new(to_crush_error(Connection::new_session())?); let dbus = DBusThing::new(to_crush_error(Connection::new_session())?);
let mut objects = dbus.list_objects(&service)?; let mut objects = dbus.list_objects(&service)?;
match (cfg.object, cfg.method) { match (cfg.object, cfg.method) {
(None, None) => context.output.send(Value::List(List::new( (None, None) => context.output.send(List::new(
ValueType::String, ValueType::String,
objects.drain(..).map(|d| Value::String(d.path)).collect(), objects.drain(..).map(|d| Value::String(d.path)).collect::<Vec<_>>(),
))), ).into()),
(Some(object), None) => { (Some(object), None) => {
let mut object = filter_object(objects, object)?; let mut object = filter_object(objects, object)?;
context.output.send(Value::List(List::new( context.output.send(List::new(
ValueType::String, ValueType::String,
object object
.interfaces .interfaces
@ -702,8 +702,8 @@ fn service_call(mut context: CommandContext) -> CrushResult<()> {
.map(|m| Value::String(format!("{}.{}", &i.name, &m.name))) .map(|m| Value::String(format!("{}.{}", &i.name, &m.name)))
.collect::<Vec<_>>() .collect::<Vec<_>>()
}) })
.collect(), .collect::Vec<_>(),
))) ).into())
} }
(Some(object), Some(method)) => { (Some(object), Some(method)) => {
let object = filter_object(objects, object)?; let object = filter_object(objects, object)?;

View File

@ -136,11 +136,10 @@ struct Nameserver {}
fn nameserver(mut context: CommandContext) -> CrushResult<()> { fn nameserver(mut context: CommandContext) -> CrushResult<()> {
let rc = resolv_conf()?; let rc = resolv_conf()?;
context.output.send( context.output.send(
Value::List(List::new( List::new(
ValueType::String, ValueType::String,
rc.nameservers.iter().map(|n| {Value::String(n.to_string())}).collect() rc.nameservers.iter().map(|n| {Value::String(n.to_string())}).collect::<Vec<_>>()
))) ).into())
} }
#[signature( #[signature(
@ -153,12 +152,12 @@ struct Search {}
fn search(mut context: CommandContext) -> CrushResult<()> { fn search(mut context: CommandContext) -> CrushResult<()> {
let rc = resolv_conf()?; let rc = resolv_conf()?;
context.output.send( context.output.send(
Value::List(List::new( List::new(
ValueType::String, ValueType::String,
rc.get_search() rc.get_search()
.map(|s|{s.iter().map(|n| {Value::String(n.to_string())}).collect()}) .map(|s|{s.iter().map(|n| {Value::String(n.to_string())}).collect()})
.unwrap_or(vec![]) .unwrap_or(vec![])
)) ).into()
) )
} }

View File

@ -61,10 +61,10 @@ fn from_json(json_value: &serde_json::Value) -> CrushResult<Value> {
row_list, row_list,
))) )))
} }
_ => Ok(Value::List(List::new(list_type.clone(), lst))), _ => Ok(List::new(list_type.clone(), lst).into()),
} }
} }
_ => Ok(Value::List(List::new(ValueType::Any, lst))), _ => Ok(List::new(ValueType::Any, lst).into()),
} }
} }
serde_json::Value::Object(o) => Ok(Value::Struct(Struct::new( serde_json::Value::Object(o) => Ok(Value::Struct(Struct::new(

View File

@ -50,14 +50,14 @@ struct Dir {
pub fn dir(context: CommandContext) -> CrushResult<()> { pub fn dir(context: CommandContext) -> CrushResult<()> {
let cfg: Dir = Dir::parse(context.arguments, &context.global_state.printer())?; let cfg: Dir = Dir::parse(context.arguments, &context.global_state.printer())?;
context.output.send(Value::List(List::new( context.output.send(List::new(
ValueType::String, ValueType::String,
cfg.value cfg.value
.fields() .fields()
.drain(..) .drain(..)
.map(|n| Value::String(n)) .map(|n| Value::String(n))
.collect(), .collect::<Vec<_>>(),
))) ).into())
} }
#[signature(echo, can_block = false, short = "Prints all arguments directly to the screen", output = Known(ValueType::Empty), example = "echo \"Hello, world!\"")] #[signature(echo, can_block = false, short = "Prints all arguments directly to the screen", output = Known(ValueType::Empty), example = "echo \"Hello, world!\"")]

View File

@ -53,10 +53,10 @@ fn from_toml(toml_value: &toml::Value) -> CrushResult<Value> {
row_list, row_list,
))) )))
} }
_ => Ok(Value::List(List::new(list_type.clone(), lst))), _ => Ok(List::new(list_type.clone(), lst).into()),
} }
} }
_ => Ok(Value::List(List::new(ValueType::Any, lst))), _ => Ok(List::new(ValueType::Any, lst).into()),
} }
} }
toml::Value::Table(t) => Ok(Value::Struct(Struct::new( toml::Value::Table(t) => Ok(Value::Struct(Struct::new(

View File

@ -62,10 +62,10 @@ fn from_yaml(yaml_value: &serde_yaml::Value) -> CrushResult<Value> {
row_list, row_list,
))) )))
} }
_ => Ok(Value::List(List::new(list_type.clone(), lst))), _ => Ok(List::new(list_type.clone(), lst).into()),
} }
} }
_ => Ok(Value::List(List::new(ValueType::Any, lst))), _ => Ok(List::new(ValueType::Any, lst).into()),
} }
} }
serde_yaml::Value::Mapping(o) => { serde_yaml::Value::Mapping(o) => {

View File

@ -83,8 +83,8 @@ fn files(mut context: CommandContext) -> CrushResult<()> {
let g = context.this.glob()?; let g = context.this.glob()?;
let mut files = Vec::new(); let mut files = Vec::new();
g.glob_files(&cwd()?, &mut files)?; g.glob_files(&cwd()?, &mut files)?;
context.output.send(Value::List(List::new( context.output.send(List::new(
ValueType::File, ValueType::File,
files.drain(..).map(|f| Value::File(f)).collect(), files.drain(..).map(|f| Value::File(f)).collect::<Vec<_>>(),
))) ).into())
} }

View File

@ -173,7 +173,7 @@ fn repeat(context: CommandContext) -> CrushResult<()> {
} }
context context
.output .output
.send(Value::List(List::new(cfg.item.value_type(), l))) .send(List::new(cfg.item.value_type(), l).into())
} }
#[signature( #[signature(
@ -244,7 +244,7 @@ fn collect(mut context: CommandContext) -> CrushResult<()> {
} }
context context
.output .output
.send(Value::List(List::new(input_type[0].cell_type.clone(), lst))) .send(List::new(input_type[0].cell_type.clone(), lst).into())
} }
1 => { 1 => {
match &context.arguments[0].value { match &context.arguments[0].value {
@ -256,7 +256,7 @@ fn collect(mut context: CommandContext) -> CrushResult<()> {
} }
context context
.output .output
.send(Value::List(List::new(input_type[idx].cell_type.clone(), lst))) .send(List::new(input_type[idx].cell_type.clone(), lst).into())
} }
_ => argument_error("Column not found", context.arguments[0].location) _ => argument_error("Column not found", context.arguments[0].location)
} }
@ -271,7 +271,7 @@ fn collect(mut context: CommandContext) -> CrushResult<()> {
fn new(mut context: CommandContext) -> CrushResult<()> { fn new(mut context: CommandContext) -> CrushResult<()> {
context.arguments.check_len(0)?; context.arguments.check_len(0)?;
match context.this.r#type()? { match context.this.r#type()? {
ValueType::List(t) => context.output.send(Value::List(List::new(*t, vec![]))), ValueType::List(t) => context.output.send(List::new(*t, []).into()),
_ => argument_error_legacy("Expected this to be a list type"), _ => argument_error_legacy("Expected this to be a list type"),
} }
} }

View File

@ -104,9 +104,9 @@ struct All {}
fn __all__(mut context: CommandContext) -> CrushResult<()> { fn __all__(mut context: CommandContext) -> CrushResult<()> {
let scope = context.this.scope()?; let scope = context.this.scope()?;
context.output.send(Value::List( context.output.send(
List::new(ValueType::String, List::new(ValueType::String,
scope.dump()?.iter().map(|e| {Value::string(e.0)}).collect()))) scope.dump()?.iter().map(|e| {Value::string(e.0)}).collect::<Vec<_>>()).into())
} }
#[signature( #[signature(
@ -119,9 +119,9 @@ struct Local {}
fn __local__(mut context: CommandContext) -> CrushResult<()> { fn __local__(mut context: CommandContext) -> CrushResult<()> {
let scope = context.this.scope()?; let scope = context.this.scope()?;
context.output.send(Value::List( context.output.send(
List::new(ValueType::String, List::new(ValueType::String,
scope.dump_local()?.iter().map(|e| {Value::string(e.0)}).collect()))) scope.dump_local()?.iter().map(|e| {Value::string(e.0)}).collect::<Vec<_>>()).into())
} }
#[signature( #[signature(
@ -163,5 +163,5 @@ struct Use {}
fn __use__(mut context: CommandContext) -> CrushResult<()> { fn __use__(mut context: CommandContext) -> CrushResult<()> {
let scope = context.this.scope()?; let scope = context.this.scope()?;
context.output.send( context.output.send(
Value::List(List::new(ValueType::Scope, scope.get_use().drain(..).map(|s| {Value::Scope(s)}).collect()))) List::new(ValueType::Scope, scope.get_use().drain(..).map(|s| {Value::Scope(s)}).collect::<Vec<_>>()).into())
} }

View File

@ -104,10 +104,10 @@ fn split(mut context: CommandContext) -> CrushResult<()> {
let cfg: Split = Split::parse(context.arguments, &context.global_state.printer())?; let cfg: Split = Split::parse(context.arguments, &context.global_state.printer())?;
let this = context.this.string()?; let this = context.this.string()?;
context.output.send(Value::List(List::new( context.output.send(List::new(
ValueType::String, ValueType::String,
this.split(&cfg.separator).map(|s| Value::string(s)).collect(), this.split(&cfg.separator).map(|s| Value::string(s)).collect::<Vec<_>>(),
))) ).into())
} }
#[signature( #[signature(