perk-cli 0.1.0

The `perk` command: create apps, add pages, check them, and run a development server that reloads on save
//! Data sources. A page imports one with `import { greeting } from "$sources";`
//! and loads it with `<DataSource source={greeting} load="server">`. The
//! generated code calls the function of the same name here.
//!
//! A resolver can take an args struct. Its fields come from the route's
//! `[params]`, the query string, and the `args` attribute on `<DataSource>`.
//! Templates read view fields in camelCase (`data.appName`).
//!
//! Every page that loads a source also answers `GET {page}/__data.json`
//! with the same data, limited to the fields its templates read.

use perk::Error;
use serde::{Deserialize, Serialize};

type Ctx = perk::RequestContext<crate::AppState>;

#[derive(Deserialize)]
pub struct GreetingArgs {
    /// `/hello?name=Ada`.
    pub name: Option<String>,
}

#[derive(Serialize)]
pub struct GreetingView {
    pub app_name: String,
    pub message: String,
}

pub async fn greeting(ctx: &Ctx, args: GreetingArgs) -> Result<GreetingView, Error> {
    let message = match args.name.as_deref().map(str::trim).filter(|n| !n.is_empty()) {
        Some(name) => format!("Hello, {name}, from a Rust resolver."),
        None => "Hello from a Rust resolver.".into(),
    };
    Ok(GreetingView { app_name: ctx.app_name.clone(), message })
}