Search by

binarcode / laravel-restify-rust-migrate

binaryk

Export and record a Restify for Laravel application, to migrate it to Restify for Laravel for Rust.

Package info

github.com/BinarCode/laravel-restify-rust

Language:Rust

pkg:composer/binarcode/laravel-restify-rust-migrate

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.0 2026-12-04 13:46 UTC

This package is auto-updated.

Last update: 2026-12-04 15:08:23 UTC


README

JSON:API endpoints and MCP tools from your Laravel for Rust models.

Describe a model's API once, in a repository. Restify serves it to people over HTTP and to AI agents over the Model Context Protocol, with the same authorization and validation for both. It answers with the same JSON as Restify for Laravel in PHP, so a PHP application can move to Rust without its clients noticing.

Status: version 0.2, on laravel-rust 0.2. See Status.

Example

use laravel::model::model;
use laravel::prelude::*;
use laravel::time::Timestamp;
use laravel::validation::Sometimes;
use restify::{Action, Field, McpTools, Repository, Restify, Schema};

#[model]
pub struct Post {
    pub id: i64,
    pub title: String,
    pub body: Option<String>,
    #[default]
    pub votes: i64,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
}

/// What a client sends to create a post. Only these fields can be written.
#[laravel::input(fills = Post)]
#[validate(anyone)]
#[derive(Schema)]
pub struct NewPost {
    /// The post's headline.
    #[validate(required, max_length(255))]
    pub title: String,
    /// The post's text.
    pub body: Option<String>,
}

/// What a client sends to change a post. A field left out keeps its value.
#[laravel::input(fills = Post)]
#[validate(anyone)]
#[derive(Schema)]
pub struct EditPost {
    /// The post's headline.
    #[validate(required, max_length(255))]
    pub title: Sometimes<String>,
    /// The post's text. Send `null` to clear it.
    pub body: Sometimes<Option<String>>,
}

pub struct PostRepository;

impl Repository for PostRepository {
    type Model = Post;
    type Store = NewPost;
    type Update = EditPost;

    /// AI agents may list posts and read one, through the MCP server.
    const MCP: Option<McpTools> = Some(McpTools::INDEX.show(true));

    /// What a post shows, and the columns clients may search, filter and sort by.
    fn fields() -> Vec<Field<Post>> {
        vec![
            Field::new(Post::title).searchable().sortable(),
            Field::new(Post::body).searchable().hide_from_index(),
            Field::new(Post::votes).matchable().sortable(),
            Field::new(Post::created_at),
        ]
    }

    /// Anyone may read posts. Nobody may write them.
    async fn authorize(action: Action, _request: &Request, _post: Option<&Post>) -> laravel::Result<()> {
        match action {
            Action::AllowRestify | Action::Index | Action::Show => Ok(()),
            _ => Err(laravel::abort(403)),
        }
    }
}

pub fn configure() -> laravel::foundation::ApplicationBuilder {
    Application::configure().with_providers(|providers| {
        providers.add(Restify::new().repository::<PostRepository>());
    })
}

This is the example of the crate's own README, where it is compiled as a doctest. A test (example/tests/readme.rs) keeps the two the same.

That application serves:

Request Does
GET /api/restify/posts Lists a page: ?search=, ?votes=10, ?-votes=10, ?sort=-votes, ?page=, ?perPage=
GET /api/restify/posts/{id} Shows one
POST /api/restify/posts Creates one
PUT or POST /api/restify/posts/{id} Changes one, checking every field's rules
PATCH /api/restify/posts/{id} Changes the fields it sends
DELETE /api/restify/posts/{id} Deletes one
POST /mcp/restify An MCP server where AI agents list posts and read one, as const MCP allows

Features

  • One definition, two audiences. The MCP tools run the same code as the endpoints.
  • Closed by default. A repository refuses every request until its authorize method allows it. Clients see only the fields it lists, write only the fields of its input structs, and search, filter and sort only by the fields marked for it.
  • Typed. A field is one of the model's typed columns, so a wrong column is a compile error and a wrong value is a 422.
  • The same API as Restify for PHP: the same URLs, query parameters, JSON and status codes. Relationships, actions and getters, bulk operations, file and image fields, soft deletes, and register, login and profile endpoints work as they do in PHP.
  • Proven against PHP. 41 recordings of what Restify for PHP answered, 697 requests in all, are replayed against the example application on SQLite, PostgreSQL, MySQL and MariaDB. Every difference that is accepted is listed, with its reason, in oracle/divergences.json.
  • Made for AI agents. #[derive(Schema)] turns an input struct, its doc comments and its validation rules into the schema an agent reads. restify:install writes skills that teach a coding agent to use Restify.
  • An OpenAPI 3.1 document of every endpoint, for API clients and SDK generators.
  • A way off PHP. restify:import writes a Rust application's Restify code from a PHP application, and lists what is left to do by hand. With an AI coding agent, it starts with two commands in the PHP application, composer require --dev binarcode/laravel-restify-rust-migrate and php artisan restify:rust-install, then /restify-migrate in Claude Code, or one sentence to any other agent.
  • Pay only for what you use. The MCP server and the OpenAPI document are Cargo features, mcp and openapi, both on by default. Turn them off and their code leaves your binary.

Install

You need a Laravel for Rust application on laravel-rust 0.2 and Rust 1.99 or later (Restify 0.1 is for laravel-rust 0.1). Add the crate from crates.io:

cargo add laravel-restify

The package is named laravel-restify, and your code imports it as restify. The installation guide goes on from there: features, registering the provider (Restify's commands come with it), the skill for AI coding agents, and a first model.

Documentation

  • The guides: installation, repositories, fields, filtering, relationships, writing, files, actions, authorization, Sanctum tokens, authentication, soft deletes, bulk operations, MCP, OpenAPI, the reference, testing and troubleshooting. They ship inside the crate, so they match the version you install.
  • Migrating from Restify for PHP: move a PHP Restify API to Rust, and prove it answers the same.
  • The benchmark: what Restify adds to a request, measured against the same endpoints written by hand.
  • The website: the project's static site, built from the guides.
  • The recordings: how PHP's answers are recorded and replayed.

Status

Version 0.2, on laravel-rust 0.2. The crate is on crates.io, and the Composer package for moving from PHP is on Packagist. Until 1.0, a minor version can change the API. CHANGELOG.md lists what each version holds.

Running the tests

No Rust toolchain is needed on your machine: bin/cargo runs cargo in Docker.

bin/cargo test --workspace

With Rust 1.99 or later installed, use cargo directly:

cargo test --workspace

The tests use SQLite. To run them on PostgreSQL:

docker run -d --name restify-pg -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_DB=restify_test postgres:18-alpine
DOCKER_FLAGS="--network container:restify-pg -e TEST_DATABASE_URL=postgres://postgres@127.0.0.1/restify_test" \
    bin/cargo test -p restify-example
docker rm -f -v restify-pg

On MySQL 8.4:

docker run -d --name restify-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -e MYSQL_DATABASE=restify_test mysql:8.4
DOCKER_FLAGS="--network container:restify-mysql -e TEST_DATABASE_URL=mysql://root@127.0.0.1/restify_test" \
    bin/cargo test -p restify-example
docker rm -f -v restify-mysql

On MariaDB 12.3, use the mariadb:// scheme, so the framework writes MariaDB's SQL:

docker run -d --name restify-mariadb -e MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1 -e MARIADB_DATABASE=restify_test mariadb:12.3
DOCKER_FLAGS="--network container:restify-mariadb -e TEST_DATABASE_URL=mariadb://root@127.0.0.1/restify_test" \
    bin/cargo test -p restify-example
docker rm -f -v restify-mariadb

The server takes a few seconds to start: if the tests can't connect, run them again. The tests make a copy of the database for each test that runs at the same time, so the database user must be allowed to create databases. That is why the commands use root.

With cargo installed, set TEST_DATABASE_URL to any PostgreSQL, MySQL or MariaDB database instead, and run cargo test -p restify-example.

To free the disk space afterwards: docker volume rm restify-rust-target restify-rust-cargo.

CONTRIBUTING.md lists every check a change must pass, and AGENTS.md maps the code.

Layout

  • restify/: the crate (laravel-restify, imported as restify), with its guides and the skills for AI coding agents.
  • restify-macros/: #[derive(Schema)].
  • example/: a real Laravel for Rust application, and every test.
  • oracle/: the recordings of Restify for PHP.
  • php/: the Composer package with restify:export, restify:record and restify:rust-install, which a PHP application installs to migrate. Its tests make the recordings. Its composer.json is at the root.
  • imported/: what restify:import writes for the PHP test application, checked by bin/check-import.
  • bench/: the benchmark.
  • site/: the website.

Community

Licence

MIT. See LICENSE.

Two folders hold files from other projects, each with its own licence beside it: site/vendor/three/ (three.js, MIT) and example/tests/openapi-3.1/ (the OpenAPI 3.1 schema, Apache 2.0).