Docstring some intricacies around SyntaxShape (#10544)

Inspired by @fdncred and @amtoine's questions
https://github.com/nushell/nushell/pull/10512#issuecomment-1739996967
This commit is contained in:
Stefan Holderbach
2023-09-29 16:35:22 +02:00
committed by GitHub
parent 7ad4c679b3
commit f2af12af2c
2 changed files with 32 additions and 7 deletions

View File

@ -1,10 +1,14 @@
use crate::{DeclId, Type};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use serde::{Deserialize, Serialize};
use crate::{DeclId, Type};
/// The syntactic shapes that values must match to be passed into a command. You can think of this as the type-checking that occurs when you call a function.
/// The syntactic shapes that describe how a sequence should be parsed.
///
/// This extends beyond [`Type`] which describes how [`Value`](crate::Value)s are represented.
/// `SyntaxShape`s can describe the parsing rules for arguments to a command.
/// e.g. [`SyntaxShape::GlobPattern`]/[`SyntaxShape::Filepath`] serve the completer,
/// but don't have an associated [`Value`](crate::Value)
/// There are additional `SyntaxShape`s that only make sense in particular expressions or keywords
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SyntaxShape {
/// Any syntactic form is allowed
@ -52,7 +56,9 @@ pub enum SyntaxShape {
/// A floating point value, eg `1.0`
Float,
/// A dotted path to navigate the table (including variable)
/// A dotted path including the variable to access items
///
/// Fully qualified
FullCellPath,
/// A glob pattern is allowed, eg `foo*`
@ -115,6 +121,16 @@ pub enum SyntaxShape {
}
impl SyntaxShape {
/// If possible provide the associated concrete [`Type`]
///
/// Note: Some [`SyntaxShape`]s don't have a corresponding [`Value`](crate::Value)
/// Here we currently return [`Type::Any`]
///
/// ```rust
/// use nu_protocol::{SyntaxShape, Type};
/// let non_value = SyntaxShape::ImportPattern;
/// assert_eq!(non_value.to_type(), Type::Any);
/// ```
pub fn to_type(&self) -> Type {
let mk_ty = |tys: &[(String, SyntaxShape)]| {
tys.iter()