difftreelog
feat support imports from fifo on unix
in: master
3 files changed
crates/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");
};
crates/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,
crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth1use 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::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///79/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained80/// from assigned `ImportResolver`81/// However, you should always check `is_default` method return, as it will return true for any paths, where default82/// search location is applicable83///84/// Resolver may also return custom implementations of this trait, for example it may return http url in case of remotely loaded files85#[derive(Eq, Debug, Clone)]86pub struct SourcePath(Rc<dyn SourcePathT>);87impl SourcePath {88 pub fn new(inner: impl SourcePathT) -> Self {89 Self(Rc::new(inner))90 }91 pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {92 self.0.as_any().downcast_ref()93 }94 pub fn is_default(&self) -> bool {95 self.0.is_default()96 }97 pub fn path(&self) -> Option<&Path> {98 self.0.path()99 }100}101impl Hash for SourcePath {102 fn hash<H: Hasher>(&self, state: &mut H) {103 self.0.hash(state);104 }105}106impl PartialEq for SourcePath {107 #[allow(clippy::op_ref)]108 fn eq(&self, other: &Self) -> bool {109 &*self.0 == &*other.0110 }111}112impl Trace for SourcePath {113 fn trace(&self, tracer: &mut Tracer) {114 (*self.0).trace(tracer)115 }116117 fn is_type_tracked() -> bool118 where119 Self: Sized,120 {121 true122 }123}124impl Display for SourcePath {125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {126 write!(f, "{}", self.0)127 }128}129impl Default for SourcePath {130 fn default() -> Self {131 Self(Rc::new(SourceDefault))132 }133}134135#[cfg(feature = "structdump")]136impl Codegen for SourcePath {137 fn gen_code(138 &self,139 res: &mut structdump::CodegenResult,140 unique: bool,141 ) -> structdump::TokenStream {142 let source_virtual = self143 .0144 .as_any()145 .downcast_ref::<SourceVirtual>()146 .expect("can only codegen for virtual source paths!")147 .0148 .clone();149 let val = res.add_value(source_virtual, false);150 res.add_code(151 structdump::quote! {152 structdump_import::SourcePath::new(structdump_import::SourceVirtual(#val))153 },154 Some(structdump::quote!(SourcePath)),155 unique,156 )157 }158}159160#[derive(Trace, Hash, PartialEq, Eq, Debug)]161struct SourceDefault;162impl Display for SourceDefault {163 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {164 write!(f, "<default>")165 }166}167impl SourcePathT for SourceDefault {168 fn is_default(&self) -> bool {169 true170 }171 fn path(&self) -> Option<&Path> {172 None173 }174 any_ext_impl!(SourcePathT);175}176177/// Represents path to the file on the disk178/// Directories shouldn't be put here, as resolution for files differs from resolution for directories:179///180/// When `file` is being resolved from `SourceFile(a/b/c)`, it should be resolved to `SourceFile(a/b/file)`,181/// however if it is being resolved from `SourceDirectory(a/b/c)`, then it should be resolved to `SourceDirectory(a/b/c/file)`182#[derive(Trace, Hash, PartialEq, Eq, Debug)]183pub struct SourceFile(PathBuf);184impl SourceFile {185 pub fn new(path: PathBuf) -> Self {186 Self(path)187 }188 pub fn path(&self) -> &Path {189 &self.0190 }191}192impl Display for SourceFile {193 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {194 write!(f, "{}", self.0.display())195 }196}197impl SourcePathT for SourceFile {198 fn is_default(&self) -> bool {199 false200 }201 fn path(&self) -> Option<&Path> {202 Some(&self.0)203 }204 any_ext_impl!(SourcePathT);205}206207/// Represents path to the directory on the disk208///209/// See also [`SourceFile`]210#[derive(Trace, Hash, PartialEq, Eq, Debug)]211pub struct SourceDirectory(PathBuf);212impl SourceDirectory {213 pub fn new(path: PathBuf) -> Self {214 Self(path)215 }216 pub fn path(&self) -> &Path {217 &self.0218 }219}220impl Display for SourceDirectory {221 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {222 write!(f, "{}", self.0.display())223 }224}225impl SourcePathT for SourceDirectory {226 fn is_default(&self) -> bool {227 false228 }229 fn path(&self) -> Option<&Path> {230 Some(&self.0)231 }232 any_ext_impl!(SourcePathT);233}234235/// Represents virtual file, whose are located in memory, and shouldn't be cached236///237/// It is used for --ext-code=.../--tla-code=.../standard library source code by default,238/// and user can construct arbitrary values by hand, without asking import resolver239#[cfg_attr(feature = "structdump", derive(Codegen))]240#[derive(Trace, Hash, PartialEq, Eq, Debug, Clone)]241pub struct SourceVirtual(pub IStr);242impl Display for SourceVirtual {243 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {244 write!(f, "{}", self.0)245 }246}247impl SourcePathT for SourceVirtual {248 fn is_default(&self) -> bool {249 true250 }251 fn path(&self) -> Option<&Path> {252 None253 }254 any_ext_impl!(SourcePathT);255}256257/// Either real file, or virtual258/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut259#[cfg_attr(feature = "structdump", derive(Codegen))]260#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]261#[derive(Clone, PartialEq, Eq, Debug)]262pub struct Source(pub Rc<(SourcePath, IStr)>);263264impl Trace for Source {265 fn trace(&self, _tracer: &mut Tracer) {}266267 fn is_type_tracked() -> bool {268 false269 }270}271272impl Source {273 pub fn new(path: SourcePath, code: IStr) -> Self {274 Self(Rc::new((path, code)))275 }276277 pub fn new_virtual(name: IStr, code: IStr) -> Self {278 Self::new(SourcePath::new(SourceVirtual(name)), code)279 }280281 pub fn code(&self) -> &str {282 &self.0 .1283 }284285 pub fn source_path(&self) -> &SourcePath {286 &self.0 .0287 }288289 pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {290 offset_to_location(&self.0 .1, locs)291 }292 pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {293 location_to_offset(&self.0 .1, line, column)294 }295}