git.delta.rocks / jrsonnet / refs/commits / 0e2224254bcf

difftreelog

source

crates/jrsonnet-stdlib/src/expr.rs3.1 KiBsourcehistory
1use jrsonnet_parser::LocExpr;23pub fn stdlib_expr() -> LocExpr {4	#[cfg(all(feature = "serialized-stdlib", feature = "codegenerated-stdlib"))]5	compile_error!(6		"features `serialized-stdlib` and `codegenerated-stdlib` are mutually exclusive"7	);8	#[cfg(all(feature = "serialized-stdlib", not(feature = "codegenerated-stdlib")))]9	{10		use bincode::{BincodeRead, DefaultOptions, Options};11		use serde::{Deserialize, Deserializer};1213		struct LocDeserializer<R, O: Options> {14			source: Source,15			wrapped: bincode::Deserializer<R, O>,16		}17		macro_rules! delegate {18			($(fn $name:ident($($arg:ident: $ty:ty),*))+) => {$(19				fn $name<V>(mut self $(, $arg: $ty)*, visitor: V) -> Result<V::Value, Self::Error>20				where V: serde::de::Visitor<'de>,21				{22					self.wrapped.$name($($arg,)* visitor)23				}24			)+};25		}26		impl<'de, R, O> Deserializer<'de> for LocDeserializer<R, O>27		where28			R: BincodeRead<'de>,29			O: Options,30		{31			type Error = <&'de mut bincode::Deserializer<R, O> as Deserializer<'de>>::Error;3233			delegate! {34				fn deserialize_any()35				fn deserialize_bool()36				fn deserialize_u16()37				fn deserialize_u32()38				fn deserialize_u64()39				fn deserialize_i16()40				fn deserialize_i32()41				fn deserialize_i64()42				fn deserialize_f32()43				fn deserialize_f64()44				fn deserialize_u128()45				fn deserialize_i128()46				fn deserialize_u8()47				fn deserialize_i8()48				fn deserialize_unit()49				fn deserialize_char()50				fn deserialize_str()51				fn deserialize_string()52				fn deserialize_bytes()53				fn deserialize_byte_buf()54				fn deserialize_enum(name: &'static str, variants: &'static [&'static str])55				fn deserialize_tuple(len: usize)56				fn deserialize_option()57				fn deserialize_seq()58				fn deserialize_map()59				fn deserialize_struct(name: &'static str, fields: &'static [&'static str])60				fn deserialize_identifier()61				fn deserialize_newtype_struct(name: &'static str)62				fn deserialize_unit_struct(name: &'static str)63				fn deserialize_tuple_struct(name: &'static str, len: usize)64				fn deserialize_ignored_any()65			}6667			fn is_human_readable(&self) -> bool {68				false69			}70		}7172		// In build.rs, Source object is populated with empty values, deserializer wrapper loads correct values on deserialize73		let mut deserializer = bincode::Deserializer::from_slice(74			include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")),75			DefaultOptions::new()76				.with_fixint_encoding()77				.allow_trailing_bytes(),78		);7980		// Should not panic, stdlib.bincode is generated in build.rs81		LocExpr::deserialize(&mut deserializer).unwrap()82	}8384	#[cfg(all(feature = "codegenerated-stdlib", not(feature = "serialized-stdlib")))]85	{86		mod structdump_import {87			pub(super) use std::{option::Option, rc::Rc, vec};8889			pub(super) use jrsonnet_parser::*;90		}9192		include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))93	}9495	#[cfg(not(any(feature = "serialized-stdlib", feature = "codegenerated-stdlib")))]96	{97		use jrsonnet_parser::Source;9899		const STDLIB_STR: &str = include_str!("./std.jsonnet");100101		jrsonnet_parser::parse(102			STDLIB_STR,103			&jrsonnet_parser::ParserSettings {104				source: Source::new_virtual("<std>".into(), STDLIB_STR.into()),105			},106		)107		.unwrap()108	}109}