referrerpolicy=no-referrer-when-downgrade

frame_support_procedural/
crate_version.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Implementation of macros related to crate versioning.
19
20use super::get_cargo_env_var;
21use frame_support_procedural_tools::generate_access_from_frame_or_crate;
22use proc_macro2::{Span, TokenStream};
23use syn::{Error, Result};
24
25/// Create an error that will be shown by rustc at the call site of the macro.
26fn create_error(message: &str) -> Error {
27	Error::new(Span::call_site(), message)
28}
29
30/// Implementation of the `crate_to_crate_version!` macro.
31pub fn crate_to_crate_version(input: proc_macro::TokenStream) -> Result<TokenStream> {
32	if !input.is_empty() {
33		return Err(create_error("No arguments expected!"))
34	}
35
36	let major_version = get_cargo_env_var::<u16>("CARGO_PKG_VERSION_MAJOR")
37		.map_err(|_| create_error("Major version needs to fit into `u16`"))?;
38
39	let minor_version = get_cargo_env_var::<u8>("CARGO_PKG_VERSION_MINOR")
40		.map_err(|_| create_error("Minor version needs to fit into `u8`"))?;
41
42	let patch_version = get_cargo_env_var::<u8>("CARGO_PKG_VERSION_PATCH")
43		.map_err(|_| create_error("Patch version needs to fit into `u8`"))?;
44
45	let crate_ = generate_access_from_frame_or_crate("frame-support")?;
46
47	Ok(quote::quote! {
48		#crate_::traits::CrateVersion {
49			major: #major_version,
50			minor: #minor_version,
51			patch: #patch_version,
52		}
53	})
54}