diff --git a/src/commands/classified.rs b/src/commands/classified.rs index eb51f9c4c..7e367b83d 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -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) => { diff --git a/src/commands/command.rs b/src/commands/command.rs index 8bbf2d798..95732abac 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -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), @@ -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"), diff --git a/src/commands/enter.rs b/src/commands/enter.rs index 5c451d3d5..2d96fe865 100644 --- a/src/commands/enter.rs +++ b/src/commands/enter.rs @@ -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, )); } diff --git a/src/commands/fetch.rs b/src/commands/fetch.rs index 25810d6e1..652ec77eb 100644 --- a/src/commands/fetch.rs +++ b/src/commands/fetch.rs @@ -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, Value, Tag, SpanSource), ShellError> { +) -> Result<(Option, 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 = 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 = 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(_) => { diff --git a/src/commands/open.rs b/src/commands/open.rs index d80d8875a..254b0bd7b 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -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, Value, Tag, SpanSource), ShellError> { +) -> Result<(Option, 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()), )), } } diff --git a/src/commands/post.rs b/src/commands/post.rs index c0276fd6a..5533e3065 100644 --- a/src/commands/post.rs +++ b/src/commands/post.rs @@ -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, ®istry, &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, Value, Tag, SpanSource), ShellError> { +) -> Result<(Option, 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 = 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(_) => { diff --git a/src/commands/save.rs b/src/commands/save.rs index f0bf51f8d..47f1a17e9 100644 --- a/src/commands/save.rs +++ b/src/commands/save.rs @@ -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)); } _ => { diff --git a/src/commands/tags.rs b/src/commands/tags.rs index d3eac9126..0cef300b0 100644 --- a/src/commands/tags.rs +++ b/src/commands/tags.rs @@ -43,10 +43,10 @@ fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result { + Some(AnchorLocation::File(source)) => { tags.insert("anchor", Value::string(source)); } - Some(SpanSource::Url(source)) => { + Some(AnchorLocation::Url(source)) => { tags.insert("anchor", Value::string(source)); } _ => {} diff --git a/src/context.rs b/src/context.rs index 57dd1a841..93f4c68b9 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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); +pub struct SourceMap(HashMap); 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 { diff --git a/src/data/meta.rs b/src/data/meta.rs index 4be3a6e1d..0a56198e6 100644 --- a/src/data/meta.rs +++ b/src/data/meta.rs @@ -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 Tagged { pub fn anchor_name(&self, source_map: &SourceMap) -> Option { 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, } } diff --git a/src/lib.rs b/src/lib.rs index b17e6c0b3..e8e09aacd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index d2d7a0aeb..d5488d324 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -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> { 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> { 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> { 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) diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index 2a2e3069f..456c73a78 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -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, source_map: &SourceMap) { if let Some(source) = source { let extension: Option = 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, source_map: &SourceMap) { } } //FIXME: this probably isn't correct - SpanSource::Source(_source) => None, + AnchorLocation::Source(_source) => None, }; match extension { diff --git a/src/prelude.rs b/src/prelude.rs index 56cd21b33..eabd77871 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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;