expression

REST: unknown enum values and compatibility

Adding a new response enum value can break an older client. An explicitly tolerant reader can map an unknown unit variant to a fallback. It must not treat an unknown state as approved or successful. Here internally tagged unit variants model a small public state machine. Standalone synthetic example. Expected stdout (not executed): Ready Unknown Common mistake: Assuming all old clients have an unknown-value fallback.

Formal Syntax & Structure

rust syntaxrust
#[serde(other)]

Working Example

Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    #[derive(Deserialize, Debug)]    #[serde(tag = "state", rename_all = "snake_case")]    enum State { Pending, Ready, #[serde(other)] Unknown }    for raw in [r#"{"state":"ready"}"#, r#"{"state":"paused"}"#] {        let state: State = serde_json::from_str(raw).unwrap();        println!("{:?}", state);    }}
Rust Reference