footing 0.1.18

Feature-gated foundations for Rust backend services
Documentation
use utoipa::{
    PartialSchema, ToSchema,
    openapi::{
        RefOr, Schema,
        schema::{ArrayBuilder, ObjectBuilder, Type},
    },
};

use crate::http::{
    FieldViolation, FieldViolations, ProblemCode, ProblemType, RequestId, TraceParent,
    error::{MAX_PROBLEM_CODE_LENGTH, MAX_PROBLEM_TYPE_LENGTH},
    request_context::{MAX_REQUEST_ID_LENGTH, MAX_TRACEPARENT_LENGTH, TRACEPARENT_LENGTH},
};

impl PartialSchema for FieldViolations {
    fn schema() -> RefOr<Schema> {
        ArrayBuilder::new().items(FieldViolation::schema()).into()
    }
}
impl ToSchema for FieldViolations {}

// These scalar wrappers serialize as strings. Deriving an object schema from
// their Rust representation would lose the constructors' validation bounds.
impl PartialSchema for ProblemCode {
    fn schema() -> RefOr<Schema> {
        string_schema(1, MAX_PROBLEM_CODE_LENGTH)
            .pattern(Some(r"^[A-Za-z0-9_.:-]+$"))
            .into()
    }
}
impl ToSchema for ProblemCode {}

impl PartialSchema for ProblemType {
    fn schema() -> RefOr<Schema> {
        string_schema(1, MAX_PROBLEM_TYPE_LENGTH).into()
    }
}
impl ToSchema for ProblemType {}

impl PartialSchema for RequestId {
    fn schema() -> RefOr<Schema> {
        string_schema(1, MAX_REQUEST_ID_LENGTH)
            .pattern(Some(r"^[A-Za-z0-9_.:-]+$"))
            .into()
    }
}
impl ToSchema for RequestId {}

impl PartialSchema for TraceParent {
    fn schema() -> RefOr<Schema> {
        string_schema(TRACEPARENT_LENGTH, MAX_TRACEPARENT_LENGTH)
            .pattern(Some(r"^(00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}|(?:0[1-9a-f]|[1-9a-e][0-9a-f]|f[0-9a-e])-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}(?:-[\s\S]*)?)$"))
            .description(Some(
                "W3C traceparent. Version 00 is exactly 55 bytes; later versions may include opaque hyphen-prefixed fields. Version ff and all-zero trace or span identifiers are rejected.",
            ))
            .into()
    }
}
impl ToSchema for TraceParent {}

fn string_schema(min: usize, max: usize) -> ObjectBuilder {
    ObjectBuilder::new()
        .schema_type(Type::String)
        .min_length(Some(min))
        .max_length(Some(max))
}