|
1 | 1 | //! This module defines the `PhpEnum` trait and related types for Rust enums that are exported to PHP. |
| 2 | +use std::ptr; |
| 3 | + |
2 | 4 | use crate::{ |
3 | | - convert::IntoZval, |
| 5 | + boxed::ZBox, |
| 6 | + class::RegisteredClass, |
| 7 | + convert::{FromZendObject, FromZval, IntoZendObject, IntoZval}, |
4 | 8 | describe::DocComments, |
5 | 9 | error::{Error, Result}, |
6 | | - flags::DataType, |
7 | | - types::Zval, |
| 10 | + ffi::zend_enum_get_case, |
| 11 | + flags::{ClassFlags, DataType}, |
| 12 | + types::{ZendObject, ZendStr, Zval}, |
8 | 13 | }; |
9 | 14 |
|
10 | 15 | /// Implemented on Rust enums which are exported to PHP. |
11 | | -pub trait PhpEnum { |
| 16 | +pub trait RegisteredEnum { |
12 | 17 | /// The cases of the enum. |
13 | 18 | const CASES: &'static [EnumCase]; |
| 19 | + |
| 20 | + /// # Errors |
| 21 | + /// |
| 22 | + /// - [`Error::InvalidProperty`] if the enum does not have a case with the given name, an error is returned. |
| 23 | + fn from_name(name: &str) -> Result<Self> |
| 24 | + where |
| 25 | + Self: Sized; |
| 26 | + |
| 27 | + /// Returns the variant name of the enum as it is registered in PHP. |
| 28 | + fn to_name(&self) -> &'static str; |
| 29 | +} |
| 30 | + |
| 31 | +impl<T> FromZendObject<'_> for T |
| 32 | +where |
| 33 | + T: RegisteredEnum, |
| 34 | +{ |
| 35 | + fn from_zend_object(obj: &ZendObject) -> Result<Self> { |
| 36 | + if !ClassFlags::from_bits_truncate(unsafe { (*obj.ce).ce_flags }).contains(ClassFlags::Enum) |
| 37 | + { |
| 38 | + return Err(Error::InvalidProperty); |
| 39 | + } |
| 40 | + |
| 41 | + let name = obj |
| 42 | + .get_properties()? |
| 43 | + .get("name") |
| 44 | + .and_then(Zval::indirect) |
| 45 | + .and_then(Zval::str) |
| 46 | + .ok_or(Error::InvalidProperty)?; |
| 47 | + |
| 48 | + T::from_name(name) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<T> FromZval<'_> for T |
| 53 | +where |
| 54 | + T: RegisteredEnum + RegisteredClass, |
| 55 | +{ |
| 56 | + const TYPE: DataType = DataType::Object(Some(T::CLASS_NAME)); |
| 57 | + |
| 58 | + fn from_zval(zval: &Zval) -> Option<Self> { |
| 59 | + zval.object() |
| 60 | + .and_then(|obj| Self::from_zend_object(obj).ok()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<T> IntoZendObject for T |
| 65 | +where |
| 66 | + T: RegisteredEnum + RegisteredClass, |
| 67 | +{ |
| 68 | + fn into_zend_object(self) -> Result<ZBox<ZendObject>> { |
| 69 | + let mut name = ZendStr::new(T::to_name(&self), false); |
| 70 | + let variant = unsafe { |
| 71 | + zend_enum_get_case( |
| 72 | + ptr::from_ref(T::get_metadata().ce()).cast_mut(), |
| 73 | + &raw mut *name, |
| 74 | + ) |
| 75 | + }; |
| 76 | + |
| 77 | + Ok(unsafe { ZBox::from_raw(variant) }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<T> IntoZval for T |
| 82 | +where |
| 83 | + T: RegisteredEnum + RegisteredClass, |
| 84 | +{ |
| 85 | + const TYPE: DataType = DataType::Object(Some(T::CLASS_NAME)); |
| 86 | + const NULLABLE: bool = false; |
| 87 | + |
| 88 | + fn set_zval(self, zv: &mut Zval, _persistent: bool) -> Result<()> { |
| 89 | + let obj = self.into_zend_object()?; |
| 90 | + zv.set_object(obj.into_raw()); |
| 91 | + Ok(()) |
| 92 | + } |
14 | 93 | } |
| 94 | +// impl<'a, T> IntoZval for T |
| 95 | +// where |
| 96 | +// T: RegisteredEnum + RegisteredClass + IntoZendObject |
| 97 | +// { |
| 98 | +// const TYPE: DataType = DataType::Object(Some(T::CLASS_NAME)); |
| 99 | +// const NULLABLE: bool = false; |
| 100 | +// |
| 101 | +// fn set_zval(self, zv: &mut Zval, persistent: bool) -> Result<()> { |
| 102 | +// let obj = self.into_zend_object()?; |
| 103 | +// } |
| 104 | +// } |
15 | 105 |
|
16 | 106 | /// Represents a case in a PHP enum. |
17 | 107 | pub struct EnumCase { |
|
0 commit comments