git.delta.rocks / jrsonnet / refs/commits / 3738ed2a5354

difftreelog

feat(ir) source url

qkmunwwmYaroslav Bolyukin2026-05-05parent: #ede8518.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -35,14 +35,14 @@
 
 pub use ctx::*;
 pub use dynamic::*;
-pub use error::{Error, ErrorKind::*, Result, ResultExt};
+pub use error::{Error, ErrorKind::*, Result, ResultExt, StackTraceElement};
 pub use evaluate::ensure_sufficient_stack;
 use function::CallLocation;
 pub use import::*;
 use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};
 pub use jrsonnet_interner::{IBytes, IStr};
 use jrsonnet_ir::Expr;
-pub use jrsonnet_ir::{NumValue, Source, SourcePath, Span};
+pub use jrsonnet_ir::{NumValue, Source, SourcePath, SourceUrl, SourceVirtual, Span};
 #[doc(hidden)]
 pub use jrsonnet_macros;
 
@@ -396,9 +396,17 @@
 			file.parsed = Some(
 				parse_jsonnet(&code, file_name.clone())
 					.map(Rc::new)
-					.map_err(|e| ImportSyntaxError {
-						path: file_name.clone(),
-						error: Box::new(e),
+					.map_err(|e| {
+						let span = e.location.clone();
+						let mut err = Error::from(ImportSyntaxError {
+							path: file_name.clone(),
+							error: Box::new(e),
+						});
+						err.trace_mut().0.push(StackTraceElement {
+							location: Some(span),
+							desc: "parse imported".to_string(),
+						});
+						err
 					})?,
 			);
 		}
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -82,18 +82,24 @@
 ) -> Result<(), std::fmt::Error> {
 	if start.line == end.line {
 		if start.column == end.column {
-			write!(out, "{}:{}", start.line, end.column.saturating_sub(1))?;
+			write!(out, "{}:{}", start.line, start.column)?;
 		} else {
-			write!(out, "{}:{}-{}", start.line, start.column - 1, end.column)?;
+			write!(
+				out,
+				"{}:{}-{}",
+				start.line,
+				start.column,
+				end.column.saturating_sub(1)
+			)?;
 		}
 	} else {
 		write!(
 			out,
 			"{}:{}-{}:{}",
-			start.line,
-			end.column.saturating_sub(1),
 			start.line,
-			end.column
+			start.column,
+			end.line,
+			end.column.saturating_sub(1)
 		)?;
 	}
 	Ok(())
@@ -131,22 +137,13 @@
 				|| path.source_path().to_string(),
 				|r| self.resolver.resolve(r),
 			);
-			let mut offset = error.location.1 as usize;
-			let is_eof = if offset >= path.code().len() {
-				offset = path.code().len().saturating_sub(1);
-				true
-			} else {
-				false
-			};
+			let offset = (error.location.1 as usize).min(path.code().len());
 			#[expect(clippy::cast_possible_truncation, reason = "code is limited by 4gb")]
-			let mut location = path
+			let location = path
 				.map_source_locations(&[offset as u32])
 				.into_iter()
 				.next()
 				.unwrap();
-			if is_eof {
-				location.column += 1;
-			}
 
 			write!(n, ":").unwrap();
 			print_code_location(&mut n, &location, &location).unwrap();
modifiedcrates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/lib.rs
+++ b/crates/jrsonnet-formatter/src/lib.rs
@@ -895,10 +895,16 @@
 	}
 }
 
+#[derive(Default)]
 pub struct FormatOptions {
 	// 0 for hard tabs, otherwise number of spaces
 	pub indent: u8,
 }
+impl FormatOptions {
+	pub fn new() -> Self {
+		Self::default()
+	}
+}
 
 #[allow(
 	clippy::result_large_err,
modifiedcrates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -220,10 +220,7 @@
 
 fn ident(p: &mut Parser<'_>) -> Result<IStr> {
 	if !p.at(SyntaxKind::IDENT) {
-		return Err(p.error(format!(
-			"expected identifier, got {}",
-			p.current_desc()
-		)));
+		return Err(p.error(format!("expected identifier, got {}", p.current_desc())));
 	}
 	let text = p.text();
 	p.eat_any();
modifiedcrates/jrsonnet-ir/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir/src/lib.rs
+++ b/crates/jrsonnet-ir/src/lib.rs
@@ -15,7 +15,7 @@
 pub use location::CodeLocation;
 pub use source::{
 	Source, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,
-	SourcePathT, SourceVirtual,
+	SourcePathT, SourceUrl, SourceVirtual,
 };
 
 // It seels to be a wrong place for this kind of stuff, but as it would also be used for static analysis and
modifiedcrates/jrsonnet-ir/src/location.rsdiffbeforeafterboth
--- a/crates/jrsonnet-ir/src/location.rs
+++ b/crates/jrsonnet-ir/src/location.rs
@@ -29,7 +29,7 @@
 		return [CodeLocation::default(); S];
 	}
 	let mut line = 1;
-	let mut column = 1;
+	let mut column = 0;
 	let max_offset = *offsets.iter().max().expect("offsets is not empty");
 
 	let mut offset_map = offsets
@@ -63,7 +63,7 @@
 		}
 		if ch == '\n' {
 			line += 1;
-			column = 1;
+			column = 0;
 
 			for idx in with_no_known_line_ending.drain(..) {
 				out[idx].line_end_offset = pos;
@@ -98,14 +98,14 @@
 				CodeLocation {
 					offset: 0,
 					line: 1,
-					column: 2,
+					column: 1,
 					line_start_offset: 0,
 					line_end_offset: 11,
 				},
 				CodeLocation {
 					offset: 14,
 					line: 2,
-					column: 4,
+					column: 3,
 					line_start_offset: 12,
 					line_end_offset: 67
 				}
modifiedcrates/jrsonnet-ir/src/source.rsdiffbeforeafterboth
before · crates/jrsonnet-ir/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::Acyclic;10use jrsonnet_interner::{IBytes, IStr};1112use crate::location::{CodeLocation, location_to_offset, offset_to_location};1314macro_rules! any_ext_methods {15	($T:ident) => {16		fn as_any(&self) -> &dyn Any;17		fn dyn_hash(&self, hasher: &mut dyn Hasher);18		fn dyn_eq(&self, other: &dyn $T) -> bool;19		fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;20	};21}22macro_rules! any_ext_impl {23	($T:ident) => {24		fn as_any(&self) -> &dyn Any {25			self26		}27		fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {28			self.hash(&mut hasher)29		}30		fn dyn_eq(&self, other: &dyn $T) -> bool {31			let Some(other) = other.as_any().downcast_ref::<Self>() else {32				return false;33			};34			let this = <Self as $T>::as_any(self)35				.downcast_ref::<Self>()36				.expect("restricted by impl");37			this == other38		}39		fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40			<Self as std::fmt::Debug>::fmt(self, fmt)41		}42	};43}44macro_rules! any_ext {45	($T:ident) => {46		impl Hash for dyn $T {47			fn hash<H: Hasher>(&self, state: &mut H) {48				self.dyn_hash(state)49			}50		}51		impl PartialEq for dyn $T {52			fn eq(&self, other: &Self) -> bool {53				self.dyn_eq(other)54			}55		}56		impl Eq for dyn $T {}57	};58}59pub trait SourcePathT: Acyclic + Debug + Display {60	/// This method should be checked by resolver before panicking with bad SourcePath input61	/// if `true` - then resolver may threat this path as default, and default is usally a CWD62	fn is_default(&self) -> bool;63	fn path(&self) -> Option<&Path>;64	any_ext_methods!(SourcePathT);65}66any_ext!(SourcePathT);6768/// Represents location of a file69///70/// Standard CLI only operates using71/// - [`SourceFile`] - for any file72/// - [`SourceDirectory`] - for resolution from CWD73/// - [`SourceVirtual`] - for stdlib/ext-str74/// - [`SourceFifo`] - for /dev/fd/X (This path may appear with `jrsonnet <(command_that_produces_jsonnet)`)75///76/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained77/// from assigned `ImportResolver`78/// However, you should always check `is_default` method return, as it will return true for any paths, where default79/// search location is applicable80///81/// Resolver may also return custom implementations of this trait, for example it may return http url in case of remotely loaded files82#[derive(Eq, Clone, Acyclic)]83pub struct SourcePath(Rc<dyn SourcePathT>);84impl SourcePath {85	pub fn new(inner: impl SourcePathT) -> Self {86		Self(Rc::new(inner))87	}88	pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {89		self.0.as_any().downcast_ref()90	}91	pub fn is_default(&self) -> bool {92		self.0.is_default()93	}94	pub fn path(&self) -> Option<&Path> {95		self.0.path()96	}97}98impl Hash for SourcePath {99	fn hash<H: Hasher>(&self, state: &mut H) {100		self.0.hash(state);101	}102}103impl PartialEq for SourcePath {104	#[allow(clippy::op_ref)]105	fn eq(&self, other: &Self) -> bool {106		&*self.0 == &*other.0107	}108}109impl Display for SourcePath {110	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {111		write!(f, "{}", self.0)112	}113}114impl Debug for SourcePath {115	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {116		write!(f, "{:?}", self.0)117	}118}119impl Default for SourcePath {120	fn default() -> Self {121		Self(Rc::new(SourceDefault))122	}123}124125#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]126struct SourceDefault;127impl Display for SourceDefault {128	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {129		write!(f, "<default>")130	}131}132impl SourcePathT for SourceDefault {133	fn is_default(&self) -> bool {134		true135	}136	fn path(&self) -> Option<&Path> {137		None138	}139	any_ext_impl!(SourcePathT);140}141142#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]143pub struct SourceDefaultIgnoreJpath;144impl Display for SourceDefaultIgnoreJpath {145	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {146		write!(f, "<default (ignoring jpath)>")147	}148}149impl SourcePathT for SourceDefaultIgnoreJpath {150	fn is_default(&self) -> bool {151		true152	}153	fn path(&self) -> Option<&Path> {154		None155	}156	any_ext_impl!(SourcePathT);157}158159/// Represents path to the file on the disk160/// Directories shouldn't be put here, as resolution for files differs from resolution for directories:161///162/// When `file` is being resolved from `SourceFile(a/b/c)`, it should be resolved to `SourceFile(a/b/file)`,163/// however if it is being resolved from `SourceDirectory(a/b/c)`, then it should be resolved to `SourceDirectory(a/b/c/file)`164#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]165pub struct SourceFile(PathBuf);166impl SourceFile {167	pub fn new(path: PathBuf) -> Self {168		Self(path)169	}170	pub fn path(&self) -> &Path {171		&self.0172	}173}174impl Display for SourceFile {175	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {176		write!(f, "{}", self.0.display())177	}178}179impl SourcePathT for SourceFile {180	fn is_default(&self) -> bool {181		false182	}183	fn path(&self) -> Option<&Path> {184		Some(&self.0)185	}186	any_ext_impl!(SourcePathT);187}188189/// Represents path to the directory on the disk190///191/// See also [`SourceFile`]192#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]193pub struct SourceDirectory(PathBuf);194impl SourceDirectory {195	pub fn new(path: PathBuf) -> Self {196		Self(path)197	}198	pub fn path(&self) -> &Path {199		&self.0200	}201}202impl Display for SourceDirectory {203	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {204		write!(f, "{}", self.0.display())205	}206}207impl SourcePathT for SourceDirectory {208	fn is_default(&self) -> bool {209		false210	}211	fn path(&self) -> Option<&Path> {212		Some(&self.0)213	}214	any_ext_impl!(SourcePathT);215}216217/// Represents virtual file, whose are located in memory, and shouldn't be cached218///219/// It is used for --ext-code=.../--tla-code=.../standard library source code by default,220/// and user can construct arbitrary values by hand, without asking import resolver221#[derive(Acyclic, Hash, PartialEq, Eq, Clone)]222pub struct SourceVirtual(pub IStr);223impl Display for SourceVirtual {224	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {225		write!(f, "virtual:{}", self.0)226	}227}228impl fmt::Debug for SourceVirtual {229	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {230		write!(f, "virtual:{}", self.0)231	}232}233impl SourcePathT for SourceVirtual {234	fn is_default(&self) -> bool {235		true236	}237	fn path(&self) -> Option<&Path> {238		None239	}240	any_ext_impl!(SourcePathT);241}242243/// Represents resolved FIFO file, those files may only be read once, and this type is only used for244/// unix, where user might want to do `jrsonnet <(command_that_produces_jsonnet_source)`245/// In most cases, user most probably want to use `jrsonnet -` instead of `jrsonnet /dev/stdin`246/// for better cross-platform support.247// PartialEq is limited to ptr equality248#[allow(clippy::derived_hash_with_manual_eq)]249#[derive(Acyclic, Debug, Hash)]250pub struct SourceFifo(pub String, pub IBytes);251impl PartialEq for SourceFifo {252	fn eq(&self, other: &Self) -> bool {253		std::ptr::eq(self, other)254	}255}256impl fmt::Display for SourceFifo {257	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {258		write!(f, "fifo({:?})", self.0)259	}260}261impl SourcePathT for SourceFifo {262	fn is_default(&self) -> bool {263		// In case of FD input, user won't expect relative paths to be resolved from /dev/fd/264		true265	}266267	fn path(&self) -> Option<&Path> {268		None269	}270271	any_ext_impl!(SourcePathT);272}273274/// Either real file, or virtual275/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut276#[derive(Clone, PartialEq, Eq, Acyclic)]277pub struct Source(pub Rc<(SourcePath, IStr)>);278279impl Source {280	pub fn new(path: SourcePath, code: IStr) -> Self {281		Self(Rc::new((path, code)))282	}283284	pub fn new_virtual(name: IStr, code: IStr) -> Self {285		Self::new(SourcePath::new(SourceVirtual(name)), code)286	}287288	pub fn code(&self) -> &str {289		&self.0.1290	}291292	pub fn source_path(&self) -> &SourcePath {293		&self.0.0294	}295296	pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {297		offset_to_location(&self.0.1, locs)298	}299	pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {300		location_to_offset(&self.0.1, line, column)301	}302}303impl fmt::Debug for Source {304	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {305		write!(f, "{:?}", self.0.0)306	}307}
after · crates/jrsonnet-ir/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::Acyclic;10use jrsonnet_interner::{IBytes, IStr};11use url::Url;1213use crate::location::{CodeLocation, location_to_offset, offset_to_location};1415macro_rules! any_ext_methods {16	($T:ident) => {17		fn as_any(&self) -> &dyn Any;18		fn dyn_hash(&self, hasher: &mut dyn Hasher);19		fn dyn_eq(&self, other: &dyn $T) -> bool;20		fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;21	};22}23macro_rules! any_ext_impl {24	($T:ident) => {25		fn as_any(&self) -> &dyn Any {26			self27		}28		fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {29			self.hash(&mut hasher)30		}31		fn dyn_eq(&self, other: &dyn $T) -> bool {32			let Some(other) = other.as_any().downcast_ref::<Self>() else {33				return false;34			};35			let this = <Self as $T>::as_any(self)36				.downcast_ref::<Self>()37				.expect("restricted by impl");38			this == other39		}40		fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {41			<Self as std::fmt::Debug>::fmt(self, fmt)42		}43	};44}45macro_rules! any_ext {46	($T:ident) => {47		impl Hash for dyn $T {48			fn hash<H: Hasher>(&self, state: &mut H) {49				self.dyn_hash(state)50			}51		}52		impl PartialEq for dyn $T {53			fn eq(&self, other: &Self) -> bool {54				self.dyn_eq(other)55			}56		}57		impl Eq for dyn $T {}58	};59}60pub trait SourcePathT: Acyclic + Debug + Display {61	/// This method should be checked by resolver before panicking with bad SourcePath input62	/// if `true` - then resolver may threat this path as default, and default is usally a CWD63	fn is_default(&self) -> bool;64	fn path(&self) -> Option<&Path>;65	any_ext_methods!(SourcePathT);66}67any_ext!(SourcePathT);6869/// Represents location of a file70///71/// Standard CLI only operates using72/// - [`SourceFile`] - for any file73/// - [`SourceDirectory`] - for resolution from CWD74/// - [`SourceVirtual`] - for stdlib/ext-str75/// - [`SourceFifo`] - for /dev/fd/X (This path may appear with `jrsonnet <(command_that_produces_jsonnet)`)76///77/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained78/// from assigned `ImportResolver`79/// However, you should always check `is_default` method return, as it will return true for any paths, where default80/// search location is applicable81///82/// Resolver may also return custom implementations of this trait, for example it may return http url in case of remotely loaded files83#[derive(Eq, Clone, Acyclic)]84pub struct SourcePath(Rc<dyn SourcePathT>);85impl SourcePath {86	pub fn new(inner: impl SourcePathT) -> Self {87		Self(Rc::new(inner))88	}89	pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {90		self.0.as_any().downcast_ref()91	}92	pub fn is_default(&self) -> bool {93		self.0.is_default()94	}95	pub fn path(&self) -> Option<&Path> {96		self.0.path()97	}98}99impl Hash for SourcePath {100	fn hash<H: Hasher>(&self, state: &mut H) {101		self.0.hash(state);102	}103}104impl PartialEq for SourcePath {105	#[allow(clippy::op_ref)]106	fn eq(&self, other: &Self) -> bool {107		&*self.0 == &*other.0108	}109}110impl Display for SourcePath {111	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {112		write!(f, "{}", self.0)113	}114}115impl Debug for SourcePath {116	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {117		write!(f, "{:?}", self.0)118	}119}120impl Default for SourcePath {121	fn default() -> Self {122		Self(Rc::new(SourceDefault))123	}124}125126#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]127struct SourceDefault;128impl Display for SourceDefault {129	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {130		write!(f, "<default>")131	}132}133impl SourcePathT for SourceDefault {134	fn is_default(&self) -> bool {135		true136	}137	fn path(&self) -> Option<&Path> {138		None139	}140	any_ext_impl!(SourcePathT);141}142143#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]144pub struct SourceDefaultIgnoreJpath;145impl Display for SourceDefaultIgnoreJpath {146	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {147		write!(f, "<default (ignoring jpath)>")148	}149}150impl SourcePathT for SourceDefaultIgnoreJpath {151	fn is_default(&self) -> bool {152		true153	}154	fn path(&self) -> Option<&Path> {155		None156	}157	any_ext_impl!(SourcePathT);158}159160/// Represents path to the file on the disk161/// Directories shouldn't be put here, as resolution for files differs from resolution for directories:162///163/// When `file` is being resolved from `SourceFile(a/b/c)`, it should be resolved to `SourceFile(a/b/file)`,164/// however if it is being resolved from `SourceDirectory(a/b/c)`, then it should be resolved to `SourceDirectory(a/b/c/file)`165#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]166pub struct SourceFile(PathBuf);167impl SourceFile {168	pub fn new(path: PathBuf) -> Self {169		Self(path)170	}171	pub fn path(&self) -> &Path {172		&self.0173	}174}175impl Display for SourceFile {176	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {177		write!(f, "{}", self.0.display())178	}179}180impl SourcePathT for SourceFile {181	fn is_default(&self) -> bool {182		false183	}184	fn path(&self) -> Option<&Path> {185		Some(&self.0)186	}187	any_ext_impl!(SourcePathT);188}189190#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]191pub struct SourceUrl(Url);192impl SourceUrl {193	pub fn new(url: Url) -> Self {194		Self(url)195	}196}197impl Display for SourceUrl {198	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {199		write!(f, "{}", self.0)200	}201}202impl SourcePathT for SourceUrl {203	fn is_default(&self) -> bool {204		false205	}206	fn path(&self) -> Option<&Path> {207		None208	}209	any_ext_impl!(SourcePathT);210}211212/// Represents path to the directory on the disk213///214/// See also [`SourceFile`]215#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]216pub struct SourceDirectory(PathBuf);217impl SourceDirectory {218	pub fn new(path: PathBuf) -> Self {219		Self(path)220	}221	pub fn path(&self) -> &Path {222		&self.0223	}224}225impl Display for SourceDirectory {226	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {227		write!(f, "{}", self.0.display())228	}229}230impl SourcePathT for SourceDirectory {231	fn is_default(&self) -> bool {232		false233	}234	fn path(&self) -> Option<&Path> {235		Some(&self.0)236	}237	any_ext_impl!(SourcePathT);238}239240/// Represents virtual file, whose are located in memory, and shouldn't be cached241///242/// It is used for --ext-code=.../--tla-code=.../standard library source code by default,243/// and user can construct arbitrary values by hand, without asking import resolver244#[derive(Acyclic, Hash, PartialEq, Eq, Clone)]245pub struct SourceVirtual(pub IStr);246impl Display for SourceVirtual {247	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {248		write!(f, "virtual:{}", self.0)249	}250}251impl fmt::Debug for SourceVirtual {252	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {253		write!(f, "virtual:{}", self.0)254	}255}256impl SourcePathT for SourceVirtual {257	fn is_default(&self) -> bool {258		true259	}260	fn path(&self) -> Option<&Path> {261		None262	}263	any_ext_impl!(SourcePathT);264}265266/// Represents resolved FIFO file, those files may only be read once, and this type is only used for267/// unix, where user might want to do `jrsonnet <(command_that_produces_jsonnet_source)`268/// In most cases, user most probably want to use `jrsonnet -` instead of `jrsonnet /dev/stdin`269/// for better cross-platform support.270// PartialEq is limited to ptr equality271#[allow(clippy::derived_hash_with_manual_eq)]272#[derive(Acyclic, Debug, Hash)]273pub struct SourceFifo(pub String, pub IBytes);274impl PartialEq for SourceFifo {275	fn eq(&self, other: &Self) -> bool {276		std::ptr::eq(self, other)277	}278}279impl fmt::Display for SourceFifo {280	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {281		write!(f, "fifo({:?})", self.0)282	}283}284impl SourcePathT for SourceFifo {285	fn is_default(&self) -> bool {286		// In case of FD input, user won't expect relative paths to be resolved from /dev/fd/287		true288	}289290	fn path(&self) -> Option<&Path> {291		None292	}293294	any_ext_impl!(SourcePathT);295}296297/// Either real file, or virtual298/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut299#[derive(Clone, PartialEq, Eq, Acyclic)]300pub struct Source(pub Rc<(SourcePath, IStr)>);301302impl Source {303	pub fn new(path: SourcePath, code: IStr) -> Self {304		Self(Rc::new((path, code)))305	}306307	pub fn new_virtual(name: IStr, code: IStr) -> Self {308		Self::new(SourcePath::new(SourceVirtual(name)), code)309	}310311	pub fn code(&self) -> &str {312		&self.0.1313	}314315	pub fn source_path(&self) -> &SourcePath {316		&self.0.0317	}318319	pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {320		offset_to_location(&self.0.1, locs)321	}322	pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {323		location_to_offset(&self.0.1, line, column)324	}325}326impl fmt::Debug for Source {327	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {328		write!(f, "{:?}", self.0.0)329	}330}