{"id":18471515,"url":"https://github.com/matteopolak/axum-codec","last_synced_at":"2025-04-08T11:32:13.463Z","repository":{"id":247717930,"uuid":"826489987","full_name":"matteopolak/axum-codec","owner":"matteopolak","description":"A multi-codec extractor and response writer for Axum.","archived":false,"fork":false,"pushed_at":"2025-02-22T17:42:48.000Z","size":248,"stargazers_count":12,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T12:04:32.812Z","etag":null,"topics":["axum","bincode","bitcode","cbor","json","msgpack","rust"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/axum-codec","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matteopolak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-09T20:03:59.000Z","updated_at":"2025-02-26T04:46:29.000Z","dependencies_parsed_at":"2024-07-10T08:13:38.405Z","dependency_job_id":"f5da6ed6-f49b-4691-ac75-bf62d6cd675f","html_url":"https://github.com/matteopolak/axum-codec","commit_stats":null,"previous_names":["matteopolak/axum-codec"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matteopolak%2Faxum-codec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matteopolak%2Faxum-codec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matteopolak%2Faxum-codec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matteopolak%2Faxum-codec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matteopolak","download_url":"https://codeload.github.com/matteopolak/axum-codec/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247834013,"owners_count":21003899,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["axum","bincode","bitcode","cbor","json","msgpack","rust"],"created_at":"2024-11-06T10:17:11.607Z","updated_at":"2025-04-08T11:32:13.448Z","avatar_url":"https://github.com/matteopolak.png","language":"Rust","readme":"# Axum Codec\n\n[![\u003chttps://img.shields.io/crates/v/axum-codec\u003e](https://img.shields.io/crates/v/axum-codec)](https://crates.io/crates/axum-codec)\n[![\u003chttps://img.shields.io/docsrs/axum-codec\u003e](https://img.shields.io/docsrs/axum-codec)](https://docs.rs/axum-codec/latest/axum_codec/)\n[![ci status](https://github.com/matteopolak/axum-codec/workflows/ci/badge.svg)](https://github.com/matteopolak/axum-codec/actions)\n\nA body extractor for the [Axum](https://github.com/tokio-rs/axum) web framework.\n\n## Features\n\n- Supports encoding and decoding of various formats with a single extractor.\n- Provides a wrapper for [`axum::routing::method_routing`](https://docs.rs/axum/latest/axum/routing/method_routing/index.html) to automatically encode responses in the correct format according to the specified `Accept` header (with a fallback to `Content-Type`, then one of the enabled formats).\n- Provides an attribute macro (under the `macros` feature) to add derives for all enabled formats to a struct/enum.\n- Zero-copy decoding with `BorrowCodec`.\n\nHere's a quick example that can do the following:\n\n- Decode a `User` from the request body in any of the supported formats.\n- Encode a `Greeting` to the response body in any of the supported formats.\n\n```rust\nuse axum::{Router, response::IntoResponse};\nuse axum_codec::{\n  response::IntoCodecResponse,\n  routing::{get, post},\n  Codec, Accept,\n};\n\n// Shorthand for the following (assuming all features are enabled):\n//\n// #[derive(\n//   serde::Serialize, serde::Deserialize,\n//   bincode::Encode, bincode::Decode,\n//   bitcode::Encode, bitcode::Decode,\n//   validator::Validate,\n// )]\n// #[serde(crate = \"...\")]\n// #[bincode(crate = \"...\")]\n// #[bitcode(crate = \"...\")]\n// #[validator(crate = \"...\")]\n#[axum_codec::apply(encode, decode)]\nstruct User {\n  name: String,\n  age: u8,\n}\n\nasync fn me() -\u003e Codec\u003cUser\u003e {\n  Codec(User {\n    name: \"Alice\".into(),\n    age: 42,\n  })\n}\n\n/// A manual implementation of the handler above.\nasync fn manual_me(accept: Accept) -\u003e impl IntoResponse {\n  Codec(User {\n    name: \"Alice\".into(),\n    age: 42,\n  })\n  .into_codec_response(accept.into())\n}\n\n#[axum_codec::apply(encode)]\nstruct Greeting {\n  message: String,\n}\n\n/// Specify `impl IntoCodecResponse`, similar to `axum`\nasync fn greet(Codec(user): Codec\u003cUser\u003e) -\u003e impl IntoCodecResponse {\n  Codec(Greeting {\n    message: format!(\"Hello, {}! You are {} years old.\", user.name, user.age),\n  })\n}\n\n#[tokio::main]\nasync fn main() {\n  let app: Router = Router::new()\n    .route(\"/me\", get(me).into())\n    .route(\"/manual\", axum::routing::get(manual_me))\n    .route(\"/greet\", post(greet).into());\n\n  let listener = tokio::net::TcpListener::bind((\"127.0.0.1\", 3000))\n    .await\n    .unwrap();\n\n  // axum::serve(listener, app).await.unwrap();\n}\n```\n\n# Feature flags\n\n- `macros`: Enables the `axum_codec::apply` attribute macro.\n- `json`\\*: Enables [`JSON`](https://github.com/serde-rs/json) support.\n- `form`: Enables [`x-www-form-urlencoded`](https://github.com/nox/serde_urlencoded) support.\n- `msgpack`: Enables [`MessagePack`](https://github.com/3Hren/msgpack-rust) support.\n- `bincode`: Enables [`Bincode`](https://github.com/bincode-org/bincode) support.\n- `bitcode`: Enables [`Bitcode`](https://github.com/SoftbearStudios/bitcode) support.\n- `yaml`: Enables [`YAML`](https://github.com/dtolnay/serde-yaml/releases) support.\n- `toml`: Enables [`TOML`](https://github.com/toml-rs/toml) support.\n- `aide`: Enables support for the [`Aide`](https://github.com/tamasfe/aide) documentation library.\n- `validator`: Enables support for the [`Validator`](https://github.com/Keats/validator) validation library, validating all input when extracted with `Codec\u003cT\u003e`.\n\n\\* Enabled by default.\n\n## A note about `#[axum::debug_handler]`\n\nSince `axum-codec` uses its own `IntoCodecResponse` trait for encoding responses, it is not compatible with `#[axum::debug_handler]`. However, a new `#[axum_codec::debug_handler]` (and `#[axum_codec::debug_middleware]`) macro\nis provided as a drop-in replacement.\n\n## Roadmap\n\n- [ ] Add `codec!` macro for defining custom codecs that use a different subset of enabled formats.\n\n## License\n\nDual-licensed under MIT or Apache License v2.0.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatteopolak%2Faxum-codec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatteopolak%2Faxum-codec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatteopolak%2Faxum-codec/lists"}