Path: blob/main/crates/bevy_reflect/derive/src/string_expr.rs
6599 views
use proc_macro2::TokenStream;1use quote::{quote, ToTokens};2use syn::{spanned::Spanned, LitStr};34/// Contains tokens representing different kinds of string.5#[derive(Clone)]6pub(crate) enum StringExpr {7/// A string that is valid at compile time.8///9/// This is either a string literal like `"mystring"`,10/// or a string created by a macro like [`module_path`]11/// or [`concat`].12Const(TokenStream),13/// A [string slice](str) that is borrowed for a `'static` lifetime.14Borrowed(TokenStream),15/// An [owned string](String).16Owned(TokenStream),17}1819impl<T: ToString + Spanned> From<T> for StringExpr {20fn from(value: T) -> Self {21Self::from_lit(&LitStr::new(&value.to_string(), value.span()))22}23}2425impl StringExpr {26/// Creates a [constant] [`StringExpr`] from a [`struct@LitStr`].27///28/// [constant]: StringExpr::Const29pub fn from_lit(lit: &LitStr) -> Self {30Self::Const(lit.to_token_stream())31}3233/// Creates a [constant] [`StringExpr`] by interpreting a [string slice][str] as a [`struct@LitStr`].34///35/// [constant]: StringExpr::Const36pub fn from_str(string: &str) -> Self {37Self::Const(string.into_token_stream())38}3940/// Returns tokens for an [owned string](String).41///42/// The returned expression will allocate unless the [`StringExpr`] is [already owned].43///44/// [already owned]: StringExpr::Owned45pub fn into_owned(self) -> TokenStream {46let bevy_reflect_path = crate::meta::get_bevy_reflect_path();4748match self {49Self::Const(tokens) | Self::Borrowed(tokens) => quote! {50#bevy_reflect_path::__macro_exports::alloc_utils::ToString::to_string(#tokens)51},52Self::Owned(owned) => owned,53}54}5556/// Returns tokens for a statically borrowed [string slice](str).57pub fn into_borrowed(self) -> TokenStream {58match self {59Self::Const(tokens) | Self::Borrowed(tokens) => tokens,60Self::Owned(owned) => quote! {61&#owned62},63}64}6566/// Appends a [`StringExpr`] to another.67///68/// If both expressions are [`StringExpr::Const`] this will use [`concat`] to merge them.69pub fn appended_by(mut self, other: StringExpr) -> Self {70if let Self::Const(tokens) = self {71if let Self::Const(more) = other {72return Self::Const(quote! {73::core::concat!(#tokens, #more)74});75}76self = Self::Const(tokens);77}7879let owned = self.into_owned();80let borrowed = other.into_borrowed();81Self::Owned(quote! {82::core::ops::Add::<&str>::add(#owned, #borrowed)83})84}85}8687impl Default for StringExpr {88fn default() -> Self {89StringExpr::from_str("")90}91}9293impl FromIterator<StringExpr> for StringExpr {94fn from_iter<T: IntoIterator<Item = StringExpr>>(iter: T) -> Self {95let mut iter = iter.into_iter();96match iter.next() {97Some(mut expr) => {98for next in iter {99expr = expr.appended_by(next);100}101102expr103}104None => Default::default(),105}106}107}108109110