1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use jrsonnet_parser::LocExpr;

pub fn stdlib_expr() -> LocExpr {
	#[cfg(feature = "serialized-stdlib")]
	{
		use bincode::{BincodeRead, DefaultOptions, Options};
		use serde::{Deserialize, Deserializer};

		struct LocDeserializer<R, O: Options> {
			source: Source,
			wrapped: bincode::Deserializer<R, O>,
		}
		macro_rules! delegate {
			($(fn $name:ident($($arg:ident: $ty:ty),*))+) => {$(
				fn $name<V>(mut self $(, $arg: $ty)*, visitor: V) -> Result<V::Value, Self::Error>
				where V: serde::de::Visitor<'de>,
				{
					self.wrapped.$name($($arg,)* visitor)
				}
			)+};
		}
		impl<'de, R, O> Deserializer<'de> for LocDeserializer<R, O>
		where
			R: BincodeRead<'de>,
			O: Options,
		{
			type Error = <&'de mut bincode::Deserializer<R, O> as Deserializer<'de>>::Error;

			delegate! {
				fn deserialize_any()
				fn deserialize_bool()
				fn deserialize_u16()
				fn deserialize_u32()
				fn deserialize_u64()
				fn deserialize_i16()
				fn deserialize_i32()
				fn deserialize_i64()
				fn deserialize_f32()
				fn deserialize_f64()
				fn deserialize_u128()
				fn deserialize_i128()
				fn deserialize_u8()
				fn deserialize_i8()
				fn deserialize_unit()
				fn deserialize_char()
				fn deserialize_str()
				fn deserialize_string()
				fn deserialize_bytes()
				fn deserialize_byte_buf()
				fn deserialize_enum(name: &'static str, variants: &'static [&'static str])
				fn deserialize_tuple(len: usize)
				fn deserialize_option()
				fn deserialize_seq()
				fn deserialize_map()
				fn deserialize_struct(name: &'static str, fields: &'static [&'static str])
				fn deserialize_identifier()
				fn deserialize_newtype_struct(name: &'static str)
				fn deserialize_unit_struct(name: &'static str)
				fn deserialize_tuple_struct(name: &'static str, len: usize)
				fn deserialize_ignored_any()
			}

			fn is_human_readable(&self) -> bool {
				false
			}
		}

		// In build.rs, Source object is populated with empty values, deserializer wrapper loads correct values on deserialize
		let mut deserializer = bincode::Deserializer::from_slice(
			include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")),
			DefaultOptions::new()
				.with_fixint_encoding()
				.allow_trailing_bytes(),
		);

		// Should not panic, stdlib.bincode is generated in build.rs
		LocExpr::deserialize(&mut deserializer).unwrap()
	}

	#[cfg(feature = "codegenerated-stdlib")]
	{
		mod structdump_import {
			pub(super) use std::{option::Option, rc::Rc, vec};

			pub(super) use jrsonnet_parser::*;
		}

		include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))
	}

	#[cfg(not(feature = "codegenerated-stdlib"))]
	{
		use jrsonnet_parser::Source;

		const STDLIB_STR: &str = include_str!("./std.jsonnet");

		jrsonnet_parser::parse(
			STDLIB_STR,
			&jrsonnet_parser::ParserSettings {
				source: Source::new_virtual("<std>".into(), STDLIB_STR.into()),
			},
		)
		.unwrap()
	}
}