use std::collections::BTreeMap;
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;
use http::request::Parts;
use crate::AppError;
use crate::extract::{FromRequestParts, urlencoded};
pub const MAX_QUERY_BYTES: usize = 16 * 1024;
pub const MAX_PARAMS: usize = 1000;
pub const MAX_CONDITIONS: usize = 64;
pub const MAX_VALUES: usize = 200;
pub const MAX_TEXT: usize = 256;
pub const MAX_CURSOR: usize = 1024;
pub const MAX_SORTS: usize = 4;
pub const MAX_FIELD: usize = 64;
pub const MAX_PAGE: u64 = 10_000_000;
pub const MAX_PAGE_SIZE: u64 = 10_000;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Op {
Eq,
Ne,
Gt,
Gte,
Lt,
Lte,
In,
NotIn,
Contains,
StartsWith,
Null,
}
impl Op {
pub const ALL: [Op; 11] = [
Op::Eq,
Op::Ne,
Op::Gt,
Op::Gte,
Op::Lt,
Op::Lte,
Op::In,
Op::NotIn,
Op::Contains,
Op::StartsWith,
Op::Null,
];
pub const fn name(self) -> &'static str {
match self {
Op::Eq => "eq",
Op::Ne => "ne",
Op::Gt => "gt",
Op::Gte => "gte",
Op::Lt => "lt",
Op::Lte => "lte",
Op::In => "in",
Op::NotIn => "not_in",
Op::Contains => "contains",
Op::StartsWith => "starts_with",
Op::Null => "null",
}
}
pub fn parse(name: &str) -> Option<Op> {
Op::ALL.into_iter().find(|op| op.name() == name)
}
pub const fn takes_list(self) -> bool {
matches!(self, Op::In | Op::NotIn)
}
}
impl fmt::Display for Op {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Condition {
pub field: String,
pub op: Op,
pub values: Vec<String>,
}
impl Condition {
pub fn param(&self) -> String {
param_of(&self.field, self.op)
}
pub fn value(&self) -> &str {
self.values.first().map_or("", String::as_str)
}
pub fn null_flag(&self) -> Option<bool> {
(self.op == Op::Null).then(|| self.value() == "true")
}
}
fn param_of(field: &str, op: Op) -> String {
if op == Op::Eq {
format!("filter[{field}]")
} else {
format!("filter[{field}][{op}]")
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SortKey {
pub field: String,
pub descending: bool,
}
impl SortKey {
pub fn term(&self) -> String {
if self.descending {
format!("-{}", self.field)
} else {
self.field.clone()
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Problem {
pub param: String,
pub message: String,
}
impl Problem {
fn new(param: impl Into<String>, message: impl Into<String>) -> Self {
Self {
param: param.into(),
message: message.into(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ListQuery {
pub filter: Vec<Condition>,
pub sort: Vec<SortKey>,
pub search: Option<String>,
pub page: Option<u64>,
pub per_page: Option<u64>,
pub cursor: Option<String>,
pub before: Option<String>,
pub limit: Option<u64>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Paging {
Pages {
page: u64,
per_page: u64,
},
Cursor {
cursor: Option<String>,
before: Option<String>,
limit: u64,
},
}
impl ListQuery {
pub fn new() -> Self {
Self::default()
}
pub fn parse_lenient(query: &str) -> (ListQuery, Vec<Problem>) {
let mut reader = Reader::default();
reader.read(query.strip_prefix('?').unwrap_or(query));
(reader.list, reader.problems)
}
pub fn condition(&self, field: &str, op: Op) -> Option<&Condition> {
self.filter
.iter()
.find(|condition| condition.field == field && condition.op == op)
}
pub fn eq(&self, field: &str) -> Option<&str> {
self.condition(field, Op::Eq).map(Condition::value)
}
pub fn to_query_string(&self) -> String {
let mut out = String::new();
let mut done: Vec<&str> = Vec::new();
for first in &self.filter {
if done.contains(&first.field.as_str()) {
continue;
}
done.push(&first.field);
for condition in self.filter.iter().filter(|c| c.field == first.field) {
pair(&mut out, &condition.param(), &condition.values.join(","));
}
}
if !self.sort.is_empty() {
let terms: Vec<String> = self.sort.iter().map(SortKey::term).collect();
pair(&mut out, "sort", &terms.join(","));
}
if let Some(search) = &self.search {
pair(&mut out, "search", search);
}
if let Some(page) = self.page {
pair(&mut out, "page", &page.to_string());
}
if let Some(per_page) = self.per_page {
pair(&mut out, "per_page", &per_page.to_string());
}
if let Some(cursor) = &self.cursor {
pair(&mut out, "cursor", cursor);
}
if let Some(before) = &self.before {
pair(&mut out, "before", before);
}
if let Some(limit) = self.limit {
pair(&mut out, "limit", &limit.to_string());
}
out
}
pub fn on_page(mut self, page: u64) -> Self {
self.page = (page > 1).then_some(page);
self.cursor = None;
self.before = None;
self
}
pub fn is_cursor(&self) -> bool {
self.cursor.is_some() || self.before.is_some() || self.limit.is_some()
}
}
fn pair(out: &mut String, name: &str, value: &str) {
if !out.is_empty() {
out.push('&');
}
encode_component_into(out, name);
out.push('=');
encode_component_into(out, value);
}
const HEX: &[u8; 16] = b"0123456789ABCDEF";
pub fn encode_component(text: &str) -> String {
let mut out = String::with_capacity(text.len());
encode_component_into(&mut out, text);
out
}
fn encode_component_into(out: &mut String, text: &str) {
for byte in text.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'*' | b'-' | b'.' | b'_' => {
out.push(char::from(byte));
}
b' ' => out.push('+'),
_ => {
out.push('%');
out.push(char::from(HEX[usize::from(byte >> 4)]));
out.push(char::from(HEX[usize::from(byte & 15)]));
}
}
}
}
fn valid_name(name: &str) -> bool {
let mut chars = name.chars();
matches!(chars.next(), Some(first) if first.is_ascii_alphabetic() || first == '_')
&& name.len() <= MAX_FIELD
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '.')
}
fn clip(text: &str, max: usize) -> &str {
if text.len() <= max {
return text;
}
let mut end = max;
while !text.is_char_boundary(end) {
end -= 1;
}
&text[..end]
}
fn split_filter_key(key: &str) -> Option<(&str, Option<&str>)> {
let rest = key.strip_prefix("filter[")?;
let close = rest.find(']')?;
let field = &rest[..close];
let after = &rest[close + 1..];
if after.is_empty() {
return Some((field, None));
}
let op = after.strip_prefix('[')?.strip_suffix(']')?;
if op.contains(['[', ']']) {
return None;
}
Some((field, Some(op)))
}
fn count(value: &str, max: u64) -> Result<u64, &'static str> {
if !value.bytes().all(|byte| byte.is_ascii_digit()) {
return Err("must be a positive whole number");
}
match value.parse::<u64>() {
Ok(0) => Err("must be at least 1"),
Ok(n) if n > max => Err("is too large"),
Ok(n) => Ok(n),
Err(_) => Err("is too large"),
}
}
#[derive(Default)]
struct Reader {
list: ListQuery,
problems: Vec<Problem>,
sort_full: bool,
}
impl Reader {
fn problem(&mut self, param: impl Into<String>, message: impl Into<String>) {
self.problems.push(Problem::new(param, message));
}
fn read(&mut self, query: &str) {
let mut bytes = query.as_bytes();
if bytes.len() > MAX_QUERY_BYTES {
self.problem(
"query",
format!("the query string must not exceed {MAX_QUERY_BYTES} bytes"),
);
bytes = &bytes[..MAX_QUERY_BYTES];
}
for (read, (name, value)) in urlencoded::parse(bytes).enumerate() {
if read >= MAX_PARAMS {
self.problem(
"query",
format!("a list query takes at most {MAX_PARAMS} parameters"),
);
break;
}
let (name, value) = (&*name, &*value);
match name {
"sort" => self.sort(value),
"search" => self.search(value),
"page" => self.number(name, value, MAX_PAGE),
"per_page" | "limit" => self.number(name, value, MAX_PAGE_SIZE),
"cursor" | "before" => self.cursor(name, value),
"filter" => self.problem(
name,
"write a filter as filter[field]=value or filter[field][operator]=value",
),
_ if name.starts_with("filter[") => self.filter(name, value),
_ => {}
}
}
let numbered = self.list.page.is_some() || self.list.per_page.is_some();
if numbered && self.list.is_cursor() {
let param = if self.list.cursor.is_some() {
"cursor"
} else if self.list.before.is_some() {
"before"
} else {
"limit"
};
self.problem(
param,
"use page and per_page, or cursor and limit, not both",
);
}
}
fn sort(&mut self, value: &str) {
for term in value.split(',').map(str::trim).filter(|t| !t.is_empty()) {
let (field, descending) = term
.strip_prefix('-')
.map_or((term, false), |field| (field, true));
if !valid_name(field) {
self.problem("sort", format!("`{term}` is not a field name"));
} else if self.list.sort.iter().any(|key| key.field == field) {
self.problem("sort", format!("`{field}` is sorted twice"));
} else if self.list.sort.len() >= MAX_SORTS {
if !self.sort_full {
self.sort_full = true;
self.problem("sort", format!("sort by at most {MAX_SORTS} fields"));
}
} else {
self.list.sort.push(SortKey {
field: field.to_owned(),
descending,
});
}
}
}
fn search(&mut self, value: &str) {
let value = value.trim();
if value.is_empty() {
return;
}
if value.contains('\0') {
self.problem("search", "must not contain a NUL character");
return;
}
if value.len() > MAX_TEXT {
self.problem("search", format!("must not exceed {MAX_TEXT} bytes"));
}
self.list.search = Some(clip(value, MAX_TEXT).to_owned());
}
fn number(&mut self, name: &str, value: &str, max: u64) {
if value.is_empty() {
return;
}
match count(value, max) {
Ok(n) => match name {
"page" => self.list.page = Some(n),
"per_page" => self.list.per_page = Some(n),
_ => self.list.limit = Some(n),
},
Err(why) => self.problem(name, format!("{name} {why}")),
}
}
fn cursor(&mut self, name: &str, value: &str) {
if value.is_empty() {
return;
}
if value.contains('\0') {
self.problem(name, "must not contain a NUL character");
} else if value.len() > MAX_CURSOR {
self.problem(name, format!("must not exceed {MAX_CURSOR} bytes"));
} else if name == "cursor" {
self.list.cursor = Some(value.to_owned());
} else {
self.list.before = Some(value.to_owned());
}
}
fn filter(&mut self, key: &str, value: &str) {
let Some((field, op)) = split_filter_key(key) else {
self.problem(
key,
"write a filter as filter[field]=value or filter[field][operator]=value",
);
return;
};
if !valid_name(field) {
self.problem(
key,
format!("`{field}` is not a field name (letters, digits, _ and . only)"),
);
return;
}
let op = match op {
None => Op::Eq,
Some(name) => match Op::parse(name) {
Some(op) => op,
None => {
self.problem(
key,
format!(
"`{name}` is not an operator; use one of: {}",
Op::ALL.map(Op::name).join(", ")
),
);
return;
}
},
};
if value.trim().is_empty() {
return;
}
if value.contains('\0') {
self.problem(key, "must not contain a NUL character");
return;
}
let param = param_of(field, op);
let values: Vec<String> = match op {
Op::In | Op::NotIn => value
.split(',')
.filter(|part| !part.is_empty())
.map(str::to_owned)
.collect(),
Op::Null => match value {
"true" | "1" => vec!["true".to_owned()],
"false" | "0" => vec!["false".to_owned()],
_ => {
self.problem(param, "must be true or false");
return;
}
},
_ => vec![value.to_owned()],
};
if values.iter().any(|v| v.len() > MAX_TEXT) {
self.problem(param, format!("a value must not exceed {MAX_TEXT} bytes"));
return;
}
if values.is_empty() {
return;
}
let at = self
.list
.filter
.iter()
.position(|c| c.field == field && c.op == op);
match at {
Some(at) if op.takes_list() => {
for value in values {
if self.list.filter[at].values.len() >= MAX_VALUES {
self.problem(param, format!("takes at most {MAX_VALUES} values"));
return;
}
self.list.filter[at].values.push(value);
}
}
Some(_) => self.problem(param, "is given twice; the first is used"),
None if self.list.filter.len() >= MAX_CONDITIONS => self.problem(
"filter",
format!("a list query takes at most {MAX_CONDITIONS} filters"),
),
None => {
let mut values = values;
if values.len() > MAX_VALUES {
values.truncate(MAX_VALUES);
self.problem(param, format!("takes at most {MAX_VALUES} values"));
}
let at = self
.list
.filter
.iter()
.rposition(|c| c.field == field)
.map_or(self.list.filter.len(), |last| last + 1);
self.list.filter.insert(
at,
Condition {
field: field.to_owned(),
op,
values,
},
);
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Kind {
Text,
Int,
Decimal,
Bool,
Date,
DateTime,
Uuid,
OneOf(&'static [&'static str]),
}
const TEXT_OPS: &[Op] = &[
Op::Eq,
Op::Ne,
Op::Contains,
Op::StartsWith,
Op::In,
Op::NotIn,
Op::Null,
];
const ORDERED_OPS: &[Op] = &[
Op::Eq,
Op::Ne,
Op::Gt,
Op::Gte,
Op::Lt,
Op::Lte,
Op::In,
Op::NotIn,
Op::Null,
];
const SET_OPS: &[Op] = &[Op::Eq, Op::Ne, Op::In, Op::NotIn, Op::Null];
const BOOL_OPS: &[Op] = &[Op::Eq, Op::Ne, Op::Null];
impl Kind {
pub const fn default_ops(self) -> &'static [Op] {
match self {
Kind::Text => TEXT_OPS,
Kind::Int | Kind::Decimal | Kind::Date | Kind::DateTime => ORDERED_OPS,
Kind::Uuid | Kind::OneOf(_) => SET_OPS,
Kind::Bool => BOOL_OPS,
}
}
pub fn check(self, text: &str) -> Result<(), String> {
let wrong = |what: &str| Err(format!("`{}` is not {what}", clip(text, 40)));
match self {
Kind::Text => Ok(()),
Kind::Int => match text.parse::<i64>() {
Ok(_) => Ok(()),
Err(_) => wrong("a whole number"),
},
Kind::Decimal => {
let digits = text.strip_prefix('-').unwrap_or(text);
let (whole, fraction) = digits.split_once('.').unwrap_or((digits, "0"));
let ok = !whole.is_empty()
&& !fraction.is_empty()
&& whole.len() + fraction.len() <= 38
&& whole
.bytes()
.chain(fraction.bytes())
.all(|b| b.is_ascii_digit());
if ok {
Ok(())
} else {
wrong("a decimal number")
}
}
Kind::Bool => match text {
"true" | "false" => Ok(()),
_ => wrong("true or false"),
},
Kind::Date => {
if is_date(text) {
Ok(())
} else {
wrong("a date (YYYY-MM-DD)")
}
}
Kind::DateTime => {
if is_date(text) || chrono::DateTime::parse_from_rfc3339(text).is_ok() {
Ok(())
} else {
wrong("a date or a date and time (2026-11-05 or 2026-11-05T10:30:00Z)")
}
}
Kind::Uuid => {
if text.len() == 36 && uuid::Uuid::parse_str(text).is_ok() {
Ok(())
} else {
wrong("a UUID")
}
}
Kind::OneOf(words) => {
if words.contains(&text) {
Ok(())
} else {
Err(format!(
"`{}` is not one of: {}",
clip(text, 40),
words.join(", ")
))
}
}
}
}
}
fn is_date(text: &str) -> bool {
text.len() == 10 && chrono::NaiveDate::parse_from_str(text, "%Y-%m-%d").is_ok()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Field {
pub name: &'static str,
pub kind: Kind,
pub ops: Option<&'static [Op]>,
pub column: Option<&'static str>,
pub by_app: bool,
}
impl Field {
pub const fn new(name: &'static str, kind: Kind) -> Self {
Self {
name,
kind,
ops: None,
column: None,
by_app: false,
}
}
pub const fn text(name: &'static str) -> Self {
Self::new(name, Kind::Text)
}
pub const fn int(name: &'static str) -> Self {
Self::new(name, Kind::Int)
}
pub const fn decimal(name: &'static str) -> Self {
Self::new(name, Kind::Decimal)
}
pub const fn boolean(name: &'static str) -> Self {
Self::new(name, Kind::Bool)
}
pub const fn date(name: &'static str) -> Self {
Self::new(name, Kind::Date)
}
pub const fn datetime(name: &'static str) -> Self {
Self::new(name, Kind::DateTime)
}
pub const fn uuid(name: &'static str) -> Self {
Self::new(name, Kind::Uuid)
}
pub const fn one_of(name: &'static str, words: &'static [&'static str]) -> Self {
Self::new(name, Kind::OneOf(words))
}
pub const fn ops(mut self, ops: &'static [Op]) -> Self {
self.ops = Some(ops);
self
}
pub const fn column(mut self, column: &'static str) -> Self {
self.column = Some(column);
self
}
pub const fn by_app(mut self) -> Self {
self.by_app = true;
self
}
pub const fn allowed_ops(&self) -> &'static [Op] {
match self.ops {
Some(ops) => ops,
None => self.kind.default_ops(),
}
}
pub const fn source(&self) -> &'static str {
match self.column {
Some(column) => column,
None => self.name,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Search {
Off,
Columns(&'static [&'static str]),
ByApp,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ListSpec {
pub fields: &'static [Field],
pub sorts: &'static [&'static str],
pub default_sort: &'static [&'static str],
pub search: Search,
pub default_per_page: u64,
pub max_per_page: u64,
pub two_way_cursors: bool,
}
impl ListSpec {
pub const fn new(fields: &'static [Field]) -> Self {
Self {
fields,
sorts: &[],
default_sort: &[],
search: Search::Off,
default_per_page: 15,
max_per_page: 100,
two_way_cursors: false,
}
}
pub const fn sortable(mut self, sorts: &'static [&'static str]) -> Self {
self.sorts = sorts;
self
}
pub const fn default_sort(mut self, sorts: &'static [&'static str]) -> Self {
self.default_sort = sorts;
self
}
pub const fn searchable(mut self, columns: &'static [&'static str]) -> Self {
self.search = Search::Columns(columns);
self
}
pub const fn searchable_by_app(mut self) -> Self {
self.search = Search::ByApp;
self
}
pub const fn page_size(mut self, default: u64, max: u64) -> Self {
self.default_per_page = default;
self.max_per_page = max;
self
}
pub const fn two_way_cursors(mut self) -> Self {
self.two_way_cursors = true;
self
}
pub fn field(&self, name: &str) -> Option<&'static Field> {
self.fields.iter().find(|field| field.name == name)
}
pub fn parse(&self, query: &str) -> Result<ListQuery, ListError> {
let (list, mut problems) = ListQuery::parse_lenient(query);
problems.extend(self.check(&list));
if problems.is_empty() {
Ok(list)
} else {
Err(ListError { problems })
}
}
pub fn check(&self, list: &ListQuery) -> Vec<Problem> {
let mut problems = Vec::new();
for condition in &list.filter {
let param = condition.param();
let Some(field) = self.field(&condition.field) else {
problems.push(Problem::new(
param,
format!(
"`{}` is not a filter of this list{}",
condition.field,
names(self.fields.iter().map(|field| field.name))
),
));
continue;
};
if !field.allowed_ops().contains(&condition.op) {
let ops: Vec<&str> = field.allowed_ops().iter().map(|op| op.name()).collect();
problems.push(Problem::new(
param,
format!(
"`{}` does not take `{}`; it takes: {}",
field.name,
condition.op,
ops.join(", ")
),
));
continue;
}
if condition.op == Op::Null {
continue;
}
if condition.op == Op::StartsWith
&& condition
.values
.iter()
.any(|v| v.contains(['%', '_', '\\']))
{
problems.push(Problem::new(
param,
"a starts_with value cannot contain %, _ or a backslash",
));
continue;
}
for value in &condition.values {
if let Err(message) = field.kind.check(value) {
problems.push(Problem::new(param.clone(), message));
break;
}
}
}
for key in &list.sort {
if !self.sorts.contains(&key.field.as_str()) {
problems.push(Problem::new(
"sort",
format!(
"`{}` is not a sort of this list{}",
key.field,
names(self.sorts.iter().copied())
),
));
}
}
if list.search.is_some() && self.search == Search::Off {
problems.push(Problem::new("search", "this list cannot be searched"));
}
for (param, size) in [("per_page", list.per_page), ("limit", list.limit)] {
if let Some(size) = size.filter(|size| *size > self.max_per_page) {
problems.push(Problem::new(
param,
format!("{param} must not exceed {} (got {size})", self.max_per_page),
));
}
}
if list.before.is_some() && !self.two_way_cursors {
problems.push(Problem::new("before", "this list pages forward only"));
}
problems
}
pub fn paging(&self, list: &ListQuery) -> Paging {
if list.is_cursor() {
Paging::Cursor {
cursor: list.cursor.clone(),
before: list.before.clone(),
limit: list.limit.unwrap_or(self.default_per_page),
}
} else {
Paging::Pages {
page: list.page.unwrap_or(1),
per_page: list.per_page.unwrap_or(self.default_per_page),
}
}
}
pub fn sort_of(&self, list: &ListQuery) -> Vec<SortKey> {
if !list.sort.is_empty() {
return list.sort.clone();
}
self.default_sort
.iter()
.map(|term| {
term.strip_prefix('-').map_or_else(
|| SortKey {
field: (*term).to_owned(),
descending: false,
},
|field| SortKey {
field: field.to_owned(),
descending: true,
},
)
})
.collect()
}
}
fn names<'a>(names: impl Iterator<Item = &'a str>) -> String {
let names: Vec<&str> = names.collect();
if names.is_empty() {
String::new()
} else {
format!("; use one of: {}", names.join(", "))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListError {
pub problems: Vec<Problem>,
}
impl fmt::Display for ListError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (at, problem) in self.problems.iter().enumerate() {
if at > 0 {
f.write_str("; ")?;
}
write!(f, "{}: {}", problem.param, problem.message)?;
}
Ok(())
}
}
impl From<ListError> for AppError {
fn from(error: ListError) -> Self {
let mut errors: BTreeMap<String, Vec<String>> = BTreeMap::new();
for problem in error.problems {
errors
.entry(problem.param)
.or_default()
.push(problem.message);
}
AppError::validation(errors)
}
}
pub trait ListResource {
const SPEC: ListSpec;
}
pub struct List<R>(pub ListQuery, PhantomData<fn() -> R>);
impl<R> List<R> {
pub fn into_inner(self) -> ListQuery {
self.0
}
}
impl<R> Deref for List<R> {
type Target = ListQuery;
fn deref(&self) -> &ListQuery {
&self.0
}
}
impl<R> Clone for List<R> {
fn clone(&self) -> Self {
List(self.0.clone(), PhantomData)
}
}
impl<R> fmt::Debug for List<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("List").field(&self.0).finish()
}
}
impl<R: ListResource> List<R> {
pub fn from_uri(uri: &http::Uri) -> Result<Self, ListError> {
R::SPEC
.parse(uri.query().unwrap_or_default())
.map(|query| List(query, PhantomData))
}
}
impl<R: ListResource> FromRequestParts for List<R> {
type Rejection = AppError;
async fn from_request_parts(parts: &mut Parts) -> Result<Self, AppError> {
Self::from_uri(&parts.uri).map_err(AppError::from)
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Deals;
impl ListResource for Deals {
const SPEC: ListSpec = ListSpec::new(&[
Field::one_of("stage", &["lead", "won", "lost"]),
Field::int("amount_cents"),
Field::decimal("rate"),
Field::boolean("hot"),
Field::date("closes_on"),
Field::datetime("created_at"),
Field::uuid("owner_id"),
Field::text("title"),
Field::text("company_name").column("companies.name"),
Field::text("code").ops(&[Op::Eq]),
])
.sortable(&["amount_cents", "created_at", "title"])
.default_sort(&["-created_at"])
.searchable(&["title"])
.page_size(15, 100);
}
fn strict(query: &str) -> Result<ListQuery, ListError> {
Deals::SPEC.parse(query)
}
fn params(error: ListError) -> Vec<String> {
error.problems.into_iter().map(|p| p.param).collect()
}
#[test]
fn a_fields_conditions_are_read_together_and_read_back_as_themselves() {
let (query, problems) =
ListQuery::parse_lenient("filter[a]=x&filter[b]=%&filter[a][ne]=0&filter[b][gt]=1");
assert!(problems.is_empty(), "{problems:?}");
let order: Vec<(&str, Op)> = query
.filter
.iter()
.map(|c| (c.field.as_str(), c.op))
.collect();
assert_eq!(
order,
[("a", Op::Eq), ("a", Op::Ne), ("b", Op::Eq), ("b", Op::Gt)]
);
let (again, _) = ListQuery::parse_lenient(&query.to_query_string());
assert_eq!(again, query);
}
#[test]
fn reads_every_part() {
let list = strict(
"filter[stage][in]=lead,won&filter[amount_cents][gte]=1000&filter[hot]=true\
&filter[owner_id][null]=false&sort=-amount_cents,title&search=Sutlej&page=3&per_page=50",
)
.unwrap();
assert_eq!(list.filter.len(), 4);
assert_eq!(list.filter[0].op, Op::In);
assert_eq!(list.filter[0].values, ["lead", "won"]);
assert_eq!(list.filter[1].field, "amount_cents");
assert_eq!(list.filter[1].op, Op::Gte);
assert_eq!(list.eq("hot"), Some("true"));
assert_eq!(list.filter[3].null_flag(), Some(false));
assert_eq!(list.sort[0].term(), "-amount_cents");
assert_eq!(list.search.as_deref(), Some("Sutlej"));
assert_eq!(
Deals::SPEC.paging(&list),
Paging::Pages {
page: 3,
per_page: 50
}
);
}
#[test]
fn an_empty_query_is_an_empty_list() {
let list = strict("").unwrap();
assert_eq!(list, ListQuery::default());
assert_eq!(
Deals::SPEC.paging(&list),
Paging::Pages {
page: 1,
per_page: 15
}
);
assert_eq!(Deals::SPEC.sort_of(&list)[0].term(), "-created_at");
}
#[test]
fn cursor_pages_default_their_size() {
let list = strict("cursor=abc").unwrap();
assert_eq!(
Deals::SPEC.paging(&list),
Paging::Cursor {
cursor: Some("abc".to_owned()),
before: None,
limit: 15
}
);
let list = strict("limit=20").unwrap();
assert!(list.is_cursor());
}
#[test]
fn a_field_outside_the_allow_list_is_named() {
let error = strict("filter[password][contains]=x&filter[stage]=won").unwrap_err();
assert_eq!(error.problems.len(), 1);
assert_eq!(error.problems[0].param, "filter[password][contains]");
assert!(
error.problems[0].message.contains("use one of: stage"),
"{error}"
);
}
#[test]
fn an_operator_the_field_does_not_take_is_refused() {
assert_eq!(
params(strict("filter[hot][gte]=true").unwrap_err()),
["filter[hot][gte]"]
);
assert_eq!(
params(strict("filter[code][contains]=x").unwrap_err()),
["filter[code][contains]"]
);
assert_eq!(
params(strict("filter[stage][starts_with]=l").unwrap_err()),
["filter[stage][starts_with]"]
);
assert!(strict("filter[title][starts_with]=Su").is_ok());
assert!(strict("filter[title][starts_with]=Su%25").is_err());
assert!(strict("filter[title][starts_with]=S_").is_err());
assert!(strict("filter[closes_on][lte]=2026-11-05").is_ok());
}
#[test]
fn values_are_checked_against_the_kind() {
for (query, param) in [
("filter[amount_cents]=1.5", "filter[amount_cents]"),
("filter[amount_cents][in]=1,x", "filter[amount_cents][in]"),
("filter[rate]=1e5", "filter[rate]"),
("filter[rate]=.5", "filter[rate]"),
("filter[hot]=yes", "filter[hot]"),
("filter[closes_on]=2026-13-01", "filter[closes_on]"),
("filter[closes_on]=2026-1-1", "filter[closes_on]"),
(
"filter[created_at][gte]=yesterday",
"filter[created_at][gte]",
),
("filter[owner_id]=not-a-uuid", "filter[owner_id]"),
(
"filter[owner_id]=00000000000000000000000000000000",
"filter[owner_id]",
),
("filter[stage]=won,lost", "filter[stage]"),
("filter[stage]=archived", "filter[stage]"),
] {
assert_eq!(params(strict(query).unwrap_err()), [param], "{query}");
}
for query in [
"filter[amount_cents]=-5",
"filter[rate]=-12.50",
"filter[hot]=false",
"filter[closes_on]=2026-02-28",
"filter[created_at][lt]=2026-11-05T10:30:00%2B05:30",
"filter[created_at][lt]=2026-11-05",
"filter[owner_id]=0190a5c4-7d2e-7c3a-9d1e-0f4b6a7c8d9e",
"filter[stage][in]=lead,won",
] {
assert!(strict(query).is_ok(), "{query}");
}
}
#[test]
fn sorts_search_and_page_sizes_are_limited() {
assert_eq!(params(strict("sort=password").unwrap_err()), ["sort"]);
assert_eq!(params(strict("per_page=101").unwrap_err()), ["per_page"]);
assert_eq!(params(strict("limit=101").unwrap_err()), ["limit"]);
assert_eq!(params(strict("before=abc").unwrap_err()), ["before"]);
assert!(strict("per_page=100").is_ok());
struct Quiet;
impl ListResource for Quiet {
const SPEC: ListSpec = ListSpec::new(&[]);
}
assert_eq!(
params(Quiet::SPEC.parse("search=x").unwrap_err()),
["search"]
);
}
#[test]
fn every_problem_is_reported_at_once() {
let error = strict("filter[x]=1&sort=y&page=0&filter[hot]=maybe").unwrap_err();
assert_eq!(params(error), ["page", "filter[x]", "filter[hot]", "sort"]);
}
#[test]
fn problems_become_a_400_naming_the_parameters() {
let error = AppError::from(strict("filter[x]=1&page=0").unwrap_err());
assert_eq!(error.status.as_u16(), 400);
let errors = error.errors.unwrap();
assert!(errors.contains_key("filter[x]"));
assert!(errors.contains_key("page"));
}
#[test]
fn the_extractor_reads_the_uri() {
let uri: http::Uri = "/deals?filter[stage]=won&page=2".parse().unwrap();
let list = List::<Deals>::from_uri(&uri).unwrap();
assert_eq!(list.eq("stage"), Some("won"));
assert_eq!(list.page, Some(2));
let uri: http::Uri = "/deals?filter[nope]=1".parse().unwrap();
assert!(List::<Deals>::from_uri(&uri).is_err());
}
#[test]
fn the_query_string_round_trips() {
let text = "filter%5Bstage%5D%5Bin%5D=lead%2Cwon&filter%5Bamount_cents%5D%5Bgte%5D=100&sort=-title&search=Sutlej+Supply&page=2&per_page=25";
let (list, problems) = ListQuery::parse_lenient(text);
assert!(problems.is_empty());
let (again, _) = ListQuery::parse_lenient(&list.to_query_string());
assert_eq!(list, again);
assert_eq!(list.clone().on_page(1).page, None);
assert_eq!(list.on_page(3).page, Some(3));
}
#[test]
fn a_query_string_may_start_with_a_question_mark() {
let (list, problems) = ListQuery::parse_lenient("?search=a");
assert!(problems.is_empty());
assert_eq!(list.search.as_deref(), Some("a"));
}
#[test]
fn the_whole_query_is_clipped_not_trusted() {
let long = format!("search={}", "a".repeat(MAX_QUERY_BYTES * 2));
let (list, problems) = ListQuery::parse_lenient(&long);
assert!(problems.iter().any(|p| p.param == "query"));
assert!(list.search.is_none() || list.search.as_ref().unwrap().len() <= MAX_TEXT);
}
#[test]
fn search_is_cut_on_a_character_boundary() {
let text = "ਪ".repeat(200);
let (list, problems) = ListQuery::parse_lenient(&format!("search={text}"));
assert_eq!(problems.len(), 1);
let search = list.search.unwrap();
assert!(search.len() <= MAX_TEXT);
assert!(search.chars().all(|c| c == 'ਪ'));
}
#[test]
fn the_numbers_of_conditions_and_values_are_capped() {
let many: String = (0..MAX_CONDITIONS + 10)
.map(|n| format!("filter[f{n}]=1"))
.collect::<Vec<_>>()
.join("&");
let (list, problems) = ListQuery::parse_lenient(&many);
assert_eq!(list.filter.len(), MAX_CONDITIONS);
assert!(problems.iter().any(|p| p.param == "filter"));
let values: String = (0..MAX_VALUES + 50)
.map(|n| n.to_string())
.collect::<Vec<_>>()
.join(",");
let (list, problems) = ListQuery::parse_lenient(&format!("filter[id][in]={values}"));
assert_eq!(list.filter[0].values.len(), MAX_VALUES);
assert_eq!(problems.len(), 1);
}
#[test]
fn many_parameters_stop_the_read() {
let many: String = (0..MAX_PARAMS + 5)
.map(|n| format!("x{n}=1"))
.collect::<Vec<_>>()
.join("&");
let (_, problems) = ListQuery::parse_lenient(&many);
assert!(problems.iter().any(|p| p.param == "query"));
}
#[test]
fn encoding_matches_url_search_params() {
assert_eq!(encode_component("a b"), "a+b");
assert_eq!(encode_component("a~b"), "a%7Eb");
assert_eq!(encode_component("a*b-c.d_e"), "a*b-c.d_e");
assert_eq!(encode_component("ਪ"), "%E0%A8%AA");
assert_eq!(encode_component("a,b[c]"), "a%2Cb%5Bc%5D");
}
#[test]
fn operators_know_their_names() {
for op in Op::ALL {
assert_eq!(Op::parse(op.name()), Some(op));
}
assert_eq!(Op::parse("GTE"), None);
assert_eq!(Op::parse(""), None);
}
}