Switch to using Uuid::nil() and fix test

This commit is contained in:
Jonathan Turner 2019-09-18 19:05:33 +12:00
parent 2cf7249794
commit 72e6222992
10 changed files with 54 additions and 48 deletions

View file

@ -113,10 +113,7 @@ fn is_single_origined_text_value(input: &Vec<Tagged<Value>>) -> bool {
if let Tagged { if let Tagged {
item: Value::Primitive(Primitive::String(_)), item: Value::Primitive(Primitive::String(_)),
tag: Tag { tag: Tag { origin, .. },
origin: Some(origin),
..
},
} = input[0] } = input[0]
{ {
origin != uuid::Uuid::nil() origin != uuid::Uuid::nil()

View file

@ -75,10 +75,10 @@ impl PerItemCommand for Enter {
) )
.await.unwrap(); .await.unwrap();
if let Some(uuid) = contents_tag.origin { if contents_tag.origin != uuid::Uuid::nil() {
// If we have loaded something, track its source // If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource( yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid, contents_tag.origin,
span_source, span_source,
)); ));
} }

View file

@ -76,10 +76,10 @@ fn run(
file_extension.or(path_str.split('.').last().map(String::from)) file_extension.or(path_str.split('.').last().map(String::from))
}; };
if let Some(uuid) = contents_tag.origin { if contents_tag.origin != uuid::Uuid::nil() {
// If we have loaded something, track its source // If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource( yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid, contents_tag.origin,
span_source, span_source,
)); ));
} }
@ -158,7 +158,7 @@ pub async fn fetch(
})?), })?),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
@ -173,7 +173,7 @@ pub async fn fetch(
})?), })?),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
@ -190,7 +190,7 @@ pub async fn fetch(
Value::binary(buf), Value::binary(buf),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)) ))
@ -206,7 +206,7 @@ pub async fn fetch(
})?), })?),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
@ -223,7 +223,7 @@ pub async fn fetch(
Value::binary(buf), Value::binary(buf),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)) ))
@ -239,7 +239,7 @@ pub async fn fetch(
})?), })?),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
@ -266,7 +266,7 @@ pub async fn fetch(
})?), })?),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)) ))
@ -276,7 +276,7 @@ pub async fn fetch(
Value::string(format!("Not yet supported MIME type: {} {}", ty, sub_ty)), Value::string(format!("Not yet supported MIME type: {} {}", ty, sub_ty)),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
@ -287,7 +287,7 @@ pub async fn fetch(
Value::string(format!("No content type found")), Value::string(format!("No content type found")),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),

View file

@ -77,10 +77,10 @@ fn run(
file_extension.or(path_str.split('.').last().map(String::from)) file_extension.or(path_str.split('.').last().map(String::from))
}; };
if let Some(uuid) = contents_tag.origin { if contents_tag.origin != uuid::Uuid::nil() {
// If we have loaded something, track its source // If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource( yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid, contents_tag.origin,
span_source, span_source,
)); ));
} }
@ -147,7 +147,7 @@ pub async fn fetch(
Value::string(s), Value::string(s),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
@ -166,7 +166,7 @@ pub async fn fetch(
Value::string(s), Value::string(s),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
@ -175,7 +175,7 @@ pub async fn fetch(
Value::binary(bytes), Value::binary(bytes),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
@ -186,7 +186,7 @@ pub async fn fetch(
Value::binary(bytes), Value::binary(bytes),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)) ))
@ -204,7 +204,7 @@ pub async fn fetch(
Value::string(s), Value::string(s),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
@ -213,7 +213,7 @@ pub async fn fetch(
Value::binary(bytes), Value::binary(bytes),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
@ -224,7 +224,7 @@ pub async fn fetch(
Value::binary(bytes), Value::binary(bytes),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)) ))
@ -235,7 +235,7 @@ pub async fn fetch(
Value::binary(bytes), Value::binary(bytes),
Tag { Tag {
span, span,
origin: Some(Uuid::new_v4()), origin: Uuid::new_v4(),
}, },
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),

View file

@ -85,10 +85,10 @@ fn run(
file_extension.or(path_str.split('.').last().map(String::from)) file_extension.or(path_str.split('.').last().map(String::from))
}; };
if let Some(uuid) = contents_tag.origin { if contents_tag.origin != uuid::Uuid::nil() {
// If we have loaded something, track its source // If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource( yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid, contents_tag.origin,
span_source, span_source,
)); ));
} }

View file

@ -136,7 +136,7 @@ fn save(
// If there is no filename, check the metadata for the origin filename // If there is no filename, check the metadata for the origin filename
if input.len() > 0 { if input.len() > 0 {
let origin = input[0].origin(); let origin = input[0].origin();
match origin.and_then(|x| source_map.get(&x)) { match source_map.get(&origin) {
Some(path) => match path { Some(path) => match path {
SpanSource::File(file) => { SpanSource::File(file) => {
full_path.push(Path::new(file)); full_path.push(Path::new(file));

View file

@ -42,7 +42,7 @@ fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream,
dict.insert("end", Value::int(span.end as i64)); dict.insert("end", Value::int(span.end as i64));
tags.insert_tagged("span", dict.into_tagged_value()); tags.insert_tagged("span", dict.into_tagged_value());
match origin.and_then(|x| source_map.get(&x)) { match source_map.get(&origin) {
Some(SpanSource::File(source)) => { Some(SpanSource::File(source)) => {
tags.insert("origin", Value::string(source)); tags.insert("origin", Value::string(source));
} }

View file

@ -39,7 +39,7 @@ pub trait TaggedItem: Sized {
self, self,
Tag { Tag {
span: Span::unknown(), span: Span::unknown(),
origin: None, origin: uuid::Uuid::nil(),
}, },
) )
} }
@ -90,15 +90,14 @@ impl<T> Tagged<T> {
self.tag.span self.tag.span
} }
// TODO: This should not be optional pub fn origin(&self) -> uuid::Uuid {
pub fn origin(&self) -> Option<uuid::Uuid> {
self.tag.origin self.tag.origin
} }
pub fn origin_name(&self, source_map: &SourceMap) -> Option<String> { pub fn origin_name(&self, source_map: &SourceMap) -> Option<String> {
match self.tag.origin.map(|x| source_map.get(&x)) { match source_map.get(&self.tag.origin) {
Some(Some(SpanSource::File(file))) => Some(file.clone()), Some(SpanSource::File(file)) => Some(file.clone()),
Some(Some(SpanSource::Url(url))) => Some(url.clone()), Some(SpanSource::Url(url)) => Some(url.clone()),
_ => None, _ => None,
} }
} }
@ -168,20 +167,23 @@ impl From<&std::ops::Range<usize>> for Span {
Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash, Getters, Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash, Getters,
)] )]
pub struct Tag { pub struct Tag {
pub origin: Option<Uuid>, pub origin: Uuid,
pub span: Span, pub span: Span,
} }
impl From<Span> for Tag { impl From<Span> for Tag {
fn from(span: Span) -> Self { fn from(span: Span) -> Self {
Tag { origin: None, span } Tag {
origin: uuid::Uuid::nil(),
span,
}
} }
} }
impl From<&Span> for Tag { impl From<&Span> for Tag {
fn from(span: &Span) -> Self { fn from(span: &Span) -> Self {
Tag { Tag {
origin: None, origin: uuid::Uuid::nil(),
span: *span, span: *span,
} }
} }
@ -190,7 +192,7 @@ impl From<&Span> for Tag {
impl From<(usize, usize, Uuid)> for Tag { impl From<(usize, usize, Uuid)> for Tag {
fn from((start, end, origin): (usize, usize, Uuid)) -> Self { fn from((start, end, origin): (usize, usize, Uuid)) -> Self {
Tag { Tag {
origin: Some(origin), origin,
span: Span { start, end }, span: Span { start, end },
} }
} }
@ -199,7 +201,11 @@ impl From<(usize, usize, Uuid)> for Tag {
impl From<(usize, usize, Option<Uuid>)> for Tag { impl From<(usize, usize, Option<Uuid>)> for Tag {
fn from((start, end, origin): (usize, usize, Option<Uuid>)) -> Self { fn from((start, end, origin): (usize, usize, Option<Uuid>)) -> Self {
Tag { Tag {
origin, origin: if let Some(uuid) = origin {
uuid
} else {
uuid::Uuid::nil()
},
span: Span { start, end }, span: Span { start, end },
} }
} }
@ -208,7 +214,7 @@ impl From<(usize, usize, Option<Uuid>)> for Tag {
impl From<nom_locate::LocatedSpanEx<&str, Uuid>> for Tag { impl From<nom_locate::LocatedSpanEx<&str, Uuid>> for Tag {
fn from(input: nom_locate::LocatedSpanEx<&str, Uuid>) -> Tag { fn from(input: nom_locate::LocatedSpanEx<&str, Uuid>) -> Tag {
Tag { Tag {
origin: Some(input.extra), origin: input.extra,
span: Span { span: Span {
start: input.offset, start: input.offset,
end: input.offset + input.fragment.len(), end: input.offset + input.fragment.len(),
@ -231,19 +237,22 @@ impl From<&Tag> for Span {
impl Tag { impl Tag {
pub fn unknown_origin(span: Span) -> Tag { pub fn unknown_origin(span: Span) -> Tag {
Tag { origin: None, span } Tag {
origin: uuid::Uuid::nil(),
span,
}
} }
pub fn unknown_span(origin: Uuid) -> Tag { pub fn unknown_span(origin: Uuid) -> Tag {
Tag { Tag {
origin: Some(origin), origin,
span: Span::unknown(), span: Span::unknown(),
} }
} }
pub fn unknown() -> Tag { pub fn unknown() -> Tag {
Tag { Tag {
origin: None, origin: uuid::Uuid::nil(),
span: Span::unknown(), span: Span::unknown(),
} }
} }

View file

@ -22,7 +22,7 @@ impl language_reporting::ReportingFiles for Files {
} }
fn file_id(&self, tag: Self::Span) -> Self::FileId { fn file_id(&self, tag: Self::Span) -> Self::FileId {
tag.origin.unwrap() tag.origin
} }
fn file_name(&self, _file: Self::FileId) -> FileName { fn file_name(&self, _file: Self::FileId) -> FileName {

View file

@ -219,7 +219,7 @@ fn view_text_value(value: &Tagged<Value>, source_map: &SourceMap) {
let value_origin = value.origin(); let value_origin = value.origin();
match value.item { match value.item {
Value::Primitive(Primitive::String(ref s)) => { Value::Primitive(Primitive::String(ref s)) => {
let source = value_origin.and_then(|x| source_map.get(&x)); let source = source_map.get(&value_origin);
if let Some(source) = source { if let Some(source) = source {
let extension: Option<String> = match source { let extension: Option<String> = match source {