build_info_proc/
lib.rs

1#![forbid(unsafe_code)]
2
3use std::io::Cursor;
4
5use base64::read::DecoderReader as Base64Decoder;
6use build_info_common::{BuildInfo, VersionedString};
7use proc_macro::TokenStream;
8use proc_macro_error2::{abort_call_site, emit_call_site_error, proc_macro_error};
9
10mod format;
11#[cfg(feature = "runtime")]
12mod function;
13
14/**
15Call as `build_info!(fn name)` to create a function called `name` that returns a reference to a lazily created
16and cached `BuildInfo` object.
17
18This macro also accepts a visibility specifier for the generated function, such as `build_info!(pub fn version)`.
19*/
20#[cfg(feature = "runtime")]
21#[proc_macro_error]
22#[proc_macro]
23pub fn build_info(input: TokenStream) -> TokenStream {
24	function::build_info(input, deserialize_build_info())
25}
26
27#[proc_macro_error]
28#[proc_macro]
29pub fn format(input: TokenStream) -> TokenStream {
30	format::format(input, deserialize_build_info())
31}
32
33fn deserialize_build_info() -> BuildInfo {
34	// explicitly pull std::format into this namespace, as `abort_call_site` seems to use the macro without properly
35	// qualifying it.
36	use std::format;
37
38	let data = std::env::var("BUILD_INFO").unwrap_or_else(|err| {
39		abort_call_site!("No BuildInfo data found!";
40			note = "Did you call build_info_build::build_script() in your build.rs?";
41			note = "This crate expects version {} of the BuildInfo data", build_info_common::crate_version();
42			note = "Caused by: {}", err;
43		)
44	});
45
46	// println!("Serialized data is {} bytes long.", data.len());
47
48	let versioned: VersionedString = serde_json::from_str(&data).unwrap_or_else(|err| {
49		abort_call_site!("Could not deserialize BuildInfo data at all!";
50			note = "This crate expects version {} of the BuildInfo data", build_info_common::crate_version();
51			note = "Caused by: {}", err;
52		)
53	});
54
55	if !versioned.check() {
56		// TODO: This should really be a warning - but warnings are currently nightly-only...
57		emit_call_site_error!("BuildInfo data has an different version!";
58			note = "The serialized data has version {}", versioned.version;
59			note = "This crate expects version {} of the BuildInfo data", build_info_common::crate_version();
60		);
61	}
62
63	let mut cursor = Cursor::new(versioned.string.as_bytes());
64	const BASE64_ENGINE: base64::engine::GeneralPurpose = base64::engine::GeneralPurpose::new(
65		&base64::alphabet::STANDARD,
66		base64::engine::GeneralPurposeConfig::new()
67			.with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent),
68	);
69	let string_safe = Base64Decoder::new(&mut cursor, &BASE64_ENGINE);
70	let mut decoder = zstd::Decoder::new(string_safe).expect("Could not crate ZSTD decoder");
71	bincode::serde::decode_from_std_read(&mut decoder, bincode::config::standard()).unwrap_or_else(|err| {
72		abort_call_site!("BuildInfo data cannot be deserialized!";
73			note = "The serialized data has version {}", versioned.version;
74			note = "This crate expects version {} of the BuildInfo data", build_info_common::crate_version();
75			note = "Underlying cause: {}", err;
76		)
77	})
78}