Rename SpanSource to AnchorLocation

This commit is contained in:
Jonathan Turner 2019-09-29 18:18:59 +13:00
parent caed87c125
commit ce947d70b0
14 changed files with 72 additions and 72 deletions

View File

@ -127,8 +127,8 @@ impl InternalCommand {
CommandAction::ChangePath(path) => {
context.shell_manager.set_path(path);
}
CommandAction::AddSpanSource(uuid, span_source) => {
context.add_span_source(uuid, span_source);
CommandAction::AddAnchorLocation(uuid, anchor_location) => {
context.add_anchor_location(uuid, anchor_location);
}
CommandAction::Exit => std::process::exit(0), // TODO: save history.txt
CommandAction::EnterHelpShell(value) => {

View File

@ -1,4 +1,4 @@
use crate::context::{SourceMap, SpanSource};
use crate::context::{AnchorLocation, SourceMap};
use crate::data::Value;
use crate::errors::ShellError;
use crate::evaluate::Scope;
@ -376,7 +376,7 @@ impl EvaluatedCommandArgs {
#[derive(Debug, Serialize, Deserialize)]
pub enum CommandAction {
ChangePath(String),
AddSpanSource(Uuid, SpanSource),
AddAnchorLocation(Uuid, AnchorLocation),
Exit,
EnterShell(String),
EnterValueShell(Tagged<Value>),
@ -390,7 +390,7 @@ impl ToDebug for CommandAction {
fn fmt_debug(&self, f: &mut fmt::Formatter, _source: &str) -> fmt::Result {
match self {
CommandAction::ChangePath(s) => write!(f, "action:change-path={}", s),
CommandAction::AddSpanSource(u, source) => {
CommandAction::AddAnchorLocation(u, source) => {
write!(f, "action:add-span-source={}@{:?}", u, source)
}
CommandAction::Exit => write!(f, "action:exit"),

View File

@ -67,7 +67,7 @@ impl PerItemCommand for Enter {
let full_path = std::path::PathBuf::from(cwd);
let (file_extension, contents, contents_tag, span_source) =
let (file_extension, contents, contents_tag, anchor_location) =
crate::commands::open::fetch(
&full_path,
&location_clone,
@ -77,9 +77,9 @@ impl PerItemCommand for Enter {
if contents_tag.anchor != uuid::Uuid::nil() {
// If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource(
yield ReturnSuccess::action(CommandAction::AddAnchorLocation(
contents_tag.anchor,
span_source,
anchor_location,
));
}

View File

@ -1,5 +1,5 @@
use crate::commands::UnevaluatedCallInfo;
use crate::context::SpanSource;
use crate::context::AnchorLocation;
use crate::data::meta::Span;
use crate::data::Value;
use crate::errors::ShellError;
@ -66,7 +66,7 @@ fn run(
yield Err(e);
return;
}
let (file_extension, contents, contents_tag, span_source) = result.unwrap();
let (file_extension, contents, contents_tag, anchor_location) = result.unwrap();
let file_extension = if has_raw {
None
@ -78,9 +78,9 @@ fn run(
if contents_tag.anchor != uuid::Uuid::nil() {
// If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource(
yield ReturnSuccess::action(CommandAction::AddAnchorLocation(
contents_tag.anchor,
span_source,
anchor_location,
));
}
@ -132,7 +132,7 @@ fn run(
pub async fn fetch(
location: &str,
span: Span,
) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> {
) -> Result<(Option<String>, Value, Tag, AnchorLocation), ShellError> {
if let Err(_) = url::Url::parse(location) {
return Err(ShellError::labeled_error(
"Incomplete or incorrect url",
@ -160,7 +160,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
(mime::APPLICATION, mime::JSON) => Ok((
Some("json".to_string()),
@ -175,7 +175,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
(mime::APPLICATION, mime::OCTET_STREAM) => {
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
@ -192,7 +192,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
))
}
(mime::IMAGE, mime::SVG) => Ok((
@ -208,7 +208,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
(mime::IMAGE, image_ty) => {
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
@ -225,7 +225,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
))
}
(mime::TEXT, mime::HTML) => Ok((
@ -241,7 +241,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
(mime::TEXT, mime::PLAIN) => {
let path_extension = url::Url::parse(location)
@ -268,7 +268,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
))
}
(ty, sub_ty) => Ok((
@ -278,7 +278,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
}
}
@ -289,7 +289,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
},
Err(_) => {

View File

@ -1,5 +1,5 @@
use crate::commands::UnevaluatedCallInfo;
use crate::context::SpanSource;
use crate::context::AnchorLocation;
use crate::data::meta::Span;
use crate::data::Value;
use crate::errors::ShellError;
@ -67,7 +67,7 @@ fn run(
yield Err(e);
return;
}
let (file_extension, contents, contents_tag, span_source) = result.unwrap();
let (file_extension, contents, contents_tag, anchor_location) = result.unwrap();
let file_extension = if has_raw {
None
@ -79,9 +79,9 @@ fn run(
if contents_tag.anchor != uuid::Uuid::nil() {
// If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource(
yield ReturnSuccess::action(CommandAction::AddAnchorLocation(
contents_tag.anchor,
span_source,
anchor_location,
));
}
@ -134,7 +134,7 @@ pub async fn fetch(
cwd: &PathBuf,
location: &str,
span: Span,
) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> {
) -> Result<(Option<String>, Value, Tag, AnchorLocation), ShellError> {
let mut cwd = cwd.clone();
cwd.push(Path::new(location));
@ -149,7 +149,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
AnchorLocation::File(cwd.to_string_lossy().to_string()),
)),
Err(_) => {
//Non utf8 data.
@ -168,7 +168,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
AnchorLocation::File(cwd.to_string_lossy().to_string()),
)),
Err(_) => Ok((
None,
@ -177,7 +177,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
AnchorLocation::File(cwd.to_string_lossy().to_string()),
)),
}
} else {
@ -188,7 +188,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
AnchorLocation::File(cwd.to_string_lossy().to_string()),
))
}
}
@ -206,7 +206,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
AnchorLocation::File(cwd.to_string_lossy().to_string()),
)),
Err(_) => Ok((
None,
@ -215,7 +215,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
AnchorLocation::File(cwd.to_string_lossy().to_string()),
)),
}
} else {
@ -226,7 +226,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
AnchorLocation::File(cwd.to_string_lossy().to_string()),
))
}
}
@ -237,7 +237,7 @@ pub async fn fetch(
span,
anchor: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
AnchorLocation::File(cwd.to_string_lossy().to_string()),
)),
}
}

View File

@ -1,5 +1,5 @@
use crate::commands::UnevaluatedCallInfo;
use crate::context::SpanSource;
use crate::context::AnchorLocation;
use crate::data::Value;
use crate::errors::ShellError;
use crate::parser::hir::SyntaxShape;
@ -74,7 +74,7 @@ fn run(
let raw_args = raw_args.clone();
let stream = async_stream! {
let (file_extension, contents, contents_tag, span_source) =
let (file_extension, contents, contents_tag, anchor_location) =
post(&path_str, &body, user, password, path_span, &registry, &raw_args).await.unwrap();
let file_extension = if has_raw {
@ -87,9 +87,9 @@ fn run(
if contents_tag.anchor != uuid::Uuid::nil() {
// If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource(
yield ReturnSuccess::action(CommandAction::AddAnchorLocation(
contents_tag.anchor,
span_source,
anchor_location,
));
}
@ -146,7 +146,7 @@ pub async fn post(
tag: Tag,
registry: &CommandRegistry,
raw_args: &RawCommandArgs,
) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> {
) -> Result<(Option<String>, Value, Tag, AnchorLocation), ShellError> {
let registry = registry.clone();
let raw_args = raw_args.clone();
if location.starts_with("http:") || location.starts_with("https:") {
@ -248,7 +248,7 @@ pub async fn post(
)
})?),
tag,
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
(mime::APPLICATION, mime::JSON) => Ok((
Some("json".to_string()),
@ -260,7 +260,7 @@ pub async fn post(
)
})?),
tag,
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
(mime::APPLICATION, mime::OCTET_STREAM) => {
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
@ -274,7 +274,7 @@ pub async fn post(
None,
Value::binary(buf),
tag,
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
))
}
(mime::IMAGE, image_ty) => {
@ -289,7 +289,7 @@ pub async fn post(
Some(image_ty.to_string()),
Value::binary(buf),
tag,
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
))
}
(mime::TEXT, mime::HTML) => Ok((
@ -302,7 +302,7 @@ pub async fn post(
)
})?),
tag,
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
(mime::TEXT, mime::PLAIN) => {
let path_extension = url::Url::parse(location)
@ -326,7 +326,7 @@ pub async fn post(
)
})?),
tag,
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
))
}
(ty, sub_ty) => Ok((
@ -336,7 +336,7 @@ pub async fn post(
ty, sub_ty
)),
tag,
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
}
}
@ -344,7 +344,7 @@ pub async fn post(
None,
Value::string(format!("No content type found")),
tag,
SpanSource::Url(location.to_string()),
AnchorLocation::Url(location.to_string()),
)),
},
Err(_) => {

View File

@ -138,7 +138,7 @@ fn save(
let anchor = input[0].anchor();
match source_map.get(&anchor) {
Some(path) => match path {
SpanSource::File(file) => {
AnchorLocation::File(file) => {
full_path.push(Path::new(file));
}
_ => {

View File

@ -43,10 +43,10 @@ fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream,
tags.insert_tagged("span", dict.into_tagged_value());
match source_map.get(&anchor) {
Some(SpanSource::File(source)) => {
Some(AnchorLocation::File(source)) => {
tags.insert("anchor", Value::string(source));
}
Some(SpanSource::Url(source)) => {
Some(AnchorLocation::Url(source)) => {
tags.insert("anchor", Value::string(source));
}
_ => {}

View File

@ -11,21 +11,21 @@ use std::sync::Arc;
use uuid::Uuid;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SpanSource {
pub enum AnchorLocation {
Url(String),
File(String),
Source(Text),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SourceMap(HashMap<Uuid, SpanSource>);
pub struct SourceMap(HashMap<Uuid, AnchorLocation>);
impl SourceMap {
pub fn insert(&mut self, uuid: Uuid, span_source: SpanSource) {
self.0.insert(uuid, span_source);
pub fn insert(&mut self, uuid: Uuid, anchor_location: AnchorLocation) {
self.0.insert(uuid, anchor_location);
}
pub fn get(&self, uuid: &Uuid) -> Option<&SpanSource> {
pub fn get(&self, uuid: &Uuid) -> Option<&AnchorLocation> {
self.0.get(uuid)
}
@ -105,8 +105,8 @@ impl Context {
}
}
pub fn add_span_source(&mut self, uuid: Uuid, span_source: SpanSource) {
self.source_map.insert(uuid, span_source);
pub fn add_anchor_location(&mut self, uuid: Uuid, anchor_location: AnchorLocation) {
self.source_map.insert(uuid, anchor_location);
}
pub(crate) fn has_command(&self, name: &str) -> bool {

View File

@ -1,4 +1,4 @@
use crate::context::{SourceMap, SpanSource};
use crate::context::{AnchorLocation, SourceMap};
use crate::prelude::*;
use crate::Text;
use derive_new::new;
@ -96,8 +96,8 @@ impl<T> Tagged<T> {
pub fn anchor_name(&self, source_map: &SourceMap) -> Option<String> {
match source_map.get(&self.tag.anchor) {
Some(SpanSource::File(file)) => Some(file.clone()),
Some(SpanSource::Url(url)) => Some(url.clone()),
Some(AnchorLocation::File(file)) => Some(file.clone()),
Some(AnchorLocation::Url(url)) => Some(url.clone()),
_ => None,
}
}

View File

@ -21,7 +21,7 @@ mod traits;
mod utils;
pub use crate::commands::command::{CallInfo, ReturnSuccess, ReturnValue};
pub use crate::context::{SourceMap, SpanSource};
pub use crate::context::{AnchorLocation, SourceMap};
pub use crate::env::host::BasicHost;
pub use crate::parser::hir::SyntaxShape;
pub use crate::parser::parse::token_tree_builder::TokenTreeBuilder;

View File

@ -1,6 +1,6 @@
use crossterm::{cursor, terminal, Attribute, RawScreen};
use nu::{
serve_plugin, CallInfo, Plugin, Primitive, ShellError, Signature, SpanSource, Tagged, Value,
serve_plugin, AnchorLocation, CallInfo, Plugin, Primitive, ShellError, Signature, Tagged, Value,
};
use pretty_hex::*;
@ -35,7 +35,7 @@ impl Plugin for BinaryView {
fn view_binary(
b: &[u8],
source: Option<&SpanSource>,
source: Option<&AnchorLocation>,
lores_mode: bool,
) -> Result<(), Box<dyn std::error::Error>> {
if b.len() > 3 {
@ -254,7 +254,7 @@ fn load_from_jpg_buffer(buffer: &[u8]) -> Option<(RawImageBuffer)> {
pub fn view_contents(
buffer: &[u8],
_source: Option<&SpanSource>,
_source: Option<&AnchorLocation>,
lores_mode: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let mut raw_image_buffer = load_from_png_buffer(buffer);
@ -341,12 +341,12 @@ pub fn view_contents(
#[cfg(feature = "rawkey")]
pub fn view_contents_interactive(
buffer: &[u8],
source: Option<&SpanSource>,
source: Option<&AnchorLocation>,
lores_mode: bool,
) -> Result<(), Box<dyn std::error::Error>> {
use rawkey::{KeyCode, RawKey};
let sav_path = if let Some(SpanSource::File(f)) = source {
let sav_path = if let Some(AnchorLocation::File(f)) = source {
let mut path = std::path::PathBuf::from(f);
path.set_extension("sav");
Some(path)

View File

@ -1,7 +1,7 @@
use crossterm::{cursor, terminal, RawScreen};
use crossterm::{InputEvent, KeyEvent};
use nu::{
serve_plugin, CallInfo, Plugin, Primitive, ShellError, Signature, SourceMap, SpanSource,
serve_plugin, AnchorLocation, CallInfo, Plugin, Primitive, ShellError, Signature, SourceMap,
Tagged, Value,
};
@ -223,11 +223,11 @@ fn view_text_value(value: &Tagged<Value>, source_map: &SourceMap) {
if let Some(source) = source {
let extension: Option<String> = match source {
SpanSource::File(file) => {
AnchorLocation::File(file) => {
let path = Path::new(file);
path.extension().map(|x| x.to_string_lossy().to_string())
}
SpanSource::Url(url) => {
AnchorLocation::Url(url) => {
let url = url::Url::parse(url);
if let Ok(url) = url {
let url = url.clone();
@ -246,7 +246,7 @@ fn view_text_value(value: &Tagged<Value>, source_map: &SourceMap) {
}
}
//FIXME: this probably isn't correct
SpanSource::Source(_source) => None,
AnchorLocation::Source(_source) => None,
};
match extension {

View File

@ -54,7 +54,7 @@ pub(crate) use crate::commands::command::{
pub(crate) use crate::commands::PerItemCommand;
pub(crate) use crate::commands::RawCommandArgs;
pub(crate) use crate::context::CommandRegistry;
pub(crate) use crate::context::{Context, SpanSource};
pub(crate) use crate::context::{AnchorLocation, Context};
pub(crate) use crate::data::base as value;
pub(crate) use crate::data::meta::{Tag, Tagged, TaggedItem};
pub(crate) use crate::data::types::ExtractType;