Trait ext_php_rs::describe::ToStub
source · pub trait ToStub {
fn fmt_stub(&self, buf: &mut String) -> FmtResult;
fn to_stub(&self) -> Result<String, FmtError> { ... }
}
Expand description
Implemented on types which can be converted into PHP stubs.
Required Methods§
Provided Methods§
sourcefn to_stub(&self) -> Result<String, FmtError>
fn to_stub(&self) -> Result<String, FmtError>
Converts the implementor into PHP code, represented as a PHP stub. Returned as a string.
Returns
Returns a string on success. Returns an error if there was an error writing into the string.
Examples found in repository?
src/describe/stub.rs (line 61)
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
fn fmt_stub(&self, buf: &mut String) -> FmtResult {
writeln!(buf, "<?php")?;
writeln!(buf)?;
writeln!(buf, "// Stubs for {}", self.name)?;
writeln!(buf)?;
// To account for namespaces we need to group by them. [`None`] as the key
// represents no namespace, while [`Some`] represents a namespace.
let mut entries: HashMap<StdOption<&str>, StdVec<String>> = HashMap::new();
// Inserts a value into the entries hashmap. Takes a key and an entry, creating
// the internal vector if it doesn't already exist.
let mut insert = |ns, entry| {
let bucket = entries.entry(ns).or_insert_with(StdVec::new);
bucket.push(entry);
};
for c in &*self.constants {
let (ns, _) = split_namespace(c.name.as_ref());
insert(ns, c.to_stub()?);
}
for func in &*self.functions {
let (ns, _) = split_namespace(func.name.as_ref());
insert(ns, func.to_stub()?);
}
for class in &*self.classes {
let (ns, _) = split_namespace(class.name.as_ref());
insert(ns, class.to_stub()?);
}
let mut entries: StdVec<_> = entries.iter().collect();
entries.sort_by(|(l, _), (r, _)| match (l, r) {
(None, _) => Ordering::Greater,
(_, None) => Ordering::Less,
(Some(l), Some(r)) => l.cmp(r),
});
buf.push_str(
&entries
.into_iter()
.map(|(ns, entries)| {
let mut buf = String::new();
if let Some(ns) = ns {
writeln!(buf, "namespace {} {{", ns)?;
} else {
writeln!(buf, "namespace {{")?;
}
buf.push_str(
&entries
.iter()
.map(|entry| indent(entry, 4))
.collect::<StdVec<_>>()
.join(NEW_LINE_SEPARATOR),
);
writeln!(buf, "}}")?;
Ok(buf)
})
.collect::<Result<StdVec<_>, FmtError>>()?
.join(NEW_LINE_SEPARATOR),
);
Ok(())
}
}
impl ToStub for Function {
fn fmt_stub(&self, buf: &mut String) -> FmtResult {
self.docs.fmt_stub(buf)?;
let (_, name) = split_namespace(self.name.as_ref());
write!(
buf,
"function {}({})",
name,
self.params
.iter()
.map(ToStub::to_stub)
.collect::<Result<StdVec<_>, FmtError>>()?
.join(", ")
)?;
if let Option::Some(retval) = &self.ret {
write!(buf, ": ")?;
if retval.nullable {
write!(buf, "?")?;
}
retval.ty.fmt_stub(buf)?;
}
writeln!(buf, " {{}}")
}
}
impl ToStub for Parameter {
fn fmt_stub(&self, buf: &mut String) -> FmtResult {
if let Option::Some(ty) = &self.ty {
if self.nullable {
write!(buf, "?")?;
}
ty.fmt_stub(buf)?;
write!(buf, " ")?;
}
write!(buf, "${}", self.name)
}
}
impl ToStub for DataType {
fn fmt_stub(&self, buf: &mut String) -> FmtResult {
let mut fqdn = "\\".to_owned();
write!(
buf,
"{}",
match self {
DataType::True | DataType::False => "bool",
DataType::Long => "int",
DataType::Double => "float",
DataType::String => "string",
DataType::Array => "array",
DataType::Object(Some(ty)) => {
fqdn.push_str(ty);
fqdn.as_str()
}
DataType::Object(None) => "object",
DataType::Resource => "resource",
DataType::Reference => "reference",
DataType::Callable => "callable",
DataType::Bool => "bool",
_ => "mixed",
}
)
}
}
impl ToStub for DocBlock {
fn fmt_stub(&self, buf: &mut String) -> FmtResult {
if !self.0.is_empty() {
writeln!(buf, "/**")?;
for comment in self.0.iter() {
writeln!(buf, " *{}", comment)?;
}
writeln!(buf, " */")?;
}
Ok(())
}
}
impl ToStub for Class {
fn fmt_stub(&self, buf: &mut String) -> FmtResult {
self.docs.fmt_stub(buf)?;
let (_, name) = split_namespace(self.name.as_ref());
write!(buf, "class {} ", name)?;
if let Option::Some(extends) = &self.extends {
write!(buf, "extends {} ", extends)?;
}
if !self.implements.is_empty() {
write!(
buf,
"implements {} ",
self.implements
.iter()
.map(|s| s.str())
.collect::<StdVec<_>>()
.join(", ")
)?;
}
writeln!(buf, "{{")?;
fn stub<T: ToStub>(items: &[T]) -> impl Iterator<Item = Result<String, FmtError>> + '_ {
items
.iter()
.map(|item| item.to_stub().map(|stub| indent(&stub, 4)))
}