Refine types display.
This commit is contained in:
@@ -92,18 +92,60 @@ impl FuncInfo {
|
||||
/// `()` is cleared.
|
||||
/// [`RhaiResult`][crate::RhaiResult] and [`RhaiResultOf<T>`] are expanded.
|
||||
#[cfg(feature = "metadata")]
|
||||
pub fn format_return_type(typ: &str) -> std::borrow::Cow<str> {
|
||||
pub fn format_type(typ: &str, is_return_type: bool) -> std::borrow::Cow<str> {
|
||||
const RHAI_RESULT_TYPE: &str = "RhaiResult";
|
||||
const RHAI_RESULT_TYPE_EXPAND: &str = "Result<Dynamic, Box<EvalAltResult>>";
|
||||
const RHAI_RESULT_OF_TYPE: &str = "RhaiResultOf<";
|
||||
const RHAI_RESULT_OF_TYPE_EXPAND: &str = "Result<{}, Box<EvalAltResult>>";
|
||||
const RHAI_RANGE: &str = "ExclusiveRange";
|
||||
const RHAI_RANGE_TYPE: &str = "Range<";
|
||||
const RHAI_RANGE_EXPAND: &str = "Range<{}>";
|
||||
const RHAI_INCLUSIVE_RANGE: &str = "InclusiveRange";
|
||||
const RHAI_INCLUSIVE_RANGE_TYPE: &str = "RangeInclusive<";
|
||||
const RHAI_INCLUSIVE_RANGE_EXPAND: &str = "RangeInclusive<{}>";
|
||||
|
||||
let typ = typ.trim();
|
||||
|
||||
if typ.starts_with("rhai::") {
|
||||
return Self::format_type(&typ[6..], is_return_type);
|
||||
} else if typ.starts_with("&mut ") {
|
||||
let x = &typ[5..];
|
||||
let r = Self::format_type(x, false);
|
||||
return if r == x {
|
||||
typ.into()
|
||||
} else {
|
||||
format!("&mut {}", r).into()
|
||||
};
|
||||
}
|
||||
|
||||
match typ {
|
||||
"" | "()" => "".into(),
|
||||
"" | "()" if is_return_type => "".into(),
|
||||
"INT" => std::any::type_name::<crate::INT>().into(),
|
||||
#[cfg(not(feature = "no_float"))]
|
||||
"FLOAT" => std::any::type_name::<crate::FLOAT>().into(),
|
||||
RHAI_RANGE => RHAI_RANGE_EXPAND
|
||||
.replace("{}", std::any::type_name::<crate::INT>())
|
||||
.into(),
|
||||
RHAI_INCLUSIVE_RANGE => RHAI_INCLUSIVE_RANGE_EXPAND
|
||||
.replace("{}", std::any::type_name::<crate::INT>())
|
||||
.into(),
|
||||
RHAI_RESULT_TYPE => RHAI_RESULT_TYPE_EXPAND.into(),
|
||||
ty if ty.starts_with(RHAI_RANGE_TYPE) && ty.ends_with('>') => {
|
||||
let inner = &ty[RHAI_RANGE_TYPE.len()..ty.len() - 1];
|
||||
RHAI_RANGE_EXPAND
|
||||
.replace("{}", Self::format_type(inner, false).trim())
|
||||
.into()
|
||||
}
|
||||
ty if ty.starts_with(RHAI_INCLUSIVE_RANGE_TYPE) && ty.ends_with('>') => {
|
||||
let inner = &ty[RHAI_INCLUSIVE_RANGE_TYPE.len()..ty.len() - 1];
|
||||
RHAI_INCLUSIVE_RANGE_EXPAND
|
||||
.replace("{}", Self::format_type(inner, false).trim())
|
||||
.into()
|
||||
}
|
||||
ty if ty.starts_with(RHAI_RESULT_OF_TYPE) && ty.ends_with('>') => {
|
||||
let inner = &ty[RHAI_RESULT_OF_TYPE.len()..ty.len() - 1];
|
||||
RHAI_RESULT_OF_TYPE_EXPAND
|
||||
.replace("{}", ty[RHAI_RESULT_OF_TYPE.len()..ty.len() - 1].trim())
|
||||
.replace("{}", Self::format_type(inner, false).trim())
|
||||
.into()
|
||||
}
|
||||
ty => ty.into(),
|
||||
@@ -116,14 +158,27 @@ impl FuncInfo {
|
||||
pub fn gen_signature(&self) -> String {
|
||||
let mut sig = format!("{}(", self.metadata.name);
|
||||
|
||||
let return_type = Self::format_return_type(&self.metadata.return_type);
|
||||
let return_type = Self::format_type(&self.metadata.return_type, true);
|
||||
|
||||
if !self.metadata.params_info.is_empty() {
|
||||
let params: StaticVec<_> = self
|
||||
.metadata
|
||||
.params_info
|
||||
.iter()
|
||||
.map(|s| s.as_str())
|
||||
.map(|s| {
|
||||
let mut seg = s.splitn(2, ':');
|
||||
let name = match seg.next().unwrap().trim() {
|
||||
"" => "_",
|
||||
s => s,
|
||||
};
|
||||
let result: std::borrow::Cow<str> = match seg.next() {
|
||||
Some(typ) => {
|
||||
format!("{}: {}", name, FuncInfo::format_type(typ, false)).into()
|
||||
}
|
||||
None => name.into(),
|
||||
};
|
||||
result
|
||||
})
|
||||
.collect();
|
||||
sig.push_str(¶ms.join(", "));
|
||||
sig.push(')');
|
||||
|
Reference in New Issue
Block a user