Rename the Path and Pattern primitives (#2889)

* Rename the Path primitive to FilePath

* Rename glob pattern also

* more fun

* Fix the Windows path methods
This commit is contained in:
Jonathan Turner
2021-01-08 20:30:41 +13:00
committed by GitHub
parent 2dcb16870b
commit 0e13d9fbaa
48 changed files with 146 additions and 131 deletions

View File

@ -1092,7 +1092,7 @@ impl Expression {
}))
}
pub fn pattern(p: String) -> Expression {
pub fn glob_pattern(p: String) -> Expression {
Expression::Literal(Literal::GlobPattern(p))
}

View File

@ -19,9 +19,9 @@ pub enum SyntaxShape {
/// Only an integer value is allowed
Int,
/// A filepath is allowed
Path,
FilePath,
/// A glob pattern is allowed, eg `foo*`
Pattern,
GlobPattern,
/// A block is allowed, eg `{start this thing}`
Block,
/// A table is allowed, eg `[first second]`
@ -48,8 +48,8 @@ impl PrettyDebug for SyntaxShape {
SyntaxShape::Number => "number",
SyntaxShape::Range => "range",
SyntaxShape::Int => "integer",
SyntaxShape::Path => "file path",
SyntaxShape::Pattern => "pattern",
SyntaxShape::FilePath => "file path",
SyntaxShape::GlobPattern => "pattern",
SyntaxShape::Block => "block",
SyntaxShape::Table => "table",
SyntaxShape::Unit => "unit",

View File

@ -43,7 +43,7 @@ pub enum Type {
/// A path through a table
ColumnPath,
/// A glob pattern (like foo*)
Pattern,
GlobPattern,
/// A boolean value
Boolean,
/// A date value (in UTC)
@ -51,7 +51,7 @@ pub enum Type {
/// A data duration value
Duration,
/// A filepath value
Path,
FilePath,
/// A binary (non-text) buffer value
Binary,
@ -141,11 +141,11 @@ impl Type {
Primitive::Filesize(_) => Type::Filesize,
Primitive::String(_) => Type::String,
Primitive::ColumnPath(_) => Type::ColumnPath,
Primitive::Pattern(_) => Type::Pattern,
Primitive::GlobPattern(_) => Type::GlobPattern,
Primitive::Boolean(_) => Type::Boolean,
Primitive::Date(_) => Type::Date,
Primitive::Duration(_) => Type::Duration,
Primitive::Path(_) => Type::Path,
Primitive::FilePath(_) => Type::FilePath,
Primitive::Binary(_) => Type::Binary,
Primitive::BeginningOfStream => Type::BeginningOfStream,
Primitive::EndOfStream => Type::EndOfStream,
@ -223,11 +223,11 @@ impl PrettyDebug for Type {
Type::String => ty("string"),
Type::Line => ty("line"),
Type::ColumnPath => ty("column-path"),
Type::Pattern => ty("pattern"),
Type::GlobPattern => ty("pattern"),
Type::Boolean => ty("boolean"),
Type::Date => ty("date"),
Type::Duration => ty("duration"),
Type::Path => ty("path"),
Type::FilePath => ty("path"),
Type::Binary => ty("binary"),
Type::Error => b::error("error"),
Type::BeginningOfStream => b::keyword("beginning-of-stream"),

View File

@ -175,13 +175,13 @@ impl UntaggedValue {
}
/// Helper for creating glob pattern values
pub fn pattern(s: impl Into<String>) -> UntaggedValue {
pub fn glob_pattern(s: impl Into<String>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::String(s.into()))
}
/// Helper for creating filepath values
pub fn path(s: impl Into<PathBuf>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Path(s.into()))
pub fn filepath(s: impl Into<PathBuf>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::FilePath(s.into()))
}
/// Helper for creating filesize values
@ -312,7 +312,7 @@ impl Value {
pub fn as_string(&self) -> Result<String, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::String(string)) => Ok(string.clone()),
UntaggedValue::Primitive(Primitive::Path(path)) => {
UntaggedValue::Primitive(Primitive::FilePath(path)) => {
Ok(path.to_string_lossy().to_string())
}
_ => Err(ShellError::type_error("string", self.spanned_type_name())),
@ -327,7 +327,7 @@ impl Value {
UntaggedValue::Primitive(Primitive::Decimal(x)) => format!("{}", x),
UntaggedValue::Primitive(Primitive::Int(x)) => format!("{}", x),
UntaggedValue::Primitive(Primitive::Filesize(x)) => format!("{}", x),
UntaggedValue::Primitive(Primitive::Path(x)) => format!("{}", x.display()),
UntaggedValue::Primitive(Primitive::FilePath(x)) => format!("{}", x.display()),
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => {
let joined: String = path
.iter()
@ -364,7 +364,7 @@ impl Value {
/// View the Value as a path, if possible
pub fn as_path(&self) -> Result<PathBuf, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::Path(path)) => Ok(path.clone()),
UntaggedValue::Primitive(Primitive::FilePath(path)) => Ok(path.clone()),
UntaggedValue::Primitive(Primitive::String(path_str)) => Ok(PathBuf::from(&path_str)),
_ => Err(ShellError::type_error("Path", self.spanned_type_name())),
}
@ -623,13 +623,13 @@ impl StringExt for String {
fn to_pattern_value(&self, the_tag: Tag) -> Value {
Value {
value: UntaggedValue::Primitive(Primitive::Pattern(self.to_string())),
value: UntaggedValue::Primitive(Primitive::GlobPattern(self.to_string())),
tag: the_tag,
}
}
fn to_pattern_untagged_value(&self) -> UntaggedValue {
UntaggedValue::pattern(self)
UntaggedValue::glob_pattern(self)
}
}
@ -682,13 +682,13 @@ impl StrExt for &str {
fn to_pattern_value(&self, the_tag: Tag) -> Value {
Value {
value: UntaggedValue::Primitive(Primitive::Pattern(self.to_string())),
value: UntaggedValue::Primitive(Primitive::GlobPattern(self.to_string())),
tag: the_tag,
}
}
fn to_pattern_untagged_value(&self) -> UntaggedValue {
UntaggedValue::pattern(*self)
UntaggedValue::glob_pattern(*self)
}
}
@ -830,7 +830,7 @@ impl PathBufExt for PathBuf {
fn to_value(&self, the_tag: Tag) -> Value {
let pb = self.clone();
Value {
value: UntaggedValue::Primitive(Primitive::Path(pb)),
value: UntaggedValue::Primitive(Primitive::FilePath(pb)),
tag: the_tag,
}
}
@ -842,7 +842,7 @@ impl PathBufExt for PathBuf {
.len();
let pb = self.clone();
Value {
value: UntaggedValue::Primitive(Primitive::Path(pb)),
value: UntaggedValue::Primitive(Primitive::FilePath(pb)),
tag: Tag {
anchor: None,
span: Span::new(0, end),
@ -852,7 +852,7 @@ impl PathBufExt for PathBuf {
fn to_untagged_value(&self) -> UntaggedValue {
let pb = self.clone();
UntaggedValue::path(pb)
UntaggedValue::filepath(pb)
}
}
@ -988,7 +988,7 @@ mod tests {
let a_pattern = r"[a-zA-Z0-9 ]";
assert_eq!(
a_pattern.to_pattern_untagged_value(),
UntaggedValue::pattern(a_pattern)
UntaggedValue::glob_pattern(a_pattern)
);
}

View File

@ -36,11 +36,11 @@ impl PrettyType for Primitive {
Primitive::Filesize(_) => ty("filesize"),
Primitive::String(_) => ty("string"),
Primitive::ColumnPath(_) => ty("column-path"),
Primitive::Pattern(_) => ty("pattern"),
Primitive::GlobPattern(_) => ty("pattern"),
Primitive::Boolean(_) => ty("boolean"),
Primitive::Date(_) => ty("date"),
Primitive::Duration(_) => ty("duration"),
Primitive::Path(_) => ty("path"),
Primitive::FilePath(_) => ty("path"),
Primitive::Binary(_) => ty("binary"),
Primitive::BeginningOfStream => b::keyword("beginning-of-stream"),
Primitive::EndOfStream => b::keyword("end-of-stream"),
@ -73,14 +73,14 @@ impl PrettyDebug for Primitive {
Primitive::Filesize(bytes) => primitive_doc(bytes, "filesize"),
Primitive::String(string) => prim(string),
Primitive::ColumnPath(path) => path.pretty(),
Primitive::Pattern(pattern) => primitive_doc(pattern, "pattern"),
Primitive::GlobPattern(pattern) => primitive_doc(pattern, "pattern"),
Primitive::Boolean(boolean) => match boolean {
true => b::primitive("$yes"),
false => b::primitive("$no"),
},
Primitive::Date(date) => primitive_doc(date, "date"),
Primitive::Duration(duration) => primitive_doc(duration, "nanoseconds"),
Primitive::Path(path) => primitive_doc(path, "path"),
Primitive::FilePath(path) => primitive_doc(path, "path"),
Primitive::Binary(_) => b::opaque("binary"),
Primitive::BeginningOfStream => b::keyword("beginning-of-stream"),
Primitive::EndOfStream => b::keyword("end-of-stream"),

View File

@ -37,7 +37,7 @@ pub enum Primitive {
/// A path to travel to reach a value in a table
ColumnPath(ColumnPath),
/// A glob pattern, eg foo*
Pattern(String),
GlobPattern(String),
/// A boolean value
Boolean(bool),
/// A date value
@ -48,7 +48,7 @@ pub enum Primitive {
/// A range of values
Range(Box<Range>),
/// A file path
Path(PathBuf),
FilePath(PathBuf),
/// A vector of raw binary data
#[serde(with = "serde_bytes")]
Binary(Vec<u8>),
@ -234,11 +234,11 @@ impl ShellTypeName for Primitive {
Primitive::Filesize(_) => "filesize(in bytes)",
Primitive::String(_) => "string",
Primitive::ColumnPath(_) => "column path",
Primitive::Pattern(_) => "pattern",
Primitive::GlobPattern(_) => "pattern",
Primitive::Boolean(_) => "boolean",
Primitive::Date(_) => "date",
Primitive::Duration(_) => "duration",
Primitive::Path(_) => "file path",
Primitive::FilePath(_) => "file path",
Primitive::Binary(_) => "binary",
Primitive::BeginningOfStream => "marker<beginning of stream>",
Primitive::EndOfStream => "marker<end of stream>",
@ -252,7 +252,7 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
Primitive::Nothing => String::new(),
Primitive::BeginningOfStream => String::new(),
Primitive::EndOfStream => String::new(),
Primitive::Path(p) => format!("{}", p.display()),
Primitive::FilePath(p) => format!("{}", p.display()),
Primitive::Filesize(num_bytes) => {
let byte = byte_unit::Byte::from_bytes(*num_bytes as u128);
@ -289,7 +289,7 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
},
format_primitive(&range.to.0.item, None)
),
Primitive::Pattern(s) => s.to_string(),
Primitive::GlobPattern(s) => s.to_string(),
Primitive::String(s) => s.to_owned(),
Primitive::ColumnPath(p) => {
let mut members = p.iter();