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

difftreelog

source

crates/jrsonnet-ir/src/function.rs2.7 KiBsourcehistory
1use std::{fmt, ops::Deref, rc::Rc};23use jrsonnet_gcmodule::Acyclic;4use jrsonnet_interner::IStr;56#[derive(Clone, Acyclic, Debug, PartialEq, Eq)]7pub enum ParamName {8	Unnamed,9	Named(IStr),10}11impl ParamName {12	pub fn as_str(&self) -> Option<&str> {13		match self {14			ParamName::Unnamed => None,15			ParamName::Named(istr) => Some(istr),16		}17	}18	pub fn is_anonymous(&self) -> bool {19		matches!(self, Self::Unnamed)20	}21	pub fn is_named(&self) -> bool {22		matches!(self, Self::Named(_))23	}24}25impl PartialEq<IStr> for ParamName {26	fn eq(&self, other: &IStr) -> bool {27		match self {28			ParamName::Unnamed => false,29			ParamName::Named(istr) => istr == other,30		}31	}32}3334impl fmt::Display for ParamName {35	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {36		match &self {37			Self::Named(v) => write!(f, "{v}"),38			Self::Unnamed => write!(f, "<unnamed>"),39		}40	}41}4243#[derive(Clone, Copy, Debug, Acyclic, PartialEq, Eq)]44pub enum ParamDefault {45	None,46	Exists,47	Literal(&'static str),48}49impl ParamDefault {50	pub const fn exists(is_exists: bool) -> Self {51		if is_exists { Self::Exists } else { Self::None }52	}53}54impl fmt::Display for ParamDefault {55	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {56		match self {57			ParamDefault::None => Ok(()),58			ParamDefault::Exists => write!(f, " = <default>"),59			ParamDefault::Literal(lit) => write!(f, " = {lit}"),60		}61	}62}6364#[derive(Clone, Acyclic, Debug, PartialEq, Eq)]65pub struct ParamParse {66	name: ParamName,67	default: ParamDefault,68}69impl ParamParse {70	pub fn new(name: ParamName, default: ParamDefault) -> Self {71		Self { name, default }72	}73	/// Parameter name for named call parsing74	pub fn name(&self) -> &ParamName {75		&self.name76	}77	pub fn default(&self) -> ParamDefault {78		self.default79	}80	pub fn has_default(&self) -> bool {81		!matches!(self.default, ParamDefault::None)82	}83}84impl fmt::Display for ParamParse {85	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {86		write!(f, "{}{}", self.name, self.default)87	}88}8990#[derive(Debug, Clone, Acyclic, PartialEq, Eq)]91pub struct FunctionSignature(Rc<[ParamParse]>);92impl Deref for FunctionSignature {93	type Target = [ParamParse];9495	fn deref(&self) -> &Self::Target {96		&self.097	}98}99100thread_local! {101	static EMPTY_SIGNATURE: FunctionSignature = FunctionSignature::new([].into());102}103104impl FunctionSignature {105	pub fn new(v: Rc<[ParamParse]>) -> Self {106		Self(v)107	}108	pub fn empty() -> Self {109		EMPTY_SIGNATURE.with(|p| p.clone())110	}111}112impl fmt::Display for FunctionSignature {113	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {114		if self.0.is_empty() {115			return write!(f, "(/*no arguments*/)");116		}117		write!(f, "(")?;118		for (i, par) in self.0.iter().enumerate() {119			if i != 0 {120				write!(f, ", ")?;121			}122			write!(f, "{par}")?;123		}124		write!(f, ")")125	}126}