auric-runtime 0.1.6

Runtime for the Ember-inspired Auric SPA framework
Documentation
use super::RouteModel;
use crate::Context;
use anyhow::Result;
use async_trait::async_trait;

#[async_trait]
pub trait Route {
    // Initialize the route
    fn init(&mut self, context: &dyn Context) -> Result<()>;

    async fn model(&self, params: &[serde_json::Value]) -> Result<RouteModel>;
}

#[cfg(test)]
mod tests {
    use super::{Route, RouteModel};
    use crate::Context;
    use async_trait::async_trait;
    use serde_json::{Value, json};

    #[test]
    fn can_implement_route() {
        struct MyRoute {}

        #[async_trait]
        impl Route for MyRoute {
            fn init(&mut self, _context: &dyn Context) -> anyhow::Result<()> {
                Ok(())
            }
            async fn model(&self, _params: &[Value]) -> anyhow::Result<RouteModel> {
                Ok(RouteModel::Json(json!({
                    "a": "A",
                    "b": "B",
                })))
            }
        }

        let _ = MyRoute {};
    }
}