use proc_macro::{Delimiter, TokenStream, TokenTree};
use std::fmt::Write as _;
#[proc_macro_derive(Model, attributes(model, obs))]
pub fn model(input: TokenStream) -> TokenStream {
finish(expand_model(input))
}
fn finish(result: Result<String, String>) -> TokenStream {
match result {
Ok(out) => out
.parse()
.unwrap_or_else(|e| panic!("generated code did not parse: {e}")),
Err(msg) => format!("compile_error!({msg:?});")
.parse()
.expect("compile_error! literal always parses"),
}
}
#[derive(Default)]
struct FieldDef {
name: String,
ty: String,
key: bool,
skip: bool,
column: Option<String>,
unique: bool,
indexed: bool,
transient: bool,
with: Option<String>,
json: bool,
lazy: bool,
relation: Option<RelationAttr>,
link: Option<LinkAttr>,
}
struct LinkAttr {
target: String,
local: String,
remote: String,
}
struct RelationAttr {
target: String,
inverse: Option<String>,
delete: String,
ordered: Option<String>,
join: Option<String>,
}
impl FieldDef {
fn column_name(&self) -> &str {
self.column.as_deref().unwrap_or(&self.name)
}
fn persisted(&self) -> bool {
!self.skip && !self.transient && self.relation.is_none() && self.link.is_none()
}
fn nullable(&self) -> bool {
self.ty == "Option" || self.ty.starts_with("Option <")
}
}
struct StructDef {
name: String,
table: Option<String>,
external: Option<String>,
composites: Vec<Vec<String>>,
uniques: Vec<Vec<String>>,
fts: Vec<String>,
fts_tokenize: Option<String>,
spatial: Option<(String, String)>,
fields: Vec<FieldDef>,
}
fn expand_model(input: TokenStream) -> Result<String, String> {
let def = parse_struct(input)?;
let keys: Vec<&FieldDef> = def.fields.iter().filter(|f| f.key).collect();
let key = match keys.as_slice() {
[k] => *k,
[] => {
return Err(format!(
"Model: `{}` needs exactly one field marked #[model(id)]",
def.name
));
}
_ => {
return Err(format!(
"Model: `{}` marks more than one field as the id",
def.name
));
}
};
if key.transient || key.skip {
return Err(format!(
"Model: `{}`'s id field cannot be transient or skipped",
def.name
));
}
if key.with.is_some() || key.json {
return Err(format!(
"Model: `{}`'s id field takes no codec — integer, Uuid and String keys store \
in their canonical forms",
def.name
));
}
let mut out = observable_impls(&def);
out.push_str(&emit_model(&def, key)?);
Ok(out)
}
fn observable_impls(def: &StructDef) -> String {
let mut out = format!(
"day_persistence::__observable_impls! {{\n struct {} {{\n",
def.name
);
for f in def
.fields
.iter()
.filter(|f| !f.skip && f.relation.is_none() && f.link.is_none())
{
let key = if f.key { "#[obs(key)] " } else { "" };
let _ = writeln!(out, " {key}{}: {},", f.name, f.ty);
}
out.push_str(" }\n}\n");
out
}
fn emit_model(def: &StructDef, key: &FieldDef) -> Result<String, String> {
let name = &def.name;
let bare_table = def.table.clone().unwrap_or_else(|| snake_case(name));
let table = match &def.external {
Some(alias) => format!("{alias}.{bare_table}"),
None => bare_table.clone(),
};
for f in def.fields.iter().filter(|f| f.lazy) {
if f.key {
return Err(format!(
"Model: `{name}`'s id field `{}` cannot be a Blob",
f.name
));
}
if f.with.is_some() || f.json {
return Err(format!(
"Model: Blob field `{}` of `{name}` takes no codec; it is stored as it is",
f.name
));
}
if !f.persisted() {
return Err(format!(
"Model: Blob field `{}` of `{name}` cannot be transient or a relation marker",
f.name
));
}
}
if let Some(f) = def.fields.iter().find(|f| {
f.nullable() && is_blob_type(f.ty.trim_start_matches("Option <").trim_end_matches('>'))
}) {
return Err(format!(
"Model: field `{}` of `{name}` is `Option<Blob>`; a Blob column is nullable by \
itself (a NULL reads as empty), so declare it as `Blob`",
f.name
));
}
let persisted: Vec<&FieldDef> = def
.fields
.iter()
.filter(|f| f.persisted() && !f.lazy)
.chain(def.fields.iter().filter(|f| f.persisted() && f.lazy))
.collect();
for f in def.fields.iter().filter(|f| f.link.is_some()) {
let l = f.link.as_ref().expect("filtered");
if !persisted.iter().any(|p| p.column_name() == l.local) {
return Err(format!(
"Model: link(…) on `{}` names local = {:?}, which is not a persisted column of `{name}`",
f.name, l.local
));
}
}
check_name("table", &bare_table)?;
for f in &persisted {
check_name(&format!("column of field `{}`", f.name), f.column_name())?;
}
for (i, f) in persisted.iter().enumerate() {
let twin = persisted[..i]
.iter()
.find(|p| p.column_name().eq_ignore_ascii_case(f.column_name()));
if let Some(twin) = twin {
return Err(format!(
"Model: fields `{}` and `{}` of `{name}` both store in column {:?} (SQLite \
compares column names without case)",
twin.name,
f.name,
f.column_name()
));
}
}
let mut columns = String::new();
for f in &persisted {
let sql = sql_type_expr(f);
let _ = writeln!(
columns,
" day_persistence::ColumnDef {{ name: {:?}, field: \"{}\", sql: {sql}, \
not_null: {}, unique: {}, indexed: {}, lazy: {} }},",
f.column_name(),
f.name,
!f.nullable() && !f.lazy,
f.unique,
f.indexed,
f.lazy,
);
}
let column_names: Vec<&str> = persisted.iter().map(|f| f.column_name()).collect();
for c in &def.fts {
if !column_names.contains(&c.as_str()) {
return Err(format!(
"Model: fts(…) names `{c}`, which is not a persisted column of `{name}`"
));
}
}
for (what, lists) in [("index", &def.composites), ("unique", &def.uniques)] {
for cols in lists {
for c in cols {
if !column_names.contains(&c.as_str()) {
return Err(format!(
"Model: {what}(…) names `{c}`, which is not a persisted column of `{name}`"
));
}
}
}
}
if let Some((lat, lon)) = &def.spatial {
for c in [lat, lon] {
if !column_names.contains(&c.as_str()) {
return Err(format!(
"Model: spatial(…) names `{c}`, which is not a persisted column of `{name}`"
));
}
}
}
let uniques = def
.uniques
.iter()
.map(|cols| {
format!(
"&[{}]",
cols.iter()
.map(|c| format!("{c:?}"))
.collect::<Vec<_>>()
.join(", ")
)
})
.collect::<Vec<_>>()
.join(", ");
let composites = def
.composites
.iter()
.map(|cols| {
format!(
"&[{}]",
cols.iter()
.map(|c| format!("{c:?}"))
.collect::<Vec<_>>()
.join(", ")
)
})
.collect::<Vec<_>>()
.join(", ");
let mut to_row = String::new();
for f in &persisted {
let _ = writeln!(to_row, " {},", encode_expr(f));
}
let mut from_row = String::new();
for f in &def.fields {
if let Some(i) = persisted.iter().position(|p| p.name == f.name) {
let _ = writeln!(from_row, " {}: {},", f.name, decode_expr(f, i));
} else {
let _ = writeln!(
from_row,
" {}: ::core::default::Default::default(),",
f.name
);
}
}
let mut cols = String::new();
for f in &persisted {
let encode = match codec(f) {
Some(codec) => format!(
"<{codec} as day_persistence::ValueCodec<{}>>::to_sqlite_value",
f.ty
),
None => format!("day_persistence::encode_column::<{}>", f.ty),
};
let _ = write!(
cols,
" pub fn {}() -> day_persistence::Col<{}> {{\n\
\x20 day_persistence::Col::new({:?}, \"{}\", {table:?}, {encode})\n\
\x20 }}\n",
f.name,
f.ty,
f.column_name(),
f.name,
);
}
for f in def.fields.iter().filter(|f| f.relation.is_some()) {
let r = f.relation.as_ref().expect("filtered");
let _ = write!(
cols,
" pub fn {}() -> day_persistence::RelationCol<{name}, {}> {{\n\
\x20 day_persistence::RelationCol::new(\"{}\", {table:?}, \
<{} as day_persistence::Model>::TABLE)\n\
\x20 }}\n",
f.name, r.target, f.name, r.target,
);
}
for f in def.fields.iter().filter(|f| f.link.is_some()) {
let l = f.link.as_ref().expect("filtered");
let _ = write!(
cols,
" pub fn {}() -> day_persistence::RelationCol<{name}, {}> {{\n\
\x20 day_persistence::RelationCol::new(\"{}\", {table:?}, \
<{} as day_persistence::Model>::TABLE)\n\
\x20 }}\n",
f.name, l.target, f.name, l.target,
);
}
if !def.fts.is_empty() {
let _ = write!(
cols,
" pub fn fts() -> day_persistence::FtsRef {{\n\
\x20 day_persistence::FtsRef {{ columns: <{name} as day_persistence::Model>::FTS_COLUMNS }}\n\
\x20 }}\n"
);
}
if let Some((lat, lon)) = &def.spatial {
let _ = write!(
cols,
" pub fn geo() -> day_persistence::GeoRef {{\n\
\x20 day_persistence::GeoRef {{ lat: {lat:?}, lon: {lon:?} }}\n\
\x20 }}\n"
);
}
let col_impl = format!("#[allow(dead_code)]\nimpl {name} {{\n{cols}}}\n");
let rels: Vec<(&FieldDef, &RelationAttr)> = def
.fields
.iter()
.filter_map(|f| f.relation.as_ref().map(|r| (f, r)))
.collect();
let delete_expr = |d: &str| match d {
"cascade" => "day_persistence::DeleteRule::Cascade",
"deny" => "day_persistence::DeleteRule::Deny",
_ => "day_persistence::DeleteRule::Nullify",
};
let opt_str = |o: &Option<String>| match o {
Some(s) => format!("Some({s:?})"),
None => "None".into(),
};
#[expect(
clippy::format_collect,
reason = "the derive emits source text, and each item reads as the template it produces"
)]
let relations_const = if rels.is_empty() {
String::new()
} else {
format!(
" const RELATIONS: &'static [day_persistence::RelationDef] = &[\n{} ];\n",
rels.iter()
.map(|(f, r)| format!(
" day_persistence::RelationDef {{ field: \"{}\", target_table: \
<{} as day_persistence::Model>::TABLE, inverse: {:?}, delete: {}, \
ordered: {}, join: {} }},\n",
f.name,
r.target,
r.inverse.as_deref().unwrap_or(""),
delete_expr(&r.delete),
opt_str(&r.ordered),
opt_str(&r.join),
))
.collect::<String>()
)
};
let wire_fn = if rels.is_empty() {
String::new()
} else {
format!(
" fn wire(reg: &mut day_persistence::Registrar<'_>) {{\n{} }}\n",
rels.iter()
.map(|(f, r)| match (&r.join, &r.inverse) {
(Some(join), _) => format!(
" day_persistence::wire_join::<Self, {}>(reg, \"{}\", \
{join:?}, {}, {});\n",
r.target,
f.name,
opt_str(&r.ordered),
delete_expr(&r.delete),
),
(None, Some(inverse)) => format!(
" day_persistence::wire_to_many::<Self, {}>(reg, \"{}\", \
{inverse:?}, {}, {});\n",
r.target,
f.name,
delete_expr(&r.delete),
opt_str(&r.ordered),
),
(None, None) => String::new(), })
.collect::<String>()
)
};
#[expect(
clippy::format_collect,
reason = "the derive emits source text, and each item reads as the template it produces"
)]
let rel_trait = if rels.is_empty() {
String::new()
} else {
format!(
"#[allow(non_camel_case_types)]\n\
pub trait {name}Relations: day_model::Source<{name}> + Sized {{\n{}}}\n\
impl<S: day_model::Source<{name}>> {name}Relations for S {{}}\n",
rels.iter()
.map(|(f, r)| format!(
" fn {}(self) -> day_persistence::RelationRef<Self, {name}, {}> {{\n\
\x20 day_persistence::RelationRef::new(day_model::project(\
self, \"{}\", |s| &s.{}, |s| &mut s.{}))\n\
\x20 }}\n",
f.name, r.target, f.name, f.name, f.name
))
.collect::<String>()
)
};
let fts_const = if def.fts.is_empty() {
String::new()
} else {
let tokenize = match &def.fts_tokenize {
Some(t) => format!(" const FTS_TOKENIZE: Option<&'static str> = Some({t:?});\n"),
None => String::new(),
};
format!(
" const FTS_COLUMNS: &'static [&'static str] = &[{}];\n{tokenize}",
def.fts
.iter()
.map(|c| format!("{c:?}"))
.collect::<Vec<_>>()
.join(", ")
)
};
let links: Vec<(&FieldDef, &LinkAttr)> = def
.fields
.iter()
.filter_map(|f| f.link.as_ref().map(|l| (f, l)))
.collect();
#[expect(
clippy::format_collect,
reason = "the derive emits source text, and each item reads as the template it produces"
)]
let links_const = if links.is_empty() {
String::new()
} else {
format!(
" const LINKS: &'static [day_persistence::LinkDef] = &[\n{} ];\n",
links
.iter()
.map(|(f, l)| format!(
" day_persistence::LinkDef {{ field: \"{}\", local: {:?}, \
target_table: <{} as day_persistence::Model>::TABLE, remote: {:?} }},\n",
f.name, l.local, l.target, l.remote
))
.collect::<String>()
)
};
let external_const = match &def.external {
Some(alias) => format!(
" const EXTERNAL: bool = true;\n const SCHEMA: Option<&'static str> = Some({alias:?});\n"
),
None => String::new(),
};
let spatial_const = match &def.spatial {
None => String::new(),
Some((lat, lon)) => format!(
" const SPATIAL: Option<day_persistence::SpatialCols> = \
Some(day_persistence::SpatialCols {{ lat: {lat:?}, lon: {lon:?} }});\n"
),
};
let lazy_fields: Vec<&FieldDef> = persisted.iter().copied().filter(|f| f.lazy).collect();
#[expect(
clippy::format_collect,
reason = "the derive emits source text, and each item reads as the template it produces"
)]
let lazy_const = if lazy_fields.is_empty() {
String::new()
} else {
format!(
" const LAZY_FIELDS: &'static [&'static str] = &[{}];\n\
\x20 fn settle_lazy(&mut self) -> Vec<(&'static str, ::std::sync::Arc<Vec<u8>>)> {{\n\
\x20 let mut out = Vec::new();\n{}\
\x20 out\n\
\x20 }}\n",
lazy_fields
.iter()
.map(|f| format!("\"{}\"", f.name))
.collect::<Vec<_>>()
.join(", "),
lazy_fields
.iter()
.map(|f| format!(
" if let Some(b) = self.{}.take_pending() {{ out.push((\"{}\", b)); }}\n",
f.name, f.name
))
.collect::<String>(),
)
};
let key_sql = sql_type_expr(key);
Ok(col_impl
+ &rel_trait
+ &format!(
"impl day_persistence::Model for {name} {{\n\
\x20 const TABLE: &'static str = {table:?};\n\
\x20 const KEY: &'static str = {key_col:?};\n\
\x20 const KEY_SQL: day_persistence::SqlType = {key_sql};\n\
\x20 const COLUMNS: &'static [day_persistence::ColumnDef] = &[\n{columns} ];\n\
\x20 const COMPOSITE_INDEXES: &'static [&'static [&'static str]] = &[{composites}];\n\
\x20 const UNIQUE_INDEXES: &'static [&'static [&'static str]] = &[{uniques}];\n\
{fts_const}{spatial_const}{relations_const}{links_const}{external_const}{lazy_const}\
\x20 fn to_row(&self) -> Vec<day_persistence::Value> {{\n\
\x20 vec![\n{to_row} ]\n\
\x20 }}\n\
\x20 fn from_row(row: &dyn day_persistence::Row) -> \
Result<Self, day_persistence::DbError> {{\n\
\x20 Ok(Self {{\n{from_row} }})\n\
\x20 }}\n\
\x20 fn default_row() -> Vec<day_persistence::Value> {{\n\
\x20 day_persistence::Model::to_row(&<Self as ::core::default::Default>::default())\n\
\x20 }}\n\
{wire_fn}\
}}\n",
key_col = key.column_name(),
))
}
fn sql_type_expr(f: &FieldDef) -> String {
match codec(f) {
Some(codec) => format!(
"<{codec} as day_persistence::ValueCodec<{}>>::SQL_TYPE",
f.ty
),
None => format!("<{} as day_persistence::ColumnValue>::SQL_TYPE", f.ty),
}
}
fn encode_expr(f: &FieldDef) -> String {
match codec(f) {
Some(codec) => format!(
"<{codec} as day_persistence::ValueCodec<{}>>::to_sqlite_value(&self.{})",
f.ty, f.name
),
None => format!(
"day_persistence::ColumnValue::to_sqlite_value(&self.{})",
f.name
),
}
}
fn decode_expr(f: &FieldDef, i: usize) -> String {
let read = format!("day_persistence::Row::get(row, {i}usize)");
match codec(f) {
Some(codec) => format!(
"match {read} {{\n\
\x20 day_persistence::Value::Null => ::core::default::Default::default(),\n\
\x20 v => <{codec} as day_persistence::ValueCodec<{}>>::from_sqlite_value(v)?,\n\
\x20 }}",
f.ty
),
None => format!(
"match {read} {{\n\
\x20 day_persistence::Value::Null => ::core::default::Default::default(),\n\
\x20 v => <{} as day_persistence::ColumnValue>::from_sqlite_value(v)?,\n\
\x20 }}",
f.ty
),
}
}
fn codec(f: &FieldDef) -> Option<String> {
if let Some(with) = &f.with {
Some(with.clone())
} else if f.json {
Some("day_persistence::Json".into())
} else {
None
}
}
fn snake_case(name: &str) -> String {
let mut out = String::with_capacity(name.len() + 4);
for (i, ch) in name.chars().enumerate() {
if ch.is_uppercase() {
if i > 0 {
out.push('_');
}
out.extend(ch.to_lowercase());
} else {
out.push(ch);
}
}
out
}
fn parse_struct(input: TokenStream) -> Result<StructDef, String> {
let tokens: Vec<TokenTree> = input.into_iter().collect();
let mut opts = StructOptions::default();
let mut i = 0;
while i < tokens.len() {
match &tokens[i] {
TokenTree::Ident(id) if id.to_string() == "struct" => break,
TokenTree::Punct(p) if p.as_char() == '#' => {
if let Some(TokenTree::Group(g)) = tokens.get(i + 1) {
if let Some(items) = attr_items(g, "model") {
parse_struct_options(items, &mut opts)?;
}
i += 2;
continue;
}
return Err("malformed attribute on the struct".into());
}
_ => i += 1,
}
}
if i >= tokens.len() {
return Err("the derive expects a struct".into());
}
let name = match tokens.get(i + 1) {
Some(TokenTree::Ident(id)) => id.to_string(),
_ => return Err("the derive expects a named struct".into()),
};
let body = match tokens.get(i + 2) {
Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => g.stream(),
Some(TokenTree::Punct(p)) if p.as_char() == '<' => {
return Err("the derive does not support generic structs yet".into());
}
_ => return Err("the derive expects a struct with named fields".into()),
};
let StructOptions {
table,
external,
composites,
uniques,
fts,
fts_tokenize,
spatial,
} = opts;
Ok(StructDef {
name,
table,
external,
composites,
uniques,
fts,
fts_tokenize,
spatial,
fields: parse_fields(body)?,
})
}
fn attr_items(bracket: &proc_macro::Group, name: &str) -> Option<Vec<Vec<TokenTree>>> {
let inner: Vec<TokenTree> = bracket.stream().into_iter().collect();
match (inner.first(), inner.get(1)) {
(Some(TokenTree::Ident(id)), Some(TokenTree::Group(g)))
if id.to_string() == name && g.delimiter() == Delimiter::Parenthesis =>
{
let mut items = vec![Vec::new()];
for tt in g.stream() {
match &tt {
TokenTree::Punct(p) if p.as_char() == ',' => items.push(Vec::new()),
_ => items.last_mut().expect("never empty").push(tt),
}
}
items.retain(|item| !item.is_empty());
Some(items)
}
_ => None,
}
}
#[derive(Default)]
struct StructOptions {
table: Option<String>,
external: Option<String>,
composites: Vec<Vec<String>>,
uniques: Vec<Vec<String>>,
fts: Vec<String>,
fts_tokenize: Option<String>,
spatial: Option<(String, String)>,
}
fn parse_struct_options(
items: Vec<Vec<TokenTree>>,
opts: &mut StructOptions,
) -> Result<(), String> {
let StructOptions {
table,
external,
composites,
uniques,
fts,
fts_tokenize,
spatial,
} = opts;
for item in items {
match item.as_slice() {
[TokenTree::Ident(id), TokenTree::Group(g)]
if id.to_string() == "fts" && g.delimiter() == Delimiter::Parenthesis =>
{
let mut fts_items = vec![Vec::new()];
for tt in g.stream() {
match &tt {
TokenTree::Punct(p) if p.as_char() == ',' => fts_items.push(Vec::new()),
_ => fts_items.last_mut().expect("never empty").push(tt.clone()),
}
}
for item in fts_items.into_iter().filter(|i| !i.is_empty()) {
match item.as_slice() {
[TokenTree::Literal(lit)] => fts.push(string_literal(lit)?),
[
TokenTree::Ident(k),
TokenTree::Punct(eq),
TokenTree::Literal(lit),
] if k.to_string() == "tokenize" && eq.as_char() == '=' => {
*fts_tokenize = Some(string_literal(lit)?);
}
other => {
return Err(format!(
"fts(…) expects column string literals and an optional \
tokenize = \"…\", found `{}`",
tokens_text(other)
));
}
}
}
}
[TokenTree::Ident(id), TokenTree::Group(g)]
if id.to_string() == "spatial" && g.delimiter() == Delimiter::Parenthesis =>
{
let mut lat = None;
let mut lon = None;
let inner: Vec<TokenTree> = g.stream().into_iter().collect();
let mut j = 0;
while j + 2 < inner.len() + 1 {
match (&inner.get(j), &inner.get(j + 1), &inner.get(j + 2)) {
(
Some(TokenTree::Ident(k)),
Some(TokenTree::Punct(eq)),
Some(TokenTree::Literal(lit)),
) if eq.as_char() == '=' => {
match k.to_string().as_str() {
"lat" => lat = Some(string_literal(lit)?),
"lon" => lon = Some(string_literal(lit)?),
other => {
return Err(format!(
"spatial(…) takes lat = \"…\" and lon = \"…\", found `{other}`"
));
}
}
j += 3;
if matches!(inner.get(j), Some(TokenTree::Punct(p)) if p.as_char() == ',')
{
j += 1;
}
}
_ => return Err("spatial(…) takes lat = \"…\" and lon = \"…\"".into()),
}
}
match (lat, lon) {
(Some(lat), Some(lon)) => *spatial = Some((lat, lon)),
_ => return Err("spatial(…) needs BOTH lat = \"…\" and lon = \"…\"".into()),
}
}
[
TokenTree::Ident(id),
TokenTree::Punct(eq),
TokenTree::Literal(lit),
] if id.to_string() == "table" && eq.as_char() == '=' => {
*table = Some(string_literal(lit)?);
}
[
TokenTree::Ident(id),
TokenTree::Punct(eq),
TokenTree::Literal(lit),
] if id.to_string() == "external" && eq.as_char() == '=' => {
let alias = string_literal(lit)?;
if alias.is_empty() || !alias.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
{
return Err(format!(
"external = {alias:?} must be a plain SQL schema name (letters, digits, `_`)"
));
}
*external = Some(alias);
}
[TokenTree::Ident(id), TokenTree::Group(g)]
if id.to_string() == "index" && g.delimiter() == Delimiter::Parenthesis =>
{
let mut cols = Vec::new();
for tt in g.stream() {
match &tt {
TokenTree::Literal(lit) => cols.push(string_literal(lit)?),
TokenTree::Punct(p) if p.as_char() == ',' => {}
other => {
return Err(format!(
"index(…) expects string literals, found `{other}`"
));
}
}
}
composites.push(cols);
}
[TokenTree::Ident(id), TokenTree::Group(g)]
if id.to_string() == "unique" && g.delimiter() == Delimiter::Parenthesis =>
{
let mut cols = Vec::new();
for tt in g.stream() {
match &tt {
TokenTree::Literal(lit) => cols.push(string_literal(lit)?),
TokenTree::Punct(p) if p.as_char() == ',' => {}
other => {
return Err(format!(
"unique(…) expects string literals, found `{other}`"
));
}
}
}
if cols.is_empty() {
return Err("unique(…) names at least one column".into());
}
uniques.push(cols);
}
other => {
return Err(format!(
"unknown #[model] option on the struct: `{}` (supported: table = \"…\", external = \"alias\", index(\"a\", \"b\"), unique(\"a\", \"b\"), fts(\"a\", \"b\"), spatial(lat = \"…\", lon = \"…\"))",
tokens_text(other)
));
}
}
}
Ok(())
}
fn parse_field_options(items: Vec<Vec<TokenTree>>, f: &mut FieldDef) -> Result<(), String> {
for item in items {
match item.as_slice() {
[TokenTree::Ident(id)] => match id.to_string().as_str() {
"id" => f.key = true,
"unique" => f.unique = true,
"index" => f.indexed = true,
"transient" => f.transient = true,
"json" => f.json = true,
other => {
return Err(format!(
"unknown #[model] option on field `{}`: `{other}` (supported: id, unique, index, transient, json, column = \"…\", with = Codec)",
f.name
));
}
},
[
TokenTree::Ident(id),
TokenTree::Punct(eq),
TokenTree::Literal(lit),
] if id.to_string() == "column" && eq.as_char() == '=' => {
f.column = Some(string_literal(lit)?);
}
[TokenTree::Ident(id), TokenTree::Punct(eq), rest @ ..]
if id.to_string() == "with" && eq.as_char() == '=' && !rest.is_empty() =>
{
f.with = Some(tokens_text(rest));
}
[TokenTree::Ident(id), TokenTree::Group(g)]
if id.to_string() == "relation" && g.delimiter() == Delimiter::Parenthesis =>
{
f.relation = Some(parse_relation(g, &f.name)?);
}
[TokenTree::Ident(id), TokenTree::Group(g)]
if id.to_string() == "link" && g.delimiter() == Delimiter::Parenthesis =>
{
f.link = Some(parse_link(g, &f.name)?);
}
other => {
return Err(format!(
"unknown #[model] option on field `{}`: `{}`",
f.name,
tokens_text(other)
));
}
}
}
Ok(())
}
fn parse_relation(g: &proc_macro::Group, field: &str) -> Result<RelationAttr, String> {
let mut items = vec![Vec::new()];
for tt in g.stream() {
match &tt {
TokenTree::Punct(p) if p.as_char() == ',' => items.push(Vec::new()),
_ => items.last_mut().expect("never empty").push(tt),
}
}
items.retain(|i| !i.is_empty());
let (mut target, mut inverse, mut delete, mut ordered, mut join) =
(None, None, None, None, None);
for item in items {
match item.as_slice() {
[TokenTree::Ident(k), TokenTree::Punct(eq), rest @ ..]
if eq.as_char() == '=' && !rest.is_empty() =>
{
match k.to_string().as_str() {
"target" => target = Some(tokens_text(rest)),
"inverse" => inverse = Some(literal_str(rest, field)?),
"delete" => delete = Some(literal_str(rest, field)?),
"ordered" => ordered = Some(literal_str(rest, field)?),
"join" => join = Some(literal_str(rest, field)?),
other => {
return Err(format!(
"relation(…) on `{field}`: unknown option `{other}` (supported: \
target = Type, inverse = \"field\", delete = \"…\", \
ordered = \"field\", join = \"table\")"
));
}
}
}
[TokenTree::Ident(k)] if k.to_string() == "ordered" => {
ordered = Some("position".into());
}
other => {
return Err(format!(
"relation(…) on `{field}`: expected `name = …`, found `{}`",
tokens_text(other)
));
}
}
}
let Some(target) = target else {
return Err(format!("relation(…) on `{field}` needs target = Type"));
};
let delete = delete.unwrap_or_else(|| "nullify".into());
if !["nullify", "cascade", "deny"].contains(&delete.as_str()) {
return Err(format!(
"relation(…) on `{field}`: delete = {delete:?} — nullify, cascade, or deny"
));
}
if inverse.is_none() && join.is_none() {
return Err(format!(
"relation(…) on `{field}` needs inverse = \"field\" (the target's One<…> field) \
— or join = \"table\" for a many-to-many"
));
}
Ok(RelationAttr {
target,
inverse,
delete,
ordered,
join,
})
}
fn parse_link(g: &proc_macro::Group, field: &str) -> Result<LinkAttr, String> {
let mut items = vec![Vec::new()];
for tt in g.stream() {
match &tt {
TokenTree::Punct(p) if p.as_char() == ',' => items.push(Vec::new()),
_ => items.last_mut().expect("never empty").push(tt),
}
}
items.retain(|i| !i.is_empty());
let (mut target, mut local, mut remote) = (None, None, None);
for item in items {
match item.as_slice() {
[TokenTree::Ident(k), TokenTree::Punct(eq), rest @ ..]
if eq.as_char() == '=' && !rest.is_empty() =>
{
match k.to_string().as_str() {
"target" => target = Some(tokens_text(rest)),
"local" => local = Some(literal_str(rest, field)?),
"remote" => remote = Some(literal_str(rest, field)?),
other => {
return Err(format!(
"link(…) on `{field}`: unknown option `{other}` (supported: \
target = Type, local = \"column\", remote = \"column\")"
));
}
}
}
other => {
return Err(format!(
"link(…) on `{field}`: expected `name = …`, found `{}`",
tokens_text(other)
));
}
}
}
match (target, local, remote) {
(Some(target), Some(local), Some(remote)) => Ok(LinkAttr {
target,
local,
remote,
}),
_ => Err(format!(
"link(…) on `{field}` needs target = Type, local = \"column\", and remote = \"column\""
)),
}
}
fn literal_str(rest: &[TokenTree], field: &str) -> Result<String, String> {
match rest {
[TokenTree::Literal(l)] => string_literal(l),
other => Err(format!(
"relation(…) on `{field}`: expected a string literal, found `{}`",
tokens_text(other)
)),
}
}
fn string_literal(lit: &proc_macro::Literal) -> Result<String, String> {
let s = lit.to_string();
if let Some(raw) = s.strip_prefix('r') {
let hashes = raw.len() - raw.trim_start_matches('#').len();
let body = &raw[hashes..raw.len() - hashes];
if hashes * 2 + 2 <= raw.len() && body.starts_with('"') && body.ends_with('"') {
return Ok(body[1..body.len() - 1].to_string());
}
return Err(format!("expected a string literal, found `{s}`"));
}
if s.len() < 2 || !s.starts_with('"') || !s.ends_with('"') {
return Err(format!("expected a string literal, found `{s}`"));
}
let mut out = String::with_capacity(s.len());
let mut chars = s[1..s.len() - 1].chars();
while let Some(ch) = chars.next() {
if ch != '\\' {
out.push(ch);
continue;
}
match chars.next() {
Some('"') => out.push('"'),
Some('\\') => out.push('\\'),
Some('\'') => out.push('\''),
Some('n') => out.push('\n'),
Some('r') => out.push('\r'),
Some('t') => out.push('\t'),
Some('0') => out.push('\0'),
Some('x') => {
let hex: String = chars.by_ref().take(2).collect();
let code = u8::from_str_radix(&hex, 16)
.map_err(|_| format!("bad escape `\\x{hex}` in {s}"))?;
out.push(code as char);
}
Some('u') => {
if chars.next() != Some('{') {
return Err(format!("bad escape `\\u` in {s}"));
}
let hex: String = chars.by_ref().take_while(|c| *c != '}').collect();
let code = u32::from_str_radix(&hex, 16)
.ok()
.and_then(char::from_u32)
.ok_or_else(|| format!("bad escape `\\u{{{hex}}}` in {s}"))?;
out.push(code);
}
Some('\n') => {
let rest = chars.as_str();
let trimmed = rest.trim_start();
chars = trimmed.chars();
let _ = rest;
}
other => {
return Err(format!(
"unsupported escape `\\{}` in {s}",
other.map_or_default(String::from)
));
}
}
}
Ok(out)
}
fn check_name(what: &str, name: &str) -> Result<(), String> {
if name.is_empty() {
return Err(format!("Model: the {what} name is empty"));
}
if name.contains('\0') {
return Err(format!(
"Model: the {what} name {name:?} contains a NUL, which SQL cannot carry"
));
}
Ok(())
}
fn tokens_text(tokens: &[TokenTree]) -> String {
tokens.iter().cloned().collect::<TokenStream>().to_string()
}
fn parse_fields(body: TokenStream) -> Result<Vec<FieldDef>, String> {
let mut fields = Vec::new();
let mut chunk: Vec<TokenTree> = Vec::new();
let mut angle_depth = 0i32;
let mut prev_dash = false;
for tt in body {
match &tt {
TokenTree::Punct(p) if p.as_char() == '<' => {
angle_depth += 1;
chunk.push(tt);
}
TokenTree::Punct(p) if p.as_char() == '>' && !prev_dash => {
angle_depth = (angle_depth - 1).max(0);
chunk.push(tt);
}
TokenTree::Punct(p) if p.as_char() == ',' && angle_depth == 0 => {
if !chunk.is_empty() {
fields.push(parse_one(&chunk)?);
chunk.clear();
}
}
_ => chunk.push(tt),
}
prev_dash = matches!(
chunk.last(),
Some(TokenTree::Punct(p)) if p.as_char() == '-'
);
}
if !chunk.is_empty() {
fields.push(parse_one(&chunk)?);
}
Ok(fields)
}
fn is_blob_type(ty: &str) -> bool {
let last = ty.rsplit("::").next().unwrap_or(ty).trim();
last == "Blob"
}
fn parse_one(chunk: &[TokenTree]) -> Result<FieldDef, String> {
let mut i = 0;
let mut f = FieldDef::default();
let mut deferred: Vec<Vec<Vec<TokenTree>>> = Vec::new();
while i < chunk.len() {
match &chunk[i] {
TokenTree::Punct(p) if p.as_char() == '#' => {
if let Some(TokenTree::Group(g)) = chunk.get(i + 1) {
if let Some(items) = attr_items(g, "obs") {
for item in items {
match item.as_slice() {
[TokenTree::Ident(id)] if id.to_string() == "key" => f.key = true,
[TokenTree::Ident(id)] if id.to_string() == "skip" => {
f.skip = true;
}
other => {
return Err(format!(
"unknown #[obs] option: `{}` (supported: key, skip)",
tokens_text(other)
));
}
}
}
} else if let Some(items) = attr_items(g, "model") {
deferred.push(items);
}
i += 2;
continue;
}
return Err("malformed attribute on a field".into());
}
_ => break,
}
}
if let Some(TokenTree::Ident(id)) = chunk.get(i)
&& id.to_string() == "pub"
{
i += 1;
if let Some(TokenTree::Group(g)) = chunk.get(i)
&& g.delimiter() == Delimiter::Parenthesis
{
i += 1;
}
}
f.name = match chunk.get(i) {
Some(TokenTree::Ident(id)) => id.to_string(),
_ => return Err("expected a field name".into()),
};
i += 1;
match chunk.get(i) {
Some(TokenTree::Punct(p)) if p.as_char() == ':' => i += 1,
_ => return Err(format!("expected `:` after field `{}`", f.name)),
}
f.ty = tokens_text(&chunk[i..]);
if f.ty.is_empty() {
return Err(format!("field `{}` has no type", f.name));
}
f.lazy = is_blob_type(&f.ty);
for items in deferred {
parse_field_options(items, &mut f)?;
}
Ok(f)
}