ruitl 0.2.0

Template compiler for type-safe, server-rendered HTML components in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Server Integration Example for RUITL
//!
//! Demonstrates integrating RUITL-compiled components with an HTTP server.
//!
//! The `DemoButton` and `DemoUserCard` components are loaded from their
//! sibling `*_ruitl.rs` files (generated at build time from
//! `examples/demo_templates/*.ruitl`) — no hand-writing. The `Page`,
//! `Button`, and `UserCard` definitions below are legacy in-file showcases
//! kept for backward compatibility with the routes that still use them.
//!
//! Run with: cargo run --example server_integration
//! Then visit: http://localhost:3000

use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, Server, StatusCode};
use ruitl::prelude::*;
use std::convert::Infallible;
use std::net::SocketAddr;
use tokio;

// Pull in the generated siblings for the demo_templates directory.
#[path = "demo_templates/mod.rs"]
mod demo_templates;
use demo_templates::{DemoButton, DemoButtonProps, DemoUserCard, DemoUserCardProps};

// Legacy in-file components (Page/Button/UserCard) — kept so existing
// routes still work during the transition.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PageProps {
    pub title: String,
    pub content: String,
}

impl ComponentProps for PageProps {
    fn validate(&self) -> ruitl::error::Result<()> {
        if self.title.is_empty() {
            return Err(RuitlError::validation("Title cannot be empty"));
        }
        Ok(())
    }
}

#[derive(Debug)]
pub struct Page;

impl Component for Page {
    type Props = PageProps;

    fn render(
        &self,
        props: &Self::Props,
        _context: &ComponentContext,
    ) -> ruitl::error::Result<Html> {
        use ruitl::html::*;
        Ok(Html::Element(
            html()
                .child(Html::Element(
                    head()
                        .child(Html::Element(title().text(&props.title)))
                        .child(Html::Element(style().text(
                            "body { font-family: Arial, sans-serif; margin: 40px; }
                             .container { max-width: 800px; margin: 0 auto; }
                             .button { background: #007bff; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; }
                             .card { border: 1px solid #ddd; padding: 20px; margin: 20px 0; border-radius: 8px; }"
                        ))),
                ))
                .child(Html::Element(
                    body().child(Html::Element(
                        div()
                            .class("container")
                            .child(Html::Element(h1().text(&props.title)))
                            // `content` is pre-rendered HTML markup (e.g. the
                            // about-page body with <h2>/<ul>/<a>), so emit it
                            // raw rather than escaping with `.text()`.
                            .child(Html::Element(div().raw(&props.content))),
                    )),
                )),
        ))
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ButtonProps {
    pub text: String,
    pub variant: String,
    pub href: Option<String>,
}

impl ComponentProps for ButtonProps {
    fn validate(&self) -> ruitl::error::Result<()> {
        let valid_variants = vec!["primary", "secondary", "success", "danger"];
        if !valid_variants.contains(&self.variant.as_str()) {
            return Err(RuitlError::validation(&format!(
                "Invalid variant '{}'. Must be one of: {:?}",
                self.variant, valid_variants
            )));
        }
        Ok(())
    }
}

#[derive(Debug)]
pub struct Button;

impl Component for Button {
    type Props = ButtonProps;

    fn render(
        &self,
        props: &Self::Props,
        _context: &ComponentContext,
    ) -> ruitl::error::Result<Html> {
        use ruitl::html::*;

        if let Some(href) = &props.href {
            Ok(Html::Element(
                a().attr("href", href)
                    .class(&format!("button btn-{}", props.variant))
                    .text(&props.text),
            ))
        } else {
            Ok(Html::Element(
                button()
                    .class(&format!("button btn-{}", props.variant))
                    .attr("type", "button")
                    .text(&props.text),
            ))
        }
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct UserCardProps {
    pub name: String,
    pub email: String,
    pub role: String,
}

impl ComponentProps for UserCardProps {
    fn validate(&self) -> ruitl::error::Result<()> {
        if self.name.is_empty() {
            return Err(RuitlError::validation("Name is required"));
        }
        if !self.email.contains('@') {
            return Err(RuitlError::validation("Invalid email format"));
        }
        Ok(())
    }
}

#[derive(Debug)]
pub struct UserCard;

impl Component for UserCard {
    type Props = UserCardProps;

    fn render(
        &self,
        props: &Self::Props,
        _context: &ComponentContext,
    ) -> ruitl::error::Result<Html> {
        use ruitl::html::*;
        Ok(Html::Element(
            div()
                .class("card")
                .child(Html::Element(h3().text(&format!("User: {}", props.name))))
                .child(Html::Element(p().text(&format!("Email: {}", props.email))))
                .child(Html::Element(p().text(&format!("Role: {}", props.role)))),
        ))
    }
}

// HTTP Handler Functions
async fn handle_request(req: Request<Body>) -> std::result::Result<Response<Body>, Infallible> {
    let response = match (req.method(), req.uri().path()) {
        (&Method::GET, "/") => serve_home_page().await,
        (&Method::GET, "/users") => serve_users_page().await,
        (&Method::GET, "/demo") => serve_demo_page().await,
        (&Method::GET, "/about") => serve_about_page().await,
        (&Method::GET, "/api/users") => serve_users_api().await,
        _ => serve_404().await,
    };

    Ok(response)
}

async fn serve_home_page() -> Response<Body> {
    let page = Page;
    let props = PageProps {
        title: "RUITL Server Integration Demo".to_string(),
        content: "Welcome to the RUITL server integration example!".to_string(),
    };

    match render_full_page(page, props).await {
        Ok(html) => {
            let body = add_navigation(&html);
            Response::builder()
                .header("content-type", "text/html; charset=utf-8")
                .body(Body::from(body))
                .unwrap()
        }
        Err(e) => error_response(&format!("Render error: {}", e)),
    }
}

async fn serve_users_page() -> Response<Body> {
    let users = vec![
        UserCardProps {
            name: "Alice Johnson".to_string(),
            email: "alice@example.com".to_string(),
            role: "Admin".to_string(),
        },
        UserCardProps {
            name: "Bob Smith".to_string(),
            email: "bob@example.com".to_string(),
            role: "User".to_string(),
        },
        UserCardProps {
            name: "Carol Davis".to_string(),
            email: "carol@example.com".to_string(),
            role: "Moderator".to_string(),
        },
    ];

    let context = ComponentContext::new();
    let user_card = UserCard;

    let mut users_html = String::new();
    for user_props in users {
        match user_card.render(&user_props, &context) {
            Ok(html) => users_html.push_str(&html.render()),
            Err(e) => return error_response(&format!("User render error: {}", e)),
        }
    }

    let page = Page;
    let props = PageProps {
        title: "Users - RUITL Demo".to_string(),
        content: format!("<h2>Our Users</h2>{}", users_html),
    };

    match render_full_page(page, props).await {
        Ok(html) => {
            let body = add_navigation(&html);
            Response::builder()
                .header("content-type", "text/html; charset=utf-8")
                .body(Body::from(body))
                .unwrap()
        }
        Err(e) => error_response(&format!("Render error: {}", e)),
    }
}

/// Demonstrates rendering via sibling-generated `*_ruitl.rs` files. The two
/// components (`DemoButton`, `DemoUserCard`) live in
/// `examples/demo_templates/` and are compiled by `build.rs` at build time.
async fn serve_demo_page() -> Response<Body> {
    let ctx = ComponentContext::new();

    let btn = DemoButton;
    let btn_html = btn
        .render(
            &DemoButtonProps {
                text: "Back to home".to_string(),
                variant: "primary".to_string(),
                href: Some("/".to_string()),
            },
            &ctx,
        )
        .map(|h| h.render())
        .unwrap_or_default();

    let card = DemoUserCard;
    let card_html = card
        .render(
            &DemoUserCardProps {
                name: "Ada Lovelace".to_string(),
                email: "ada@example.com".to_string(),
                role: "Mathematician".to_string(),
            },
            &ctx,
        )
        .map(|h| h.render())
        .unwrap_or_default();

    let body = format!(
        "<h2>Components compiled from .ruitl</h2>\n<p>The markup below is rendered by real Rust structs generated at build time from <code>examples/demo_templates/*.ruitl</code>.</p>\n{}\n{}",
        card_html, btn_html
    );

    let page = Page;
    let props = PageProps {
        title: "Compiled Component Demo".to_string(),
        content: body,
    };
    match render_full_page(page, props).await {
        Ok(html) => {
            let body = add_navigation(&html);
            Response::builder()
                .header("content-type", "text/html; charset=utf-8")
                .body(Body::from(body))
                .unwrap()
        }
        Err(e) => error_response(&format!("Render error: {}", e)),
    }
}

async fn serve_about_page() -> Response<Body> {
    let button = Button;
    let button_props = ButtonProps {
        text: "Back to Home".to_string(),
        variant: "primary".to_string(),
        href: Some("/".to_string()),
    };

    let context = ComponentContext::new();
    let button_html = match button.render(&button_props, &context) {
        Ok(html) => html.render(),
        Err(e) => return error_response(&format!("Button render error: {}", e)),
    };

    let page = Page;
    let props = PageProps {
        title: "About - RUITL Demo".to_string(),
        content: format!(
            "<h2>About RUITL</h2>
             <p>RUITL is a Rust UI Template Language that compiles templates to efficient Rust code.</p>
             <p>This demo shows server-side rendering with generated components.</p>
             <h3>Features:</h3>
             <ul>
                 <li>✅ Type-safe components</li>
                 <li>✅ Server-side rendering</li>
                 <li>✅ Zero runtime template parsing</li>
                 <li>✅ HTML escaping built-in</li>
             </ul>
             <p>{}</p>",
            button_html
        ),
    };

    match render_full_page(page, props).await {
        Ok(html) => {
            let body = add_navigation(&html);
            Response::builder()
                .header("content-type", "text/html; charset=utf-8")
                .body(Body::from(body))
                .unwrap()
        }
        Err(e) => error_response(&format!("Render error: {}", e)),
    }
}

async fn serve_users_api() -> Response<Body> {
    let users = vec![
        UserCardProps {
            name: "Alice Johnson".to_string(),
            email: "alice@example.com".to_string(),
            role: "Admin".to_string(),
        },
        UserCardProps {
            name: "Bob Smith".to_string(),
            email: "bob@example.com".to_string(),
            role: "User".to_string(),
        },
    ];

    let json = serde_json::to_string(&users).unwrap();
    Response::builder()
        .header("content-type", "application/json")
        .body(Body::from(json))
        .unwrap()
}

async fn serve_404() -> Response<Body> {
    let page = Page;
    let props = PageProps {
        title: "404 - Page Not Found".to_string(),
        content: "<h2>Page Not Found</h2><p>The requested page could not be found.</p>".to_string(),
    };

    match render_full_page(page, props).await {
        Ok(html) => {
            let body = add_navigation(&html);
            Response::builder()
                .status(StatusCode::NOT_FOUND)
                .header("content-type", "text/html; charset=utf-8")
                .body(Body::from(body))
                .unwrap()
        }
        Err(e) => error_response(&format!("Render error: {}", e)),
    }
}

// Helper Functions
async fn render_full_page(page: Page, props: PageProps) -> ruitl::error::Result<String> {
    let context = ComponentContext::new();
    let html = page.render(&props, &context)?;
    Ok(html.render())
}

fn add_navigation(content: &str) -> String {
    let nav = r#"
        <style>
            .nav { background: #f8f9fa; padding: 10px 0; margin-bottom: 20px; border-bottom: 1px solid #ddd; }
            .nav ul { list-style: none; margin: 0; padding: 0; display: flex; justify-content: center; }
            .nav li { margin: 0 20px; }
            .nav a { text-decoration: none; color: #007bff; font-weight: bold; }
            .nav a:hover { text-decoration: underline; }
        </style>
        <nav class="nav">
            <ul>
                <li><a href="/">Home</a></li>
                <li><a href="/users">Users</a></li>
                <li><a href="/about">About</a></li>
                <li><a href="/api/users">API</a></li>
            </ul>
        </nav>
    "#;

    // Insert navigation after <body> tag
    content.replace("<body>", &format!("<body>{}", nav))
}

fn error_response(message: &str) -> Response<Body> {
    Response::builder()
        .status(StatusCode::INTERNAL_SERVER_ERROR)
        .header("content-type", "text/plain")
        .body(Body::from(format!("Error: {}", message)))
        .unwrap()
}

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
    println!("🚀 Starting RUITL server integration demo...");

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    let make_svc =
        make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) });

    let server = Server::bind(&addr).serve(make_svc);

    println!("🌐 Server running at http://{}", addr);
    println!("📄 Routes available:");
    println!("   • http://localhost:3000/       - Home page");
    println!("   • http://localhost:3000/users  - Users page with components");
    println!("   • http://localhost:3000/about  - About page with buttons");
    println!("   • http://localhost:3000/api/users - JSON API");
    println!();
    println!("✨ This demonstrates:");
    println!("   ✅ Server-side rendering with RUITL components");
    println!("   ✅ Type-safe props and validation");
    println!("   ✅ Component composition and reuse");
    println!("   ✅ HTML generation with proper escaping");
    println!("   ✅ Integration with HTTP frameworks");
    println!();
    println!("Press Ctrl+C to stop the server");

    if let Err(e) = server.await {
        eprintln!("Server error: {}", e);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_component_rendering() {
        let page = Page;
        let props = PageProps {
            title: "Test Page".to_string(),
            content: "Test content".to_string(),
        };

        let result = render_full_page(page, props).await;
        assert!(result.is_ok());

        let html = result.unwrap();
        assert!(html.contains("Test Page"));
        assert!(html.contains("Test content"));
        assert!(html.contains("<!DOCTYPE html") || html.contains("<html"));
    }

    #[test]
    fn test_button_validation() {
        let valid_props = ButtonProps {
            text: "Click me".to_string(),
            variant: "primary".to_string(),
            href: None,
        };
        assert!(valid_props.validate().is_ok());

        let invalid_props = ButtonProps {
            text: "Click me".to_string(),
            variant: "invalid".to_string(),
            href: None,
        };
        assert!(invalid_props.validate().is_err());
    }

    #[test]
    fn test_user_validation() {
        let valid_props = UserCardProps {
            name: "John Doe".to_string(),
            email: "john@example.com".to_string(),
            role: "User".to_string(),
        };
        assert!(valid_props.validate().is_ok());

        let invalid_props = UserCardProps {
            name: "".to_string(),
            email: "invalid-email".to_string(),
            role: "User".to_string(),
        };
        assert!(invalid_props.validate().is_err());
    }
}