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
|
|
|
use crate::syntax_shape::SyntaxShape;
|
2019-05-26 06:54:41 +00:00
|
|
|
use indexmap::IndexMap;
|
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
|
|
|
use nu_source::{b, DebugDocBuilder, PrettyDebug, PrettyDebugWithSource};
|
2019-07-02 07:56:20 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-05-26 06:54:41 +00:00
|
|
|
|
2019-07-16 07:08:35 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2019-05-28 06:45:18 +00:00
|
|
|
pub enum NamedType {
|
2019-06-01 05:50:16 +00:00
|
|
|
Switch,
|
2019-09-14 16:30:24 +00:00
|
|
|
Mandatory(SyntaxShape),
|
|
|
|
Optional(SyntaxShape),
|
2019-05-26 06:54:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 07:56:20 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2019-05-28 06:45:18 +00:00
|
|
|
pub enum PositionalType {
|
2019-09-14 16:30:24 +00:00
|
|
|
Mandatory(String, SyntaxShape),
|
|
|
|
Optional(String, SyntaxShape),
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
impl PrettyDebug for PositionalType {
|
|
|
|
fn pretty(&self) -> DebugDocBuilder {
|
|
|
|
match self {
|
|
|
|
PositionalType::Mandatory(string, shape) => {
|
2019-12-07 09:34:32 +00:00
|
|
|
b::description(string) + b::delimit("(", shape.pretty(), ")").into_kind().group()
|
2019-11-21 14:33:14 +00:00
|
|
|
}
|
|
|
|
PositionalType::Optional(string, shape) => {
|
|
|
|
b::description(string)
|
|
|
|
+ b::operator("?")
|
2019-12-07 09:34:32 +00:00
|
|
|
+ b::delimit("(", shape.pretty(), ")").into_kind().group()
|
2019-11-21 14:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 06:45:18 +00:00
|
|
|
impl PositionalType {
|
2019-09-14 16:30:24 +00:00
|
|
|
pub fn mandatory(name: &str, ty: SyntaxShape) -> PositionalType {
|
2019-07-15 21:16:27 +00:00
|
|
|
PositionalType::Mandatory(name.to_string(), ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mandatory_any(name: &str) -> PositionalType {
|
2019-09-14 16:30:24 +00:00
|
|
|
PositionalType::Mandatory(name.to_string(), SyntaxShape::Any)
|
2019-07-13 02:07:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 19:10:25 +00:00
|
|
|
pub fn mandatory_block(name: &str) -> PositionalType {
|
2019-09-14 16:30:24 +00:00
|
|
|
PositionalType::Mandatory(name.to_string(), SyntaxShape::Block)
|
2019-07-16 19:10:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 16:30:24 +00:00
|
|
|
pub fn optional(name: &str, ty: SyntaxShape) -> PositionalType {
|
2019-07-16 07:25:48 +00:00
|
|
|
PositionalType::Optional(name.to_string(), ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn optional_any(name: &str) -> PositionalType {
|
2019-09-14 16:30:24 +00:00
|
|
|
PositionalType::Optional(name.to_string(), SyntaxShape::Any)
|
2019-07-16 07:25:48 +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 fn name(&self) -> &str {
|
2019-06-30 06:14:40 +00:00
|
|
|
match self {
|
2019-07-03 20:31:15 +00:00
|
|
|
PositionalType::Mandatory(s, _) => s,
|
|
|
|
PositionalType::Optional(s, _) => s,
|
2019-06-30 06:14:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-15 21:16:27 +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 fn syntax_type(&self) -> SyntaxShape {
|
2019-07-15 21:16:27 +00:00
|
|
|
match *self {
|
|
|
|
PositionalType::Mandatory(_, t) => t,
|
|
|
|
PositionalType::Optional(_, t) => t,
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:15:35 +00:00
|
|
|
type Description = String;
|
|
|
|
|
2019-11-18 03:12:37 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2019-08-02 19:15:07 +00:00
|
|
|
pub struct Signature {
|
2019-07-02 07:56:20 +00:00
|
|
|
pub name: String,
|
2019-08-29 22:52:32 +00:00
|
|
|
pub usage: String,
|
2019-10-28 05:15:35 +00:00
|
|
|
pub positional: Vec<(PositionalType, Description)>,
|
|
|
|
pub rest_positional: Option<(SyntaxShape, Description)>,
|
|
|
|
pub named: IndexMap<String, (NamedType, Description)>,
|
2019-07-02 07:56:20 +00:00
|
|
|
pub is_filter: bool,
|
2019-05-28 06:45:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
impl PrettyDebugWithSource for Signature {
|
|
|
|
fn pretty_debug(&self, source: &str) -> DebugDocBuilder {
|
|
|
|
b::typed(
|
|
|
|
"signature",
|
|
|
|
b::description(&self.name)
|
|
|
|
+ b::preceded(
|
|
|
|
b::space(),
|
|
|
|
b::intersperse(
|
|
|
|
self.positional
|
|
|
|
.iter()
|
|
|
|
.map(|(ty, _)| ty.pretty_debug(source)),
|
|
|
|
b::space(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
impl Signature {
|
2019-11-18 03:12:37 +00:00
|
|
|
pub fn new(name: String) -> Signature {
|
|
|
|
Signature {
|
|
|
|
name,
|
|
|
|
usage: String::new(),
|
|
|
|
positional: vec![],
|
|
|
|
rest_positional: None,
|
|
|
|
named: IndexMap::new(),
|
|
|
|
is_filter: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
pub fn build(name: impl Into<String>) -> Signature {
|
|
|
|
Signature::new(name.into())
|
2019-07-24 04:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 22:52:32 +00:00
|
|
|
pub fn desc(mut self, usage: impl Into<String>) -> Signature {
|
|
|
|
self.usage = usage.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-10-28 05:15:35 +00:00
|
|
|
pub fn required(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
|
|
|
ty: impl Into<SyntaxShape>,
|
|
|
|
desc: impl Into<String>,
|
|
|
|
) -> Signature {
|
|
|
|
self.positional.push((
|
|
|
|
PositionalType::Mandatory(name.into(), ty.into()),
|
|
|
|
desc.into(),
|
|
|
|
));
|
2019-07-24 04:10:48 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-10-28 05:15:35 +00:00
|
|
|
pub fn optional(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
|
|
|
ty: impl Into<SyntaxShape>,
|
|
|
|
desc: impl Into<String>,
|
|
|
|
) -> Signature {
|
|
|
|
self.positional.push((
|
|
|
|
PositionalType::Optional(name.into(), ty.into()),
|
|
|
|
desc.into(),
|
|
|
|
));
|
2019-07-24 04:10:48 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-10-28 05:15:35 +00:00
|
|
|
pub fn named(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
|
|
|
ty: impl Into<SyntaxShape>,
|
|
|
|
desc: impl Into<String>,
|
|
|
|
) -> Signature {
|
2019-08-02 19:15:07 +00:00
|
|
|
self.named
|
2019-10-28 05:15:35 +00:00
|
|
|
.insert(name.into(), (NamedType::Optional(ty.into()), desc.into()));
|
2019-08-02 19:15:07 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn required_named(
|
|
|
|
mut self,
|
|
|
|
name: impl Into<String>,
|
2019-09-14 16:30:24 +00:00
|
|
|
ty: impl Into<SyntaxShape>,
|
2019-10-28 05:15:35 +00:00
|
|
|
desc: impl Into<String>,
|
2019-08-02 19:15:07 +00:00
|
|
|
) -> Signature {
|
|
|
|
self.named
|
2019-10-28 05:15:35 +00:00
|
|
|
.insert(name.into(), (NamedType::Mandatory(ty.into()), desc.into()));
|
2019-08-02 19:15:07 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-10-28 05:15:35 +00:00
|
|
|
pub fn switch(mut self, name: impl Into<String>, desc: impl Into<String>) -> Signature {
|
|
|
|
self.named
|
|
|
|
.insert(name.into(), (NamedType::Switch, desc.into()));
|
2019-07-24 04:10:48 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
pub fn filter(mut self) -> Signature {
|
|
|
|
self.is_filter = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-10-28 05:15:35 +00:00
|
|
|
pub fn rest(mut self, ty: SyntaxShape, desc: impl Into<String>) -> Signature {
|
|
|
|
self.rest_positional = Some((ty, desc.into()));
|
2019-08-02 19:15:07 +00:00
|
|
|
self
|
|
|
|
}
|
2019-07-24 04:10:48 +00:00
|
|
|
}
|