use yew_server_hook::yewserverhook;
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub enum DataState<G> {
Loading,
Error(String),
Data(G),
Empty,
}
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct ApiHook<G> {
pub state: DataState<G>,
pub is_loading: bool,
pub is_updating: bool,
}
#[yewserverhook(path = "/api/users")]
pub async fn create_user(name: String, email: String) -> Result<String, String> {
Ok(format!("Created user: {} with email: {}", name, email))
}
#[yewserverhook(path = "/api/users", method = "GET")]
pub async fn list_users() -> Result<Vec<String>, String> {
Ok(vec!["user1".to_string(), "user2".to_string()])
}
#[yewserverhook(path = "/api/user", method = "PUT")]
pub async fn update_user(id: String, name: String) -> Result<String, String> {
Ok(format!("Updated user {} with name: {}", id, name))
}
#[yewserverhook(path = "/api/user", method = "DELETE")]
pub async fn delete_user(id: String) -> Result<String, String> {
Ok(format!("Deleted user {}", id))
}
#[yewserverhook(method = "PATCH", path = "/api/user/status")]
pub async fn update_user_status(id: String, status: String) -> Result<String, String> {
Ok(format!("Updated user {} status to: {}", id, status))
}
fn main() {
println!("This example demonstrates the yewserverhook macro with different HTTP methods");
println!("The macro now supports: GET, POST, PUT, DELETE, PATCH");
println!("You can specify method and path in any order");
println!("If method is not specified, it defaults to POST");
}