nushell/src/object/meta.rs

256 lines
5.5 KiB
Rust
Raw Normal View History

2019-08-10 22:18:14 +02:00
use crate::context::{SourceMap, SpanSource};
2019-07-24 00:22:11 +02:00
use crate::prelude::*;
2019-06-22 22:46:16 +02:00
use crate::Text;
2019-06-11 07:53:04 +02:00
use derive_new::new;
2019-06-22 03:36:57 +02:00
use getset::Getters;
2019-08-02 21:15:07 +02:00
use serde::Deserialize;
use serde::Serialize;
use uuid::Uuid;
2019-06-11 07:53:04 +02:00
#[derive(new, Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
pub struct Tagged<T> {
pub tag: Tag,
2019-06-27 06:56:48 +02:00
pub item: T,
2019-06-11 07:53:04 +02:00
}
2019-08-09 06:51:21 +02:00
impl<T> HasSpan for Tagged<T> {
2019-07-24 00:22:11 +02:00
fn span(&self) -> Span {
2019-08-09 06:51:21 +02:00
self.tag.span
2019-07-24 00:22:11 +02:00
}
}
pub trait TaggedItem: Sized {
fn tagged(self, tag: impl Into<Tag>) -> Tagged<Self> {
Tagged::from_item(self, tag.into())
2019-07-09 06:31:26 +02:00
}
fn simple_spanned(self, span: impl Into<Span>) -> Tagged<Self> {
Tagged::from_simple_spanned_item(self, span.into())
}
2019-07-08 18:44:53 +02:00
// For now, this is a temporary facility. In many cases, there are other useful spans that we
// could be using, such as the original source spans of JSON or Toml files, but we don't yet
// have the infrastructure to make that work.
fn tagged_unknown(self) -> Tagged<Self> {
Tagged::from_item(
self,
Tag {
span: Span::unknown(),
origin: None,
},
)
2019-07-08 18:44:53 +02:00
}
}
impl<T> TaggedItem for T {}
impl<T> std::ops::Deref for Tagged<T> {
2019-06-11 07:53:04 +02:00
type Target = T;
fn deref(&self) -> &T {
&self.item
}
}
impl<T> Tagged<T> {
pub fn spanned(self, span: impl Into<Span>) -> Tagged<T> {
Tagged::from_item(
self.item,
Tag {
span: span.into(),
origin: None,
},
)
}
pub fn from_item(item: T, tag: impl Into<Tag>) -> Tagged<T> {
Tagged {
2019-06-11 07:53:04 +02:00
item,
tag: tag.into(),
2019-06-11 07:53:04 +02:00
}
}
pub fn from_simple_spanned_item(item: T, span: impl Into<Span>) -> Tagged<T> {
Tagged::from_item(
item,
Tag {
span: span.into(),
origin: None,
},
)
}
pub fn map<U>(self, input: impl FnOnce(T) -> U) -> Tagged<U> {
let tag = self.tag();
2019-06-11 07:53:04 +02:00
let mapped = input(self.item);
Tagged::from_item(mapped, tag.clone())
2019-06-11 07:53:04 +02:00
}
2019-06-22 03:36:57 +02:00
crate fn copy_span<U>(&self, output: U) -> Tagged<U> {
let span = self.span();
2019-06-22 03:36:57 +02:00
Tagged::from_simple_spanned_item(output, span)
2019-06-22 03:36:57 +02:00
}
2019-06-22 22:46:16 +02:00
pub fn source(&self, source: &Text) -> Text {
Text::from(self.span().slice(source))
2019-06-22 03:36:57 +02:00
}
2019-06-11 07:53:04 +02:00
pub fn span(&self) -> Span {
self.tag.span
}
2019-06-11 07:53:04 +02:00
pub fn tag(&self) -> Tag {
self.tag
}
pub fn origin(&self) -> Option<uuid::Uuid> {
self.tag.origin
}
2019-08-10 22:18:14 +02:00
pub fn origin_name(&self, source_map: &SourceMap) -> Option<String> {
match self.tag.origin.map(|x| source_map.get(&x)) {
Some(Some(SpanSource::File(file))) => Some(file.clone()),
Some(Some(SpanSource::Url(url))) => Some(url.clone()),
_ => None,
}
}
pub fn item(&self) -> &T {
&self.item
2019-07-08 18:44:53 +02:00
}
}
impl<T> From<&Tagged<T>> for Span {
fn from(input: &Tagged<T>) -> Span {
input.span()
}
}
2019-06-22 03:36:57 +02:00
impl From<&Span> for Span {
fn from(input: &Span) -> Span {
*input
}
}
2019-07-16 21:10:25 +02:00
impl From<nom5_locate::LocatedSpan<&str>> for Span {
fn from(input: nom5_locate::LocatedSpan<&str>) -> Span {
2019-06-22 03:36:57 +02:00
Span {
start: input.offset,
end: input.offset + input.fragment.len(),
}
}
}
2019-07-16 21:10:25 +02:00
impl<T> From<(nom5_locate::LocatedSpan<T>, nom5_locate::LocatedSpan<T>)> for Span {
fn from(input: (nom5_locate::LocatedSpan<T>, nom5_locate::LocatedSpan<T>)) -> Span {
2019-06-11 07:53:04 +02:00
Span {
start: input.0.offset,
end: input.1.offset,
}
}
}
impl From<(usize, usize)> for Span {
fn from(input: (usize, usize)) -> Span {
Span {
start: input.0,
end: input.1,
}
}
}
impl From<&std::ops::Range<usize>> for Span {
fn from(input: &std::ops::Range<usize>) -> Span {
Span {
start: input.start,
end: input.end,
}
}
}
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash, Getters,
)]
pub struct Tag {
pub origin: Option<Uuid>,
pub span: Span,
}
impl Tag {
pub fn unknown_origin(span: Span) -> Tag {
Tag { origin: None, span }
}
pub fn unknown() -> Tag {
Tag {
origin: None,
span: Span::unknown(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
pub struct Span {
crate start: usize,
crate end: usize,
}
impl From<Option<Span>> for Span {
fn from(input: Option<Span>) -> Span {
match input {
None => Span { start: 0, end: 0 },
Some(span) => span,
2019-06-11 07:53:04 +02:00
}
}
}
impl Span {
2019-07-08 18:44:53 +02:00
pub fn unknown() -> Span {
Span { start: 0, end: 0 }
}
/*
pub fn unknown_with_uuid(uuid: Uuid) -> Span {
Span {
start: 0,
end: 0,
source: Some(uuid),
}
2019-07-08 18:44:53 +02:00
}
*/
2019-07-08 18:44:53 +02:00
pub fn is_unknown(&self) -> bool {
self.start == 0 && self.end == 0
}
2019-06-22 22:46:16 +02:00
pub fn slice(&self, source: &'a str) -> &'a str {
2019-06-22 03:36:57 +02:00
&source[self.start..self.end]
2019-06-11 07:53:04 +02:00
}
}
impl language_reporting::ReportingSpan for Span {
fn with_start(&self, start: usize) -> Self {
Span {
start,
end: self.end,
}
}
fn with_end(&self, end: usize) -> Self {
Span {
start: self.start,
end,
}
}
fn start(&self) -> usize {
self.start
}
fn end(&self) -> usize {
self.end
}
}