|
2 | 2 | //! CLI application to generate PHP stub files used by IDEs. |
3 | 3 | use std::vec::Vec as StdVec; |
4 | 4 |
|
| 5 | +#[cfg(feature = "enum")] |
| 6 | +use crate::builders::EnumBuilder; |
5 | 7 | use crate::{ |
6 | 8 | builders::{ClassBuilder, FunctionBuilder}, |
7 | 9 | constant::IntoConst, |
@@ -67,6 +69,9 @@ pub struct Module { |
67 | 69 | pub functions: Vec<Function>, |
68 | 70 | /// Classes exported by the extension. |
69 | 71 | pub classes: Vec<Class>, |
| 72 | + #[cfg(feature = "enum")] |
| 73 | + /// Enums exported by the extension. |
| 74 | + pub enums: Vec<Enum>, |
70 | 75 | /// Constants exported by the extension. |
71 | 76 | pub constants: Vec<Constant>, |
72 | 77 | } |
@@ -95,6 +100,13 @@ impl From<ModuleBuilder<'_>> for Module { |
95 | 100 | .map(Constant::from) |
96 | 101 | .collect::<StdVec<_>>() |
97 | 102 | .into(), |
| 103 | + #[cfg(feature = "enum")] |
| 104 | + enums: builder |
| 105 | + .enums |
| 106 | + .into_iter() |
| 107 | + .map(|e| e().into()) |
| 108 | + .collect::<StdVec<_>>() |
| 109 | + .into(), |
98 | 110 | } |
99 | 111 | } |
100 | 112 | } |
@@ -216,6 +228,86 @@ impl From<ClassBuilder> for Class { |
216 | 228 | } |
217 | 229 | } |
218 | 230 |
|
| 231 | +#[cfg(feature = "enum")] |
| 232 | +/// Represents an exported enum. |
| 233 | +#[repr(C)] |
| 234 | +#[derive(Debug, PartialEq)] |
| 235 | +pub struct Enum { |
| 236 | + /// Name of the enum. |
| 237 | + pub name: RString, |
| 238 | + /// Documentation comments for the enum. |
| 239 | + pub docs: DocBlock, |
| 240 | + /// Cases of the enum. |
| 241 | + pub cases: Vec<EnumCase>, |
| 242 | + /// Backing type of the enum. |
| 243 | + pub backing_type: Option<RString>, |
| 244 | +} |
| 245 | + |
| 246 | +#[cfg(feature = "enum")] |
| 247 | +impl From<EnumBuilder> for Enum { |
| 248 | + fn from(val: EnumBuilder) -> Self { |
| 249 | + Self { |
| 250 | + name: val.name.into(), |
| 251 | + docs: DocBlock( |
| 252 | + val.docs |
| 253 | + .iter() |
| 254 | + .map(|d| (*d).into()) |
| 255 | + .collect::<StdVec<_>>() |
| 256 | + .into(), |
| 257 | + ), |
| 258 | + cases: val |
| 259 | + .cases |
| 260 | + .into_iter() |
| 261 | + .map(EnumCase::from) |
| 262 | + .collect::<StdVec<_>>() |
| 263 | + .into(), |
| 264 | + backing_type: match val.datatype { |
| 265 | + DataType::Long => Some("int".into()), |
| 266 | + DataType::String => Some("string".into()), |
| 267 | + _ => None, |
| 268 | + } |
| 269 | + .into(), |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +#[cfg(feature = "enum")] |
| 275 | +/// Represents a case in an exported enum. |
| 276 | +#[repr(C)] |
| 277 | +#[derive(Debug, PartialEq)] |
| 278 | +pub struct EnumCase { |
| 279 | + /// Name of the enum case. |
| 280 | + pub name: RString, |
| 281 | + /// Documentation comments for the enum case. |
| 282 | + pub docs: DocBlock, |
| 283 | + /// Value of the enum case. |
| 284 | + pub value: Option<RString>, |
| 285 | +} |
| 286 | + |
| 287 | +#[cfg(feature = "enum")] |
| 288 | +impl From<&'static crate::enum_::EnumCase> for EnumCase { |
| 289 | + fn from(val: &'static crate::enum_::EnumCase) -> Self { |
| 290 | + Self { |
| 291 | + name: val.name.into(), |
| 292 | + docs: DocBlock( |
| 293 | + val.docs |
| 294 | + .iter() |
| 295 | + .map(|d| (*d).into()) |
| 296 | + .collect::<StdVec<_>>() |
| 297 | + .into(), |
| 298 | + ), |
| 299 | + value: val |
| 300 | + .discriminant |
| 301 | + .as_ref() |
| 302 | + .map(|v| match v { |
| 303 | + crate::enum_::Discriminant::Int(i) => i.to_string().into(), |
| 304 | + crate::enum_::Discriminant::String(s) => format!("'{s}'").into(), |
| 305 | + }) |
| 306 | + .into(), |
| 307 | + } |
| 308 | + } |
| 309 | +} |
| 310 | + |
219 | 311 | /// Represents a property attached to an exported class. |
220 | 312 | #[repr(C)] |
221 | 313 | #[derive(Debug, PartialEq)] |
@@ -437,6 +529,8 @@ mod tests { |
437 | 529 | functions: vec![].into(), |
438 | 530 | classes: vec![].into(), |
439 | 531 | constants: vec![].into(), |
| 532 | + #[cfg(feature = "enum")] |
| 533 | + enums: vec![].into(), |
440 | 534 | }; |
441 | 535 |
|
442 | 536 | let description = Description::new(module); |
|
0 commit comments