2019-06-01 05:50:16 +00:00
|
|
|
use derive_new::new;
|
2020-08-18 07:00:02 +00:00
|
|
|
use nu_protocol::{Dictionary, MaybeOwned, Primitive, UntaggedValue, Value};
|
|
|
|
use nu_source::{b, DebugDocBuilder, PrettyDebug, Spanned, Tag};
|
2019-05-10 16:59:12 +00:00
|
|
|
|
2019-11-04 15:47:03 +00:00
|
|
|
#[derive(Debug, new)]
|
|
|
|
struct DebugEntry<'a> {
|
|
|
|
key: &'a str,
|
2019-11-21 14:33:14 +00:00
|
|
|
value: &'a Value,
|
2019-11-04 15:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PrettyDebug for DebugEntry<'a> {
|
2019-11-21 14:33:14 +00:00
|
|
|
fn pretty(&self) -> DebugDocBuilder {
|
2019-12-07 09:34:32 +00:00
|
|
|
(b::key(self.key.to_string()) + b::equals() + self.value.pretty().into_value()).group()
|
2019-11-04 15:47:03 +00: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 02:30:48 +00:00
|
|
|
pub trait DictionaryExt {
|
2019-12-06 15:28:26 +00:00
|
|
|
fn get_data(&self, desc: &str) -> MaybeOwned<'_, Value>;
|
2019-06-24 00:55:31 +00: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 02:30:48 +00: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 15:55:50 +00: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 02:30:48 +00:00
|
|
|
impl DictionaryExt for Dictionary {
|
2019-12-06 15:28:26 +00:00
|
|
|
fn get_data(&self, desc: &str) -> MaybeOwned<'_, Value> {
|
2019-05-24 18:48:33 +00:00
|
|
|
match self.entries.get(desc) {
|
2019-05-15 18:14:51 +00:00
|
|
|
Some(v) => MaybeOwned::Borrowed(v),
|
2019-11-21 14:33:14 +00:00
|
|
|
None => MaybeOwned::Owned(
|
|
|
|
UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value(),
|
|
|
|
),
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 15:55:50 +00: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 02:30:48 +00:00
|
|
|
fn keys(&self) -> indexmap::map::Keys<String, Value> {
|
2019-11-04 15:47:03 +00: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 02:30:48 +00:00
|
|
|
fn get_data_by_key(&self, name: Spanned<&str>) -> Option<Value> {
|
2019-11-04 15:47:03 +00:00
|
|
|
let result = self
|
2019-05-24 18:48:33 +00:00
|
|
|
.entries
|
|
|
|
.iter()
|
2019-11-04 15:47:03 +00:00
|
|
|
.find(|(desc_name, _)| *desc_name == name.item)?
|
|
|
|
.1;
|
|
|
|
|
|
|
|
Some(
|
|
|
|
result
|
2019-11-21 14:33:14 +00:00
|
|
|
.value
|
2019-11-04 15:47:03 +00:00
|
|
|
.clone()
|
2019-11-21 14:33:14 +00:00
|
|
|
.into_value(Tag::new(result.anchor(), name.span)),
|
2019-11-04 15:47:03 +00:00
|
|
|
)
|
2019-05-17 15:55:50 +00:00
|
|
|
}
|
2019-06-22 03:43:37 +00: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 02:30:48 +00:00
|
|
|
fn get_mut_data_by_key(&mut self, name: &str) -> Option<&mut Value> {
|
2020-08-03 17:43:27 +00:00
|
|
|
self.entries
|
2019-10-20 11:55:56 +00:00
|
|
|
.iter_mut()
|
|
|
|
.find(|(desc_name, _)| *desc_name == name)
|
2020-08-03 17:43:27 +00:00
|
|
|
.map_or_else(|| None, |x| Some(x.1))
|
2019-10-20 11:55:56 +00: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 02:30:48 +00:00
|
|
|
fn insert_data_at_key(&mut self, name: &str, value: Value) {
|
2019-11-04 15:47:03 +00:00
|
|
|
self.entries.insert(name.to_string(), value);
|
2019-06-22 03:43:37 +00:00
|
|
|
}
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
2019-07-08 16:44:53 +00:00
|
|
|
|
2019-09-02 05:37:13 +00:00
|
|
|
#[derive(Debug)]
|
2019-08-01 01:58:42 +00:00
|
|
|
pub struct TaggedListBuilder {
|
2019-09-03 07:43:37 +00:00
|
|
|
tag: Tag,
|
2019-11-21 14:33:14 +00:00
|
|
|
pub list: Vec<Value>,
|
2019-07-09 04:31:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
impl TaggedListBuilder {
|
2019-08-05 08:54:29 +00:00
|
|
|
pub fn new(tag: impl Into<Tag>) -> TaggedListBuilder {
|
2019-08-01 01:58:42 +00:00
|
|
|
TaggedListBuilder {
|
2019-08-05 08:54:29 +00:00
|
|
|
tag: tag.into(),
|
2019-07-09 04:31:26 +00:00
|
|
|
list: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub fn push_value(&mut self, value: impl Into<Value>) {
|
|
|
|
self.list.push(value.into());
|
2019-07-09 04:31:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00: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 04:31:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub fn into_untagged_value(self) -> UntaggedValue {
|
|
|
|
UntaggedValue::Table(self.list).into_value(self.tag).value
|
2019-07-09 04:31:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
impl From<TaggedListBuilder> for Value {
|
|
|
|
fn from(input: TaggedListBuilder) -> Value {
|
|
|
|
input.into_value()
|
2019-07-09 04:31:26 +00:00
|
|
|
}
|
|
|
|
}
|