1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
//! Module defining script-defined functions.
#![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};
/// _(internals)_ A type containing information on a script-defined function.
/// Exported under the `internals` feature only.
#[derive(Debug, Clone)]
pub struct ScriptFuncDef {
/// Function body.
pub body: StmtBlock,
/// Function name.
pub name: ImmutableString,
/// Function access mode.
pub access: FnAccess,
#[cfg(not(feature = "no_object"))]
/// Type of `this` pointer, if any.
/// Not available under `no_object`.
pub this_type: Option<ImmutableString>,
/// Names of function parameters.
pub params: FnArgsVec<ImmutableString>,
/// _(metadata)_ Function doc-comments (if any). Exported under the `metadata` feature only.
///
/// Doc-comments are comment lines beginning with `///` or comment blocks beginning with `/**`,
/// placed immediately before a function definition.
///
/// Block doc-comments are kept in a single string with line-breaks within.
///
/// Line doc-comments are merged, with line-breaks, into a single string without a termination line-break.
///
/// Leading white-spaces are stripped, and each string always starts with the corresponding
/// doc-comment leader: `///` or `/**`.
///
/// Each line in non-block doc-comments starts with `///`.
#[cfg(feature = "metadata")]
pub comments: crate::StaticVec<crate::SmartString>,
}
impl ScriptFuncDef {
/// Clone this [`ScriptFuncDef`] but with only signature-related info.
///
/// The body of the function is removed, as well as comments (if any).
#[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(", ")
)
}
}
/// A type containing the metadata of a script-defined function.
///
/// Not available under `no_function`.
///
/// Created by [`AST::iter_functions`][super::AST::iter_functions].
#[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> {
/// Function name.
pub name: &'a str,
/// Function parameters (if any).
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Vec::is_empty")
)]
pub params: Vec<&'a str>,
/// Function access mode.
pub access: FnAccess,
/// Type of `this` pointer, if any.
/// Not available under `no_object`.
#[cfg(not(feature = "no_object"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub this_type: Option<&'a str>,
/// _(metadata)_ Function doc-comments (if any).
/// Exported under the `metadata` feature only.
///
/// Doc-comments are comment lines beginning with `///` or comment blocks beginning with `/**`,
/// placed immediately before a function definition.
///
/// Block doc-comments are kept in a single string slice with line-breaks within.
///
/// Line doc-comments are merged, with line-breaks, into a single string slice without a termination line-break.
///
/// Leading white-spaces are stripped, and each string slice always starts with the
/// corresponding doc-comment leader: `///` or `/**`.
///
/// Each line in non-block doc-comments starts with `///`.
#[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(),
}
}
}