Skip to main content

larust_convert/
events.rs

1//! `app/Events/*.php` → `#[derive(Clone)] pub struct Name { pub field:
2//! Type, ... }` - `larust_events::Event` is a pure blanket impl over
3//! `Clone + Send + Sync + 'static`, no derive macro, no required methods
4//! (verified against `crates/larust-events/src/lib.rs`), so a converted
5//! event needs nothing beyond a field-only struct matching the real
6//! `PostCreated` shape. Fields come entirely from
7//! `constructor_props::extract` - whole-item safety, matching that
8//! module's own rationale: an event's field list has to be exactly right
9//! or not emitted at all, since it's the whole struct.
10
11use crate::{constructor_props, php};
12
13pub struct ConvertedEvent {
14    pub content: String,
15}
16
17/// `Ok(None)` if `class_name` isn't found in `source`. `Err` if the class
18/// is found but its constructor has a field this phase can't type.
19pub fn convert(source: &str, class_name: &str) -> Result<Option<ConvertedEvent>, String> {
20    let tree = php::parse(source).map_err(|error| error.to_string())?;
21    if php::has_syntax_error(&tree) {
22        return Err("file has a syntax error the parser couldn't recover from".to_string());
23    }
24    if php::find_class(&tree, source, class_name).is_none() {
25        return Ok(None);
26    }
27
28    let fields = constructor_props::extract(&tree, source, class_name)?;
29
30    let mut out = String::from("#[derive(Clone)]\n");
31    out.push_str(&format!("pub struct {class_name} {{\n"));
32    for field in &fields {
33        out.push_str(&format!("    pub {}: {},\n", field.name, field.rust_type));
34    }
35    out.push_str("}\n");
36
37    Ok(Some(ConvertedEvent { content: out }))
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn converts_a_promoted_property_event() {
46        let source = "<?php\nclass OrderShipped\n{\n    public function __construct(public int $orderId, public string $carrier) {}\n}\n";
47        let result = convert(source, "OrderShipped").unwrap().unwrap();
48        assert!(result.content.contains("#[derive(Clone)]"));
49        assert!(result.content.contains("pub struct OrderShipped {"));
50        assert!(result.content.contains("pub order_id: i64,"));
51        assert!(result.content.contains("pub carrier: String,"));
52    }
53
54    #[test]
55    fn converts_a_classic_style_event() {
56        let source = "<?php\nclass InvoicePaid\n{\n    public $invoiceId;\n    public function __construct(int $invoiceId)\n    {\n        $this->invoiceId = $invoiceId;\n    }\n}\n";
57        let result = convert(source, "InvoicePaid").unwrap().unwrap();
58        assert!(result.content.contains("pub invoice_id: i64,"));
59    }
60
61    #[test]
62    fn rejects_the_whole_event_when_a_field_is_a_class_type() {
63        let source = "<?php\nclass OrderShipped\n{\n    public function __construct(public Order $order) {}\n}\n";
64        assert!(convert(source, "OrderShipped").is_err());
65    }
66
67    #[test]
68    fn returns_none_when_the_class_is_not_found() {
69        let source = "<?php\nclass Foo {}\n";
70        assert!(convert(source, "Bar").unwrap().is_none());
71    }
72
73    #[test]
74    fn rejects_the_whole_event_when_a_field_type_is_unsupported() {
75        let source =
76            "<?php\nclass X\n{\n    public function __construct(public ?Order $order) {}\n}\n";
77        assert!(convert(source, "X").is_err());
78    }
79
80    #[test]
81    fn an_event_with_no_constructor_is_an_empty_struct_not_an_error() {
82        let source = "<?php\nclass Heartbeat {}\n";
83        let result = convert(source, "Heartbeat").unwrap().unwrap();
84        assert!(result.content.contains("pub struct Heartbeat {\n}\n"));
85    }
86}