Skip to content

About

Declarative context management for request handlers. Eliminates the asymmetry between nodes in a system using a YAML DSL.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

299 Commits

Folders and files

Repository files navigation

context-engine

Crates.io

Data labels used by a web system's runtime within a single processing cycle should have their session-context-dependent variations resolved outside of code (e.g., system_context["session.user"] rather than users[session[user_id]]). context-engine processes the data retrieval methods that application developers define as a DSL in YAML files, for each label. This allows server/store differences in system_context["session.user.preference"] and multi-tenant differences in context["session.user.tenant"] to be resolved appropriately through the methods defined in YAML.


Version

Version Status Date Description
0.1 Released 2026-2-12 -
0.1.6 Previous 2026-4-23 improve #57
0.1.7 Current 2026-9-29 improve #1

This project adheres to Semantic Versioning.


Provided Functions

Mod Description fn
Context operates context new/get/set/delete/exists

Why context-engine?

Before:

// Manual cache management
let session_key = format!("user:{}", id);
let user = redis.get(&session_key).or_else(|| {
    let user = db.query("SELECT id, email, name FROM users WHERE id=?", id)?;
    redis.set(&session_key, &user, 3600);
    Some(user)
})?;

After:

let user = state.get("session.user.name")?;

Quick Start

  1. Add to dependencies.
# Cargo.toml
[dependencies]
context-engine = "0.1"
  1. Write a yaml file.
# mine.yml
session:
  user:
    id:
      _get:
        store: Memory
        key: "request.authorization.user.id"
    name:
      _get:
        store: Db
        key: "users.${session.user.id}.name"
Case Example
multi-tenant app tenant.yml
  1. Implement Store and StoreRegistry for your stores.
Trait Description Example
Store get() set() delete() TenantDbClient
Stores maps YAML store names to Stores MyStore
  1. Precompile your yaml to a rs file.
cargo run --example precompile --features precompile -- examples/mine.yml src/dsl_compiled.rs
# -- <input.yml: required> <output.rs: optional>
# store: values are collected from the yaml in order of first appearance;
# the resulting order (and matching STORE_IDS constant) is printed and baked into the output.
  1. Initialize Context with your registry.
use context_engine::{Context, Index};
use std::sync::Arc;

// Include the precompiled static data
include!("generated.rs");

let index = Arc::new(Index::new(
    Box::from(PATHS),
    Box::from(CHILDREN),
    Box::from(LEAVES),
    Box::from(INTERNING),
    Box::from(INTERNING_IDX),
));

let registry = MyRegistry::new();
let mut context = Context::new(index, &registry);

// --- setup completed ---

let user_name = context.get("session.user.name")?;

Architecture

┌─────────────┐        ┌─────────────────────────────────┐
│ DSL YAML    │------->│ Index (app global instance)     │
└─────────────┘compile └──────────┬──────────────────────┘
                                  │
                                  ▼
┌─────────────┐        ┌─────────────────────────────────┐
│ Application │<-------│ Context (request scope instance)│
└─────────────┘ provide└─────────────────────────────────┘
                                  ▲
                                  │
┌─────────────┐        ┌──────────┴──────────────────────┐
│ StoreImpls  │------->│ Stores (required to impl)       │
└─────────────┘register└─────────────────────────────────┘

See for details Architecture.md

Test

Passed unit and integration tests

# unit test
cargo test

# integration test (includes precompile path verification)
RUST_LOG=debug cargo run --example precompile --features precompile -- examples/tenant.yml src/dsl_compiled.rs && RUST_LOG=debug cargo run --example integration_tests --features precompile

License

SPDX-License-Identifier: Apache-2.0 Copyright (c) 2026 Andyou andyou@animagram.jp

About

Declarative context management for request handlers. Eliminates the asymmetry between nodes in a system using a YAML DSL.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Contributors

Languages