2019-05-16 00:58:44 +02:00
|
|
|
use crate::prelude::*;
|
2019-06-01 07:50:16 +02:00
|
|
|
use derive_new::new;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_protocol::{Dictionary, Primitive, UntaggedValue, Value};
|
|
|
|
use nu_source::{b, PrettyDebug, Spanned};
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
#[derive(Debug, new)]
|
|
|
|
struct DebugEntry<'a> {
|
|
|
|
key: &'a str,
|
2019-11-21 15:33:14 +01:00
|
|
|
value: &'a Value,
|
2019-11-04 16:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PrettyDebug for DebugEntry<'a> {
|
2019-11-21 15:33:14 +01:00
|
|
|
fn pretty(&self) -> DebugDocBuilder {
|
2019-12-07 10:34:32 +01:00
|
|
|
(b::key(self.key.to_string()) + b::equals() + self.value.pretty().into_value()).group()
|
2019-11-04 16:47:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
pub trait DictionaryExt {
|
2019-12-06 16:28:26 +01:00
|
|
|
fn get_data(&self, desc: &str) -> MaybeOwned<'_, Value>;
|
2019-06-24 02:55:31 +02:00
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
fn keys(&self) -> indexmap::map::Keys<String, Value>;
|
|
|
|
fn get_data_by_key(&self, name: Spanned<&str>) -> Option<Value>;
|
|
|
|
fn get_mut_data_by_key(&mut self, name: &str) -> Option<&mut Value>;
|
|
|
|
fn insert_data_at_key(&mut self, name: &str, value: Value);
|
2019-05-17 17:55:50 +02:00
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
impl DictionaryExt for Dictionary {
|
2019-12-06 16:28:26 +01:00
|
|
|
fn get_data(&self, desc: &str) -> MaybeOwned<'_, Value> {
|
2019-05-24 20:48:33 +02:00
|
|
|
match self.entries.get(desc) {
|
2019-05-15 20:14:51 +02:00
|
|
|
Some(v) => MaybeOwned::Borrowed(v),
|
2019-11-21 15:33:14 +01:00
|
|
|
None => MaybeOwned::Owned(
|
|
|
|
UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value(),
|
|
|
|
),
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 17:55:50 +02:00
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
fn keys(&self) -> indexmap::map::Keys<String, Value> {
|
2019-11-04 16:47:03 +01:00
|
|
|
self.entries.keys()
|
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
fn get_data_by_key(&self, name: Spanned<&str>) -> Option<Value> {
|
2019-11-04 16:47:03 +01:00
|
|
|
let result = self
|
2019-05-24 20:48:33 +02:00
|
|
|
.entries
|
|
|
|
.iter()
|
2019-11-04 16:47:03 +01:00
|
|
|
.find(|(desc_name, _)| *desc_name == name.item)?
|
|
|
|
.1;
|
|
|
|
|
|
|
|
Some(
|
|
|
|
result
|
2019-11-21 15:33:14 +01:00
|
|
|
.value
|
2019-11-04 16:47:03 +01:00
|
|
|
.clone()
|
2019-11-21 15:33:14 +01:00
|
|
|
.into_value(Tag::new(result.anchor(), name.span)),
|
2019-11-04 16:47:03 +01:00
|
|
|
)
|
2019-05-17 17:55:50 +02:00
|
|
|
}
|
2019-06-22 05:43:37 +02:00
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
fn get_mut_data_by_key(&mut self, name: &str) -> Option<&mut Value> {
|
2019-10-20 13:55:56 +02:00
|
|
|
match self
|
|
|
|
.entries
|
|
|
|
.iter_mut()
|
|
|
|
.find(|(desc_name, _)| *desc_name == name)
|
|
|
|
{
|
|
|
|
Some((_, v)) => Some(v),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
fn insert_data_at_key(&mut self, name: &str, value: Value) {
|
2019-11-04 16:47:03 +01:00
|
|
|
self.entries.insert(name.to_string(), value);
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
2019-07-08 18:44:53 +02:00
|
|
|
|
2019-09-02 07:37:13 +02:00
|
|
|
#[derive(Debug)]
|
2019-08-01 03:58:42 +02:00
|
|
|
pub struct TaggedListBuilder {
|
2019-09-03 09:43:37 +02:00
|
|
|
tag: Tag,
|
2019-11-21 15:33:14 +01:00
|
|
|
pub list: Vec<Value>,
|
2019-07-09 06:31:26 +02:00
|
|
|
}
|
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
impl TaggedListBuilder {
|
2019-08-05 10:54:29 +02:00
|
|
|
pub fn new(tag: impl Into<Tag>) -> TaggedListBuilder {
|
2019-08-01 03:58:42 +02:00
|
|
|
TaggedListBuilder {
|
2019-08-05 10:54:29 +02:00
|
|
|
tag: tag.into(),
|
2019-07-09 06:31:26 +02:00
|
|
|
list: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn push_value(&mut self, value: impl Into<Value>) {
|
|
|
|
self.list.push(value.into());
|
2019-07-09 06:31:26 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn push_untagged(&mut self, value: impl Into<UntaggedValue>) {
|
|
|
|
self.list.push(value.into().into_value(self.tag.clone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_value(self) -> Value {
|
|
|
|
UntaggedValue::Table(self.list).into_value(self.tag)
|
2019-07-09 06:31:26 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn into_untagged_value(self) -> UntaggedValue {
|
|
|
|
UntaggedValue::Table(self.list).into_value(self.tag).value
|
2019-07-09 06:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl From<TaggedListBuilder> for Value {
|
|
|
|
fn from(input: TaggedListBuilder) -> Value {
|
|
|
|
input.into_value()
|
2019-07-09 06:31:26 +02:00
|
|
|
}
|
|
|
|
}
|