#![cfg(not(feature = "no_function"))]
use super::{FnAccess, StmtBlock};
use crate::{FnArgsVec, ImmutableString};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{fmt, hash::Hash};
#[derive(Debug, Clone)]
pub struct ScriptFuncDef {
pub body: StmtBlock,
pub name: ImmutableString,
pub access: FnAccess,
#[cfg(not(feature = "no_object"))]
pub this_type: Option<ImmutableString>,
pub params: FnArgsVec<ImmutableString>,
#[cfg(feature = "metadata")]
pub comments: crate::StaticVec<crate::SmartString>,
}
impl ScriptFuncDef {
#[allow(dead_code)]
pub(crate) fn clone_function_signatures(&self) -> Self {
Self {
name: self.name.clone(),
access: self.access,
body: StmtBlock::NONE,
#[cfg(not(feature = "no_object"))]
this_type: self.this_type.clone(),
params: self.params.clone(),
#[cfg(feature = "metadata")]
comments: <_>::default(),
}
}
}
impl fmt::Display for ScriptFuncDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(not(feature = "no_object"))]
let this_type = self
.this_type
.as_ref()
.map_or(String::new(), |s| format!("{s:?}."));
#[cfg(feature = "no_object")]
let this_type = "";
write!(
f,
"{}{}{}({})",
match self.access {
FnAccess::Public => "",
FnAccess::Private => "private ",
},
this_type,
self.name,
self.params
.iter()
.map(ImmutableString::as_str)
.collect::<FnArgsVec<_>>()
.join(", ")
)
}
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
#[non_exhaustive]
pub struct ScriptFnMetadata<'a> {
pub name: &'a str,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Vec::is_empty")
)]
pub params: Vec<&'a str>,
pub access: FnAccess,
#[cfg(not(feature = "no_object"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub this_type: Option<&'a str>,
#[cfg(feature = "metadata")]
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub comments: Vec<&'a str>,
}
impl fmt::Display for ScriptFnMetadata<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(not(feature = "no_object"))]
let this_type = self
.this_type
.as_ref()
.map_or(String::new(), |s| format!("{s:?}."));
#[cfg(feature = "no_object")]
let this_type = "";
write!(
f,
"{}{}{}({})",
match self.access {
FnAccess::Public => "",
FnAccess::Private => "private ",
},
this_type,
self.name,
self.params
.iter()
.copied()
.collect::<FnArgsVec<_>>()
.join(", ")
)
}
}
impl<'a> From<&'a ScriptFuncDef> for ScriptFnMetadata<'a> {
#[inline]
fn from(value: &'a ScriptFuncDef) -> Self {
Self {
name: &value.name,
params: value.params.iter().map(ImmutableString::as_str).collect(),
access: value.access,
#[cfg(not(feature = "no_object"))]
this_type: value.this_type.as_deref(),
#[cfg(feature = "metadata")]
comments: value.comments.iter().map(<_>::as_ref).collect(),
}
}
}