git.delta.rocks / jrsonnet / refs/commits / bfcd43bbf560

difftreelog

feat support imports from fifo on unix

Yaroslav Bolyukin2024-03-17parent: #d78cbb7.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -9,7 +9,8 @@
 
 use fs::File;
 use jrsonnet_gcmodule::Trace;
-use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
+use jrsonnet_interner::IBytes;
+use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath, SourceFifo};
 
 use crate::{
 	bail,
@@ -82,6 +83,37 @@
 	}
 }
 
+/// Create `SourcePath` from path, handling directories/Fifo files (on unix)/etc
+fn check_path(path: &Path) -> Result<Option<SourcePath>> {
+	let meta = match fs::metadata(path) {
+		Ok(v) => v,
+		Err(e) if e.kind() == ErrorKind::NotFound => {
+			return Ok(None);
+		}
+		Err(e) => bail!(ImportIo(e.to_string())),
+	};
+	let ty = meta.file_type();
+	if ty.is_file() {
+		return Ok(Some(SourcePath::new(SourceFile::new(
+			path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
+		))));
+	}
+	let ty = meta.file_type();
+	#[cfg(unix)]
+	{
+		use std::os::unix::fs::FileTypeExt;
+		if ty.is_fifo() {
+			let file = fs::read(path).map_err(|e| ImportIo(format!("FIFO read failed: {e}")))?;
+			return Ok(Some(SourcePath::new(SourceFifo(
+				format!("{}", path.display()),
+				IBytes::from(file.as_slice()),
+			))));
+		}
+	}
+	// Block device/some other magic thing.
+	Err(RuntimeError("special file can't be imported".into()).into())
+}
+
 impl ImportResolver for FileImportResolver {
 	fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
 		let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
@@ -95,50 +127,34 @@
 		} else {
 			unreachable!("resolver can't return this path")
 		};
+
 		direct.push(path);
-		if direct.is_file() {
-			Ok(SourcePath::new(SourceFile::new(
-				direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-			)))
-		} else {
-			for library_path in self.library_paths.borrow().iter() {
-				let mut cloned = library_path.clone();
-				cloned.push(path);
-				if cloned.exists() {
-					return Ok(SourcePath::new(SourceFile::new(
-						cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-					)));
-				}
+		if let Some(direct) = check_path(&direct)? {
+			return Ok(direct);
+		}
+		for library_path in self.library_paths.borrow().iter() {
+			let mut cloned = library_path.clone();
+			cloned.push(path);
+			if let Some(cloned) = check_path(&cloned)? {
+				return Ok(cloned);
 			}
-			bail!(ImportFileNotFound(from.clone(), path.to_owned()))
 		}
+		bail!(ImportFileNotFound(from.clone(), path.to_owned()))
 	}
 	fn resolve(&self, path: &Path) -> Result<SourcePath> {
-		let meta = match fs::metadata(path) {
-			Ok(v) => v,
-			Err(e) if e.kind() == ErrorKind::NotFound => {
-				bail!(AbsoluteImportFileNotFound(path.to_owned()))
-			}
-			Err(e) => bail!(ImportIo(e.to_string())),
+		let Some(source) = check_path(path)? else {
+			bail!(AbsoluteImportFileNotFound(path.to_owned()))
 		};
-		if meta.is_file() {
-			Ok(SourcePath::new(SourceFile::new(
-				path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-			)))
-		} else if meta.is_dir() {
-			Ok(SourcePath::new(SourceDirectory::new(
-				path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-			)))
-		} else {
-			unreachable!("this can't be a symlink")
-		}
+		Ok(source)
 	}
 
 	fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
 		let path = if let Some(f) = id.downcast_ref::<SourceFile>() {
 			f.path()
-		} else if id.downcast_ref::<SourceDirectory>().is_some() || id.is_default() {
+		} else if id.downcast_ref::<SourceDirectory>().is_some() {
 			bail!(ImportIsADirectory(id.clone()))
+		} else if let Some(f) = id.downcast_ref::<SourceFifo>() {
+			return Ok(f.1.to_vec());
 		} else {
 			unreachable!("other types are not supported in resolve");
 		};
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -11,7 +11,9 @@
 mod source;
 mod unescape;
 pub use location::CodeLocation;
-pub use source::{Source, SourceDirectory, SourceFile, SourcePath, SourcePathT, SourceVirtual};
+pub use source::{
+	Source, SourceDirectory, SourceFifo, SourceFile, SourcePath, SourcePathT, SourceVirtual,
+};
 
 pub struct ParserSettings {
 	pub source: Source,
modifiedcrates/jrsonnet-parser/src/source.rsdiffbeforeafterboth
after · crates/jrsonnet-parser/src/source.rs
1use std::{2	any::Any,3	fmt::{self, Debug, Display},4	hash::{Hash, Hasher},5	path::{Path, PathBuf},6	rc::Rc,7};89use jrsonnet_gcmodule::{Trace, Tracer};10use jrsonnet_interner::{IBytes, IStr};11#[cfg(feature = "serde")]12use serde::{Deserialize, Serialize};13#[cfg(feature = "structdump")]14use structdump::Codegen;1516use crate::location::{location_to_offset, offset_to_location, CodeLocation};1718macro_rules! any_ext_methods {19	($T:ident) => {20		fn as_any(&self) -> &dyn Any;21		fn dyn_hash(&self, hasher: &mut dyn Hasher);22		fn dyn_eq(&self, other: &dyn $T) -> bool;23		fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;24	};25}26macro_rules! any_ext_impl {27	($T:ident) => {28		fn as_any(&self) -> &dyn Any {29			self30		}31		fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {32			self.hash(&mut hasher)33		}34		fn dyn_eq(&self, other: &dyn $T) -> bool {35			let Some(other) = other.as_any().downcast_ref::<Self>() else {36				return false;37			};38			let this = <Self as $T>::as_any(self)39				.downcast_ref::<Self>()40				.expect("restricted by impl");41			this == other42		}43		fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {44			<Self as std::fmt::Debug>::fmt(self, fmt)45		}46	};47}48macro_rules! any_ext {49	($T:ident) => {50		impl Hash for dyn $T {51			fn hash<H: Hasher>(&self, state: &mut H) {52				self.dyn_hash(state)53			}54		}55		impl PartialEq for dyn $T {56			fn eq(&self, other: &Self) -> bool {57				self.dyn_eq(other)58			}59		}60		impl Eq for dyn $T {}61	};62}63pub trait SourcePathT: Trace + Debug + Display {64	/// This method should be checked by resolver before panicking with bad SourcePath input65	/// if `true` - then resolver may threat this path as default, and default is usally a CWD66	fn is_default(&self) -> bool;67	fn path(&self) -> Option<&Path>;68	any_ext_methods!(SourcePathT);69}70any_ext!(SourcePathT);7172/// Represents location of a file73///74/// Standard CLI only operates using75/// - [`SourceFile`] - for any file76/// - [`SourceDirectory`] - for resolution from CWD77/// - [`SourceVirtual`] - for stdlib/ext-str78/// - [`SourceFifo`] - for /dev/fd/X (This path may appear with `jrsonnet <(command_that_produces_jsonnet)`)79///80/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained81/// from assigned `ImportResolver`82/// However, you should always check `is_default` method return, as it will return true for any paths, where default83/// search location is applicable84///85/// Resolver may also return custom implementations of this trait, for example it may return http url in case of remotely loaded files86#[derive(Eq, Debug, Clone)]87pub struct SourcePath(Rc<dyn SourcePathT>);88impl SourcePath {89	pub fn new(inner: impl SourcePathT) -> Self {90		Self(Rc::new(inner))91	}92	pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {93		self.0.as_any().downcast_ref()94	}95	pub fn is_default(&self) -> bool {96		self.0.is_default()97	}98	pub fn path(&self) -> Option<&Path> {99		self.0.path()100	}101}102impl Hash for SourcePath {103	fn hash<H: Hasher>(&self, state: &mut H) {104		self.0.hash(state);105	}106}107impl PartialEq for SourcePath {108	#[allow(clippy::op_ref)]109	fn eq(&self, other: &Self) -> bool {110		&*self.0 == &*other.0111	}112}113impl Trace for SourcePath {114	fn trace(&self, tracer: &mut Tracer) {115		(*self.0).trace(tracer)116	}117118	fn is_type_tracked() -> bool119	where120		Self: Sized,121	{122		true123	}124}125impl Display for SourcePath {126	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {127		write!(f, "{}", self.0)128	}129}130impl Default for SourcePath {131	fn default() -> Self {132		Self(Rc::new(SourceDefault))133	}134}135136#[cfg(feature = "structdump")]137impl Codegen for SourcePath {138	fn gen_code(139		&self,140		res: &mut structdump::CodegenResult,141		unique: bool,142	) -> structdump::TokenStream {143		let source_virtual = self144			.0145			.as_any()146			.downcast_ref::<SourceVirtual>()147			.expect("can only codegen for virtual source paths!")148			.0149			.clone();150		let val = res.add_value(source_virtual, false);151		res.add_code(152			structdump::quote! {153				structdump_import::SourcePath::new(structdump_import::SourceVirtual(#val))154			},155			Some(structdump::quote!(SourcePath)),156			unique,157		)158	}159}160161#[derive(Trace, Hash, PartialEq, Eq, Debug)]162struct SourceDefault;163impl Display for SourceDefault {164	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {165		write!(f, "<default>")166	}167}168impl SourcePathT for SourceDefault {169	fn is_default(&self) -> bool {170		true171	}172	fn path(&self) -> Option<&Path> {173		None174	}175	any_ext_impl!(SourcePathT);176}177178/// Represents path to the file on the disk179/// Directories shouldn't be put here, as resolution for files differs from resolution for directories:180///181/// When `file` is being resolved from `SourceFile(a/b/c)`, it should be resolved to `SourceFile(a/b/file)`,182/// however if it is being resolved from `SourceDirectory(a/b/c)`, then it should be resolved to `SourceDirectory(a/b/c/file)`183#[derive(Trace, Hash, PartialEq, Eq, Debug)]184pub struct SourceFile(PathBuf);185impl SourceFile {186	pub fn new(path: PathBuf) -> Self {187		Self(path)188	}189	pub fn path(&self) -> &Path {190		&self.0191	}192}193impl Display for SourceFile {194	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {195		write!(f, "{}", self.0.display())196	}197}198impl SourcePathT for SourceFile {199	fn is_default(&self) -> bool {200		false201	}202	fn path(&self) -> Option<&Path> {203		Some(&self.0)204	}205	any_ext_impl!(SourcePathT);206}207208/// Represents path to the directory on the disk209///210/// See also [`SourceFile`]211#[derive(Trace, Hash, PartialEq, Eq, Debug)]212pub struct SourceDirectory(PathBuf);213impl SourceDirectory {214	pub fn new(path: PathBuf) -> Self {215		Self(path)216	}217	pub fn path(&self) -> &Path {218		&self.0219	}220}221impl Display for SourceDirectory {222	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {223		write!(f, "{}", self.0.display())224	}225}226impl SourcePathT for SourceDirectory {227	fn is_default(&self) -> bool {228		false229	}230	fn path(&self) -> Option<&Path> {231		Some(&self.0)232	}233	any_ext_impl!(SourcePathT);234}235236/// Represents virtual file, whose are located in memory, and shouldn't be cached237///238/// It is used for --ext-code=.../--tla-code=.../standard library source code by default,239/// and user can construct arbitrary values by hand, without asking import resolver240#[cfg_attr(feature = "structdump", derive(Codegen))]241#[derive(Trace, Hash, PartialEq, Eq, Debug, Clone)]242pub struct SourceVirtual(pub IStr);243impl Display for SourceVirtual {244	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {245		write!(f, "{}", self.0)246	}247}248impl SourcePathT for SourceVirtual {249	fn is_default(&self) -> bool {250		true251	}252	fn path(&self) -> Option<&Path> {253		None254	}255	any_ext_impl!(SourcePathT);256}257258/// Represents resolved FIFO file, those files may only be read once, and this type is only used for259/// unix, where user might want to do `jrsonnet <(command_that_produces_jsonnet_source)`260/// In most cases, user most probably want to use `jrsonnet -` instead of `jrsonnet /dev/stdin`261/// for better cross-platform support.262// PartialEq is limited to ptr equality263#[allow(clippy::derived_hash_with_manual_eq)]264#[derive(Trace, Debug, Hash)]265pub struct SourceFifo(pub String, pub IBytes);266impl PartialEq for SourceFifo {267	fn eq(&self, other: &Self) -> bool {268		std::ptr::eq(self, other)269	}270}271impl fmt::Display for SourceFifo {272	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {273		write!(f, "fifo({:?})", self.0)274	}275}276impl SourcePathT for SourceFifo {277	fn is_default(&self) -> bool {278		// In case of FD input, user won't expect relative paths to be resolved from /dev/fd/279		true280	}281282	fn path(&self) -> Option<&Path> {283		None284	}285286	any_ext_impl!(SourcePathT);287}288289/// Either real file, or virtual290/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut291#[cfg_attr(feature = "structdump", derive(Codegen))]292#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]293#[derive(Clone, PartialEq, Eq, Debug)]294pub struct Source(pub Rc<(SourcePath, IStr)>);295296impl Trace for Source {297	fn trace(&self, _tracer: &mut Tracer) {}298299	fn is_type_tracked() -> bool {300		false301	}302}303304impl Source {305	pub fn new(path: SourcePath, code: IStr) -> Self {306		Self(Rc::new((path, code)))307	}308309	pub fn new_virtual(name: IStr, code: IStr) -> Self {310		Self::new(SourcePath::new(SourceVirtual(name)), code)311	}312313	pub fn code(&self) -> &str {314		&self.0 .1315	}316317	pub fn source_path(&self) -> &SourcePath {318		&self.0 .0319	}320321	pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {322		offset_to_location(&self.0 .1, locs)323	}324	pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {325		location_to_offset(&self.0 .1, line, column)326	}327}