Skip to content

Commit

Permalink
Restore sp_runtime::create_runtime_str in deprecated form
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Sep 27, 2024
1 parent 85b42fb commit f4397cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
13 changes: 13 additions & 0 deletions substrate/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,19 @@ impl OpaqueValue {
}
}

// TODO: Remove in future versions and clean up `parse_str_literal` in `sp-version-proc-macro`
/// Deprecated `Cow::Borrowed()` wrapper.
#[macro_export]
#[deprecated = "Use Cow::Borrowed() instead of create_runtime_str!()"]
macro_rules! create_runtime_str {
( $y:expr ) => {{
$crate::Cow::Borrowed($y)
}};
}
// Re-export for ^ macro, should be removed once macro is gone
#[doc(hidden)]
pub use alloc::borrow::Cow;

#[cfg(test)]
mod tests {
use crate::traits::BlakeTwo256;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,23 @@ impl ParseRuntimeVersion {

fn parse_str_literal(expr: &Expr) -> Result<String> {
match expr {
// TODO: Remove this branch when `sp_runtime::create_runtime_str` is removed
Expr::Macro(syn::ExprMacro { mac, .. }) => {
let lit: ExprLit = mac.parse_body().map_err(|e| {
Error::new(
e.span(),
format!(
"a single literal argument is expected, but parsing is failed: {}",
e
),
)
})?;

match &lit.lit {
Lit::Str(lit) => Ok(lit.value()),
_ => Err(Error::new(lit.span(), "only string literals are supported here")),
}
},
Expr::Call(call) => {
if call.args.len() != 1 {
return Err(Error::new(
Expand All @@ -197,15 +214,13 @@ impl ParseRuntimeVersion {
));
};

match lit.lit {
Lit::Str(ref lit) => Ok(lit.value()),
match &lit.lit {
Lit::Str(lit) => Ok(lit.value()),
_ => Err(Error::new(lit.span(), "only string literals are supported here")),
}
},
_ => Err(Error::new(
expr.span(),
format!("a macro expression is expected here: {expr:?}"),
)),
_ =>
Err(Error::new(expr.span(), format!("a function call is expected here: {expr:?}"))),
}
}

Expand Down
3 changes: 2 additions & 1 deletion substrate/primitives/version/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ use std::collections::HashSet;
pub use alloc::borrow::Cow;
use codec::{Decode, Encode, Input};
use scale_info::TypeInfo;
pub use sp_runtime::StateVersion;
#[allow(deprecated)]
pub use sp_runtime::{create_runtime_str, StateVersion};
#[doc(hidden)]
pub use sp_std;

Expand Down

0 comments on commit f4397cc

Please sign in to comment.