Skip to main content

day_persistence_macros/
lib.rs

1// Copyright © The Daybrite Project
2// SPDX-License-Identifier: MPL-2.0
3
4//! `#[derive(Model)]`: the day-persistence derive (docs/persistence.md).
5//!
6//! `Model` implies `Observable` and adds the schema half: `impl day_persistence::Model` with
7//! the table name, column list, row↔struct mappers and the default row, typed column
8//! references for predicates (`Trip::name()`), and the relation accessors and wiring.
9//! `#[model(id)]` marks the key; field options are `column = "…"`, `unique`, `index`,
10//! `transient` (observable, never stored), `with = Codec`, `json`, `relation(…)` and
11//! `link(…)`; struct options are `table = "…"`, `external = "alias"`, `index("a", "b")` for
12//! composites, `fts(…)` and `spatial(…)`. `#[obs(skip)]` leaves a field out of both halves: a
13//! field the change log cannot name could never mark its row dirty, so persisting it would
14//! silently lose edits.
15//!
16//! The observable half is not generated here. The derive describes the struct to day-macros'
17//! `observable_impls!` (reached as `day_persistence::__observable_impls!`), which emits exactly
18//! what `#[derive(Observable)]` would for the same fields. That one macro is the whole
19//! contract between this derive and the framework's: this crate owns the `#[model(…)]` grammar
20//! and the schema emission, day-macros owns the accessors, and neither reads the other's
21//! attributes.
22//!
23//! Generated paths say `day_persistence::…` and `day_model::…` unqualified. An app that derives
24//! `Model` depends on day-persistence directly, which resolves the first; `use
25//! day::prelude::*` with the facade's `model` feature (or a direct day-model dependency)
26//! resolves the second.
27//!
28//! Same construction as day-macros, with neither syn nor quote. A derive that needs field
29//! names and type tokens never has to understand a type, only re-emit it.
30
31use proc_macro::{Delimiter, TokenStream, TokenTree};
32use std::fmt::Write as _;
33
34/// A persistable model: everything `#[derive(Observable)]` generates, plus `impl
35/// day_persistence::Model`: table name, column list, row↔struct mappers and the default row.
36/// See day-persistence's crate docs and `docs/persistence.md`.
37///
38/// `#[model(id)]` marks the key (it is `#[obs(key)]` too). Field options: `column = "…"`,
39/// `unique`, `index`, `transient` (observable, never stored), `with = Codec`, `json`. Struct
40/// options: `table = "…"` (`snake_cased` struct name otherwise), `index("a", "b")` for
41/// composites. `#[obs(skip)]` removes a field from both halves.
42#[proc_macro_derive(Model, attributes(model, obs))]
43pub fn model(input: TokenStream) -> TokenStream {
44    finish(expand_model(input))
45}
46
47fn finish(result: Result<String, String>) -> TokenStream {
48    match result {
49        Ok(out) => out
50            .parse()
51            .unwrap_or_else(|e| panic!("generated code did not parse: {e}")),
52        Err(msg) => format!("compile_error!({msg:?});")
53            .parse()
54            .expect("compile_error! literal always parses"),
55    }
56}
57
58#[derive(Default)]
59struct FieldDef {
60    name: String,
61    ty: String,
62    key: bool,
63    skip: bool,
64    // The #[model(…)] half; inert under a bare Observable.
65    column: Option<String>,
66    unique: bool,
67    indexed: bool,
68    transient: bool,
69    with: Option<String>,
70    json: bool,
71    /// A `Blob` field: a lazy BLOB column, never read by a row fault, written only when it
72    /// carries pending bytes (docs/persistence.md, "Lazy blob columns").
73    lazy: bool,
74    /// `#[model(relation(…))]`: a `Many<T>` marker field. It gets no column or plain accessor;
75    /// the Model derive emits a `RelationRef` accessor and the wiring instead.
76    relation: Option<RelationAttr>,
77    /// `#[model(link(…))]`: a `Linked<T>` marker field, a relation by value rather than by
78    /// key, across databases (docs/persistence.md). It gets no column or accessor; the Model
79    /// derive emits the predicate builder and the `LinkDef`.
80    link: Option<LinkAttr>,
81}
82
83/// The parsed `link(target = T, local = "column", remote = "column")`.
84struct LinkAttr {
85    target: String,
86    local: String,
87    remote: String,
88}
89
90/// The parsed `relation(target = T, inverse = "…", delete = "…", ordered = "…", join = "…")`.
91struct RelationAttr {
92    target: String,
93    inverse: Option<String>,
94    delete: String,
95    ordered: Option<String>,
96    join: Option<String>,
97}
98
99impl FieldDef {
100    fn column_name(&self) -> &str {
101        self.column.as_deref().unwrap_or(&self.name)
102    }
103    fn persisted(&self) -> bool {
104        !self.skip && !self.transient && self.relation.is_none() && self.link.is_none()
105    }
106    fn nullable(&self) -> bool {
107        self.ty == "Option" || self.ty.starts_with("Option <")
108    }
109}
110
111struct StructDef {
112    name: String,
113    table: Option<String>,
114    /// `external = "alias"`: the table lives in a database `ATTACHed` under `alias`, managed by
115    /// someone else, so it is read-only and gets neither DDL nor a migration.
116    external: Option<String>,
117    composites: Vec<Vec<String>>,
118    /// `unique("a", "b")`: multi-column UNIQUE constraints, as unique indexes.
119    uniques: Vec<Vec<String>>,
120    fts: Vec<String>,
121    fts_tokenize: Option<String>,
122    spatial: Option<(String, String)>,
123    fields: Vec<FieldDef>,
124}
125
126fn expand_model(input: TokenStream) -> Result<String, String> {
127    let def = parse_struct(input)?;
128    let keys: Vec<&FieldDef> = def.fields.iter().filter(|f| f.key).collect();
129    let key = match keys.as_slice() {
130        [k] => *k,
131        [] => {
132            return Err(format!(
133                "Model: `{}` needs exactly one field marked #[model(id)]",
134                def.name
135            ));
136        }
137        _ => {
138            return Err(format!(
139                "Model: `{}` marks more than one field as the id",
140                def.name
141            ));
142        }
143    };
144    if key.transient || key.skip {
145        return Err(format!(
146            "Model: `{}`'s id field cannot be transient or skipped",
147            def.name
148        ));
149    }
150    if key.with.is_some() || key.json {
151        // The key's stored form must match its `Key` form: the fold's WHERE parameters and
152        // external-merge decoding both derive it from the key kind, not from a codec.
153        return Err(format!(
154            "Model: `{}`'s id field takes no codec — integer, Uuid and String keys store \
155             in their canonical forms",
156            def.name
157        ));
158    }
159
160    let mut out = observable_impls(&def);
161    out.push_str(&emit_model(&def, key)?);
162    Ok(out)
163}
164
165// ---------------------------------------------------------------------------
166// Emission
167// ---------------------------------------------------------------------------
168
169/// The observable half, handed to day-macros: the struct restated with only what `Observable`
170/// reads. The key carries `#[obs(key)]`; skipped fields are left out, and so are relation and
171/// link markers, which observe through their `RelationRef` accessor (emitted below) rather
172/// than a plain `Field`: a `Many` holds nothing a `Field` could usefully bind.
173fn observable_impls(def: &StructDef) -> String {
174    let mut out = format!(
175        "day_persistence::__observable_impls! {{\n    struct {} {{\n",
176        def.name
177    );
178    for f in def
179        .fields
180        .iter()
181        .filter(|f| !f.skip && f.relation.is_none() && f.link.is_none())
182    {
183        let key = if f.key { "#[obs(key)] " } else { "" };
184        let _ = writeln!(out, "        {key}{}: {},", f.name, f.ty);
185    }
186    out.push_str("    }\n}\n");
187    out
188}
189
190/// The schema half. Every generated expression goes through `day_persistence::` paths and the
191/// field's type: the derive never guesses a SQL type, it asks `ColumnValue::SQL_TYPE` (or
192/// the named codec's) at compile time.
193fn emit_model(def: &StructDef, key: &FieldDef) -> Result<String, String> {
194    let name = &def.name;
195    let bare_table = def.table.clone().unwrap_or_else(|| snake_case(name));
196    // An external table is addressed through its database alias everywhere SQL names it
197    // (the fault SELECT, predicates, the FTS shadow `alias.table_fts`), which is what one
198    // qualified `TABLE` constant gives every existing code path at once.
199    let table = match &def.external {
200        Some(alias) => format!("{alias}.{bare_table}"),
201        None => bare_table.clone(),
202    };
203    for f in def.fields.iter().filter(|f| f.lazy) {
204        if f.key {
205            return Err(format!(
206                "Model: `{name}`'s id field `{}` cannot be a Blob",
207                f.name
208            ));
209        }
210        if f.with.is_some() || f.json {
211            return Err(format!(
212                "Model: Blob field `{}` of `{name}` takes no codec; it is stored as it is",
213                f.name
214            ));
215        }
216        if !f.persisted() {
217            return Err(format!(
218                "Model: Blob field `{}` of `{name}` cannot be transient or a relation marker",
219                f.name
220            ));
221        }
222    }
223    if let Some(f) = def.fields.iter().find(|f| {
224        f.nullable() && is_blob_type(f.ty.trim_start_matches("Option <").trim_end_matches('>'))
225    }) {
226        return Err(format!(
227            "Model: field `{}` of `{name}` is `Option<Blob>`; a Blob column is nullable by \
228             itself (a NULL reads as empty), so declare it as `Blob`",
229            f.name
230        ));
231    }
232    // Lazy columns go last in the table: SQLite keeps a large value in an overflow chain, and
233    // a select that stops at an earlier column never reads it. COLUMNS, to_row and from_row
234    // all follow this order, so the struct's field order is free.
235    let persisted: Vec<&FieldDef> = def
236        .fields
237        .iter()
238        .filter(|f| f.persisted() && !f.lazy)
239        .chain(def.fields.iter().filter(|f| f.persisted() && f.lazy))
240        .collect();
241    for f in def.fields.iter().filter(|f| f.link.is_some()) {
242        let l = f.link.as_ref().expect("filtered");
243        if !persisted.iter().any(|p| p.column_name() == l.local) {
244            return Err(format!(
245                "Model: link(…) on `{}` names local = {:?}, which is not a persisted column of `{name}`",
246                f.name, l.local
247            ));
248        }
249    }
250
251    // Names reach SQL quoted, so any string is a table or column name here; the two things
252    // SQLite cannot take are an empty name and a NUL (which ends the statement text early).
253    check_name("table", &bare_table)?;
254    for f in &persisted {
255        check_name(&format!("column of field `{}`", f.name), f.column_name())?;
256    }
257    for (i, f) in persisted.iter().enumerate() {
258        let twin = persisted[..i]
259            .iter()
260            .find(|p| p.column_name().eq_ignore_ascii_case(f.column_name()));
261        if let Some(twin) = twin {
262            return Err(format!(
263                "Model: fields `{}` and `{}` of `{name}` both store in column {:?} (SQLite \
264                 compares column names without case)",
265                twin.name,
266                f.name,
267                f.column_name()
268            ));
269        }
270    }
271
272    let mut columns = String::new();
273    for f in &persisted {
274        let sql = sql_type_expr(f);
275        let _ = writeln!(
276            columns,
277            "        day_persistence::ColumnDef {{ name: {:?}, field: \"{}\", sql: {sql}, \
278             not_null: {}, unique: {}, indexed: {}, lazy: {} }},",
279            f.column_name(),
280            f.name,
281            !f.nullable() && !f.lazy,
282            f.unique,
283            f.indexed,
284            f.lazy,
285        );
286    }
287
288    // fts()/spatial() name COLUMNS; a typo would silently index nothing, so check now.
289    let column_names: Vec<&str> = persisted.iter().map(|f| f.column_name()).collect();
290    for c in &def.fts {
291        if !column_names.contains(&c.as_str()) {
292            return Err(format!(
293                "Model: fts(…) names `{c}`, which is not a persisted column of `{name}`"
294            ));
295        }
296    }
297    for (what, lists) in [("index", &def.composites), ("unique", &def.uniques)] {
298        for cols in lists {
299            for c in cols {
300                if !column_names.contains(&c.as_str()) {
301                    return Err(format!(
302                        "Model: {what}(…) names `{c}`, which is not a persisted column of `{name}`"
303                    ));
304                }
305            }
306        }
307    }
308    if let Some((lat, lon)) = &def.spatial {
309        for c in [lat, lon] {
310            if !column_names.contains(&c.as_str()) {
311                return Err(format!(
312                    "Model: spatial(…) names `{c}`, which is not a persisted column of `{name}`"
313                ));
314            }
315        }
316    }
317
318    let uniques = def
319        .uniques
320        .iter()
321        .map(|cols| {
322            format!(
323                "&[{}]",
324                cols.iter()
325                    .map(|c| format!("{c:?}"))
326                    .collect::<Vec<_>>()
327                    .join(", ")
328            )
329        })
330        .collect::<Vec<_>>()
331        .join(", ");
332    let composites = def
333        .composites
334        .iter()
335        .map(|cols| {
336            format!(
337                "&[{}]",
338                cols.iter()
339                    .map(|c| format!("{c:?}"))
340                    .collect::<Vec<_>>()
341                    .join(", ")
342            )
343        })
344        .collect::<Vec<_>>()
345        .join(", ");
346
347    let mut to_row = String::new();
348    for f in &persisted {
349        let _ = writeln!(to_row, "            {},", encode_expr(f));
350    }
351
352    let mut from_row = String::new();
353    for f in &def.fields {
354        if let Some(i) = persisted.iter().position(|p| p.name == f.name) {
355            let _ = writeln!(from_row, "            {}: {},", f.name, decode_expr(f, i));
356        } else {
357            let _ = writeln!(
358                from_row,
359                "            {}: ::core::default::Default::default(),",
360                f.name
361            );
362        }
363    }
364
365    // Typed column refs: `Trip::name()` in a predicate, beside `trip.name()` the binding.
366    // Inherent fns without a receiver never collide with the Fields trait's methods (which
367    // take self): method-call syntax finds the trait, path syntax finds these.
368    let mut cols = String::new();
369    for f in &persisted {
370        let encode = match codec(f) {
371            Some(codec) => format!(
372                "<{codec} as day_persistence::ValueCodec<{}>>::to_sqlite_value",
373                f.ty
374            ),
375            None => format!("day_persistence::encode_column::<{}>", f.ty),
376        };
377        let _ = write!(
378            cols,
379            "    pub fn {}() -> day_persistence::Col<{}> {{\n\
380             \x20       day_persistence::Col::new({:?}, \"{}\", {table:?}, {encode})\n\
381             \x20   }}\n",
382            f.name,
383            f.ty,
384            f.column_name(),
385            f.name,
386        );
387    }
388    // The static relation reference, for predicates. The instance accessor of the same name
389    // lives on the Relations trait and takes `self`, so path syntax finds this one and
390    // method-call syntax finds that one, the rule `Trip::name()` already relies on.
391    for f in def.fields.iter().filter(|f| f.relation.is_some()) {
392        let r = f.relation.as_ref().expect("filtered");
393        let _ = write!(
394            cols,
395            "    pub fn {}() -> day_persistence::RelationCol<{name}, {}> {{\n\
396             \x20       day_persistence::RelationCol::new(\"{}\", {table:?}, \
397             <{} as day_persistence::Model>::TABLE)\n\
398             \x20   }}\n",
399            f.name, r.target, f.name, r.target,
400        );
401    }
402    // A link's predicate builder is a plain RelationCol: the container resolves the
403    // (owner, field) pair to the link's join condition at compile time, so `any`/`none`/
404    // `is_empty`/`count_ge` read exactly as they do across a keyed relation.
405    for f in def.fields.iter().filter(|f| f.link.is_some()) {
406        let l = f.link.as_ref().expect("filtered");
407        let _ = write!(
408            cols,
409            "    pub fn {}() -> day_persistence::RelationCol<{name}, {}> {{\n\
410             \x20       day_persistence::RelationCol::new(\"{}\", {table:?}, \
411             <{} as day_persistence::Model>::TABLE)\n\
412             \x20   }}\n",
413            f.name, l.target, f.name, l.target,
414        );
415    }
416    if !def.fts.is_empty() {
417        let _ = write!(
418            cols,
419            "    pub fn fts() -> day_persistence::FtsRef {{\n\
420             \x20       day_persistence::FtsRef {{ columns: <{name} as day_persistence::Model>::FTS_COLUMNS }}\n\
421             \x20   }}\n"
422        );
423    }
424    if let Some((lat, lon)) = &def.spatial {
425        let _ = write!(
426            cols,
427            "    pub fn geo() -> day_persistence::GeoRef {{\n\
428             \x20       day_persistence::GeoRef {{ lat: {lat:?}, lon: {lon:?} }}\n\
429             \x20   }}\n"
430        );
431    }
432    let col_impl = format!("#[allow(dead_code)]\nimpl {name} {{\n{cols}}}\n");
433
434    // Relations: the const table, the wiring, and the RelationRef accessor trait.
435    let rels: Vec<(&FieldDef, &RelationAttr)> = def
436        .fields
437        .iter()
438        .filter_map(|f| f.relation.as_ref().map(|r| (f, r)))
439        .collect();
440    let delete_expr = |d: &str| match d {
441        "cascade" => "day_persistence::DeleteRule::Cascade",
442        "deny" => "day_persistence::DeleteRule::Deny",
443        _ => "day_persistence::DeleteRule::Nullify",
444    };
445    let opt_str = |o: &Option<String>| match o {
446        Some(s) => format!("Some({s:?})"),
447        None => "None".into(),
448    };
449    #[expect(
450        clippy::format_collect,
451        reason = "the derive emits source text, and each item reads as the template it produces"
452    )]
453    let relations_const = if rels.is_empty() {
454        String::new()
455    } else {
456        format!(
457            "    const RELATIONS: &'static [day_persistence::RelationDef] = &[\n{}    ];\n",
458            rels.iter()
459                .map(|(f, r)| format!(
460                    "        day_persistence::RelationDef {{ field: \"{}\", target_table: \
461                     <{} as day_persistence::Model>::TABLE, inverse: {:?}, delete: {}, \
462                     ordered: {}, join: {} }},\n",
463                    f.name,
464                    r.target,
465                    r.inverse.as_deref().unwrap_or(""),
466                    delete_expr(&r.delete),
467                    opt_str(&r.ordered),
468                    opt_str(&r.join),
469                ))
470                .collect::<String>()
471        )
472    };
473    let wire_fn = if rels.is_empty() {
474        String::new()
475    } else {
476        format!(
477            "    fn wire(reg: &mut day_persistence::Registrar<'_>) {{\n{}    }}\n",
478            rels.iter()
479                .map(|(f, r)| match (&r.join, &r.inverse) {
480                    (Some(join), _) => format!(
481                        "        day_persistence::wire_join::<Self, {}>(reg, \"{}\", \
482                         {join:?}, {}, {});\n",
483                        r.target,
484                        f.name,
485                        opt_str(&r.ordered),
486                        delete_expr(&r.delete),
487                    ),
488                    (None, Some(inverse)) => format!(
489                        "        day_persistence::wire_to_many::<Self, {}>(reg, \"{}\", \
490                         {inverse:?}, {}, {});\n",
491                        r.target,
492                        f.name,
493                        delete_expr(&r.delete),
494                        opt_str(&r.ordered),
495                    ),
496                    (None, None) => String::new(), // refused at parse time
497                })
498                .collect::<String>()
499        )
500    };
501    #[expect(
502        clippy::format_collect,
503        reason = "the derive emits source text, and each item reads as the template it produces"
504    )]
505    let rel_trait = if rels.is_empty() {
506        String::new()
507    } else {
508        format!(
509            "#[allow(non_camel_case_types)]\n\
510             pub trait {name}Relations: day_model::Source<{name}> + Sized {{\n{}}}\n\
511             impl<S: day_model::Source<{name}>> {name}Relations for S {{}}\n",
512            rels.iter()
513                .map(|(f, r)| format!(
514                    "    fn {}(self) -> day_persistence::RelationRef<Self, {name}, {}> {{\n\
515                     \x20       day_persistence::RelationRef::new(day_model::project(\
516                     self, \"{}\", |s| &s.{}, |s| &mut s.{}))\n\
517                     \x20   }}\n",
518                    f.name, r.target, f.name, f.name, f.name
519                ))
520                .collect::<String>()
521        )
522    };
523
524    let fts_const = if def.fts.is_empty() {
525        String::new()
526    } else {
527        let tokenize = match &def.fts_tokenize {
528            Some(t) => format!("    const FTS_TOKENIZE: Option<&'static str> = Some({t:?});\n"),
529            None => String::new(),
530        };
531        format!(
532            "    const FTS_COLUMNS: &'static [&'static str] = &[{}];\n{tokenize}",
533            def.fts
534                .iter()
535                .map(|c| format!("{c:?}"))
536                .collect::<Vec<_>>()
537                .join(", ")
538        )
539    };
540    let links: Vec<(&FieldDef, &LinkAttr)> = def
541        .fields
542        .iter()
543        .filter_map(|f| f.link.as_ref().map(|l| (f, l)))
544        .collect();
545    #[expect(
546        clippy::format_collect,
547        reason = "the derive emits source text, and each item reads as the template it produces"
548    )]
549    let links_const = if links.is_empty() {
550        String::new()
551    } else {
552        format!(
553            "    const LINKS: &'static [day_persistence::LinkDef] = &[\n{}    ];\n",
554            links
555                .iter()
556                .map(|(f, l)| format!(
557                    "        day_persistence::LinkDef {{ field: \"{}\", local: {:?}, \
558                     target_table: <{} as day_persistence::Model>::TABLE, remote: {:?} }},\n",
559                    f.name, l.local, l.target, l.remote
560                ))
561                .collect::<String>()
562        )
563    };
564    let external_const = match &def.external {
565        Some(alias) => format!(
566            "    const EXTERNAL: bool = true;\n    const SCHEMA: Option<&'static str> = Some({alias:?});\n"
567        ),
568        None => String::new(),
569    };
570    let spatial_const = match &def.spatial {
571        None => String::new(),
572        Some((lat, lon)) => format!(
573            "    const SPATIAL: Option<day_persistence::SpatialCols> = \
574             Some(day_persistence::SpatialCols {{ lat: {lat:?}, lon: {lon:?} }});\n"
575        ),
576    };
577
578    let lazy_fields: Vec<&FieldDef> = persisted.iter().copied().filter(|f| f.lazy).collect();
579    #[expect(
580        clippy::format_collect,
581        reason = "the derive emits source text, and each item reads as the template it produces"
582    )]
583    let lazy_const = if lazy_fields.is_empty() {
584        String::new()
585    } else {
586        format!(
587            "    const LAZY_FIELDS: &'static [&'static str] = &[{}];\n\
588             \x20   fn settle_lazy(&mut self) -> Vec<(&'static str, ::std::sync::Arc<Vec<u8>>)> {{\n\
589             \x20       let mut out = Vec::new();\n{}\
590             \x20       out\n\
591             \x20   }}\n",
592            lazy_fields
593                .iter()
594                .map(|f| format!("\"{}\"", f.name))
595                .collect::<Vec<_>>()
596                .join(", "),
597            lazy_fields
598                .iter()
599                .map(|f| format!(
600                    "        if let Some(b) = self.{}.take_pending() {{ out.push((\"{}\", b)); }}\n",
601                    f.name, f.name
602                ))
603                .collect::<String>(),
604        )
605    };
606
607    let key_sql = sql_type_expr(key);
608    Ok(col_impl
609        + &rel_trait
610        + &format!(
611            "impl day_persistence::Model for {name} {{\n\
612         \x20   const TABLE: &'static str = {table:?};\n\
613         \x20   const KEY: &'static str = {key_col:?};\n\
614         \x20   const KEY_SQL: day_persistence::SqlType = {key_sql};\n\
615         \x20   const COLUMNS: &'static [day_persistence::ColumnDef] = &[\n{columns}    ];\n\
616         \x20   const COMPOSITE_INDEXES: &'static [&'static [&'static str]] = &[{composites}];\n\
617         \x20   const UNIQUE_INDEXES: &'static [&'static [&'static str]] = &[{uniques}];\n\
618         {fts_const}{spatial_const}{relations_const}{links_const}{external_const}{lazy_const}\
619         \x20   fn to_row(&self) -> Vec<day_persistence::Value> {{\n\
620         \x20       vec![\n{to_row}        ]\n\
621         \x20   }}\n\
622         \x20   fn from_row(row: &dyn day_persistence::Row) -> \
623         Result<Self, day_persistence::DbError> {{\n\
624         \x20       Ok(Self {{\n{from_row}        }})\n\
625         \x20   }}\n\
626         \x20   fn default_row() -> Vec<day_persistence::Value> {{\n\
627         \x20       day_persistence::Model::to_row(&<Self as ::core::default::Default>::default())\n\
628         \x20   }}\n\
629         {wire_fn}\
630         }}\n",
631            key_col = key.column_name(),
632        ))
633}
634
635fn sql_type_expr(f: &FieldDef) -> String {
636    match codec(f) {
637        Some(codec) => format!(
638            "<{codec} as day_persistence::ValueCodec<{}>>::SQL_TYPE",
639            f.ty
640        ),
641        None => format!("<{} as day_persistence::ColumnValue>::SQL_TYPE", f.ty),
642    }
643}
644
645fn encode_expr(f: &FieldDef) -> String {
646    match codec(f) {
647        Some(codec) => format!(
648            "<{codec} as day_persistence::ValueCodec<{}>>::to_sqlite_value(&self.{})",
649            f.ty, f.name
650        ),
651        None => format!(
652            "day_persistence::ColumnValue::to_sqlite_value(&self.{})",
653            f.name
654        ),
655    }
656}
657
658/// A stored NULL decodes as the field's `Default`, which is what makes an added column's old
659/// rows readable before their backfill, matching the model's deleted-row semantics.
660fn decode_expr(f: &FieldDef, i: usize) -> String {
661    let read = format!("day_persistence::Row::get(row, {i}usize)");
662    match codec(f) {
663        Some(codec) => format!(
664            "match {read} {{\n\
665             \x20               day_persistence::Value::Null => ::core::default::Default::default(),\n\
666             \x20               v => <{codec} as day_persistence::ValueCodec<{}>>::from_sqlite_value(v)?,\n\
667             \x20           }}",
668            f.ty
669        ),
670        None => format!(
671            "match {read} {{\n\
672             \x20               day_persistence::Value::Null => ::core::default::Default::default(),\n\
673             \x20               v => <{} as day_persistence::ColumnValue>::from_sqlite_value(v)?,\n\
674             \x20           }}",
675            f.ty
676        ),
677    }
678}
679
680fn codec(f: &FieldDef) -> Option<String> {
681    if let Some(with) = &f.with {
682        Some(with.clone())
683    } else if f.json {
684        Some("day_persistence::Json".into())
685    } else {
686        None
687    }
688}
689
690fn snake_case(name: &str) -> String {
691    let mut out = String::with_capacity(name.len() + 4);
692    for (i, ch) in name.chars().enumerate() {
693        if ch.is_uppercase() {
694            if i > 0 {
695                out.push('_');
696            }
697            out.extend(ch.to_lowercase());
698        } else {
699            out.push(ch);
700        }
701    }
702    out
703}
704
705// ---------------------------------------------------------------------------
706// Parsing
707// ---------------------------------------------------------------------------
708
709/// The whole derive input: struct-level `#[model(…)]` options, the name, and the fields.
710fn parse_struct(input: TokenStream) -> Result<StructDef, String> {
711    let tokens: Vec<TokenTree> = input.into_iter().collect();
712
713    let mut opts = StructOptions::default();
714    let mut i = 0;
715    while i < tokens.len() {
716        match &tokens[i] {
717            TokenTree::Ident(id) if id.to_string() == "struct" => break,
718            TokenTree::Punct(p) if p.as_char() == '#' => {
719                if let Some(TokenTree::Group(g)) = tokens.get(i + 1) {
720                    if let Some(items) = attr_items(g, "model") {
721                        parse_struct_options(items, &mut opts)?;
722                    }
723                    i += 2;
724                    continue;
725                }
726                return Err("malformed attribute on the struct".into());
727            }
728            _ => i += 1,
729        }
730    }
731    if i >= tokens.len() {
732        return Err("the derive expects a struct".into());
733    }
734    let name = match tokens.get(i + 1) {
735        Some(TokenTree::Ident(id)) => id.to_string(),
736        _ => return Err("the derive expects a named struct".into()),
737    };
738    let body = match tokens.get(i + 2) {
739        Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace => g.stream(),
740        Some(TokenTree::Punct(p)) if p.as_char() == '<' => {
741            return Err("the derive does not support generic structs yet".into());
742        }
743        _ => return Err("the derive expects a struct with named fields".into()),
744    };
745
746    let StructOptions {
747        table,
748        external,
749        composites,
750        uniques,
751        fts,
752        fts_tokenize,
753        spatial,
754    } = opts;
755    Ok(StructDef {
756        name,
757        table,
758        external,
759        composites,
760        uniques,
761        fts,
762        fts_tokenize,
763        spatial,
764        fields: parse_fields(body)?,
765    })
766}
767
768/// If the bracket group is `name(…)`, return the paren group's items split on top-level commas.
769fn attr_items(bracket: &proc_macro::Group, name: &str) -> Option<Vec<Vec<TokenTree>>> {
770    let inner: Vec<TokenTree> = bracket.stream().into_iter().collect();
771    match (inner.first(), inner.get(1)) {
772        (Some(TokenTree::Ident(id)), Some(TokenTree::Group(g)))
773            if id.to_string() == name && g.delimiter() == Delimiter::Parenthesis =>
774        {
775            let mut items = vec![Vec::new()];
776            for tt in g.stream() {
777                match &tt {
778                    TokenTree::Punct(p) if p.as_char() == ',' => items.push(Vec::new()),
779                    _ => items.last_mut().expect("never empty").push(tt),
780                }
781            }
782            items.retain(|item| !item.is_empty());
783            Some(items)
784        }
785        _ => None,
786    }
787}
788
789/// The struct-level `#[model(…)]` options, as `parse_struct_options` fills them.
790#[derive(Default)]
791struct StructOptions {
792    table: Option<String>,
793    external: Option<String>,
794    composites: Vec<Vec<String>>,
795    uniques: Vec<Vec<String>>,
796    fts: Vec<String>,
797    fts_tokenize: Option<String>,
798    spatial: Option<(String, String)>,
799}
800
801fn parse_struct_options(
802    items: Vec<Vec<TokenTree>>,
803    opts: &mut StructOptions,
804) -> Result<(), String> {
805    let StructOptions {
806        table,
807        external,
808        composites,
809        uniques,
810        fts,
811        fts_tokenize,
812        spatial,
813    } = opts;
814    for item in items {
815        match item.as_slice() {
816            [TokenTree::Ident(id), TokenTree::Group(g)]
817                if id.to_string() == "fts" && g.delimiter() == Delimiter::Parenthesis =>
818            {
819                // Column literals, plus an optional `tokenize = "…"` naming the FTS5
820                // tokenizer (e.g. "unicode61 remove_diacritics 2").
821                let mut fts_items = vec![Vec::new()];
822                for tt in g.stream() {
823                    match &tt {
824                        TokenTree::Punct(p) if p.as_char() == ',' => fts_items.push(Vec::new()),
825                        _ => fts_items.last_mut().expect("never empty").push(tt.clone()),
826                    }
827                }
828                for item in fts_items.into_iter().filter(|i| !i.is_empty()) {
829                    match item.as_slice() {
830                        [TokenTree::Literal(lit)] => fts.push(string_literal(lit)?),
831                        [
832                            TokenTree::Ident(k),
833                            TokenTree::Punct(eq),
834                            TokenTree::Literal(lit),
835                        ] if k.to_string() == "tokenize" && eq.as_char() == '=' => {
836                            *fts_tokenize = Some(string_literal(lit)?);
837                        }
838                        other => {
839                            return Err(format!(
840                                "fts(…) expects column string literals and an optional \
841                                 tokenize = \"…\", found `{}`",
842                                tokens_text(other)
843                            ));
844                        }
845                    }
846                }
847            }
848            [TokenTree::Ident(id), TokenTree::Group(g)]
849                if id.to_string() == "spatial" && g.delimiter() == Delimiter::Parenthesis =>
850            {
851                let mut lat = None;
852                let mut lon = None;
853                let inner: Vec<TokenTree> = g.stream().into_iter().collect();
854                let mut j = 0;
855                while j + 2 < inner.len() + 1 {
856                    match (&inner.get(j), &inner.get(j + 1), &inner.get(j + 2)) {
857                        (
858                            Some(TokenTree::Ident(k)),
859                            Some(TokenTree::Punct(eq)),
860                            Some(TokenTree::Literal(lit)),
861                        ) if eq.as_char() == '=' => {
862                            match k.to_string().as_str() {
863                                "lat" => lat = Some(string_literal(lit)?),
864                                "lon" => lon = Some(string_literal(lit)?),
865                                other => {
866                                    return Err(format!(
867                                        "spatial(…) takes lat = \"…\" and lon = \"…\", found `{other}`"
868                                    ));
869                                }
870                            }
871                            j += 3;
872                            if matches!(inner.get(j), Some(TokenTree::Punct(p)) if p.as_char() == ',')
873                            {
874                                j += 1;
875                            }
876                        }
877                        _ => return Err("spatial(…) takes lat = \"…\" and lon = \"…\"".into()),
878                    }
879                }
880                match (lat, lon) {
881                    (Some(lat), Some(lon)) => *spatial = Some((lat, lon)),
882                    _ => return Err("spatial(…) needs BOTH lat = \"…\" and lon = \"…\"".into()),
883                }
884            }
885            [
886                TokenTree::Ident(id),
887                TokenTree::Punct(eq),
888                TokenTree::Literal(lit),
889            ] if id.to_string() == "table" && eq.as_char() == '=' => {
890                *table = Some(string_literal(lit)?);
891            }
892            [
893                TokenTree::Ident(id),
894                TokenTree::Punct(eq),
895                TokenTree::Literal(lit),
896            ] if id.to_string() == "external" && eq.as_char() == '=' => {
897                let alias = string_literal(lit)?;
898                if alias.is_empty() || !alias.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
899                {
900                    return Err(format!(
901                        "external = {alias:?} must be a plain SQL schema name (letters, digits, `_`)"
902                    ));
903                }
904                *external = Some(alias);
905            }
906            [TokenTree::Ident(id), TokenTree::Group(g)]
907                if id.to_string() == "index" && g.delimiter() == Delimiter::Parenthesis =>
908            {
909                let mut cols = Vec::new();
910                for tt in g.stream() {
911                    match &tt {
912                        TokenTree::Literal(lit) => cols.push(string_literal(lit)?),
913                        TokenTree::Punct(p) if p.as_char() == ',' => {}
914                        other => {
915                            return Err(format!(
916                                "index(…) expects string literals, found `{other}`"
917                            ));
918                        }
919                    }
920                }
921                composites.push(cols);
922            }
923            [TokenTree::Ident(id), TokenTree::Group(g)]
924                if id.to_string() == "unique" && g.delimiter() == Delimiter::Parenthesis =>
925            {
926                let mut cols = Vec::new();
927                for tt in g.stream() {
928                    match &tt {
929                        TokenTree::Literal(lit) => cols.push(string_literal(lit)?),
930                        TokenTree::Punct(p) if p.as_char() == ',' => {}
931                        other => {
932                            return Err(format!(
933                                "unique(…) expects string literals, found `{other}`"
934                            ));
935                        }
936                    }
937                }
938                if cols.is_empty() {
939                    return Err("unique(…) names at least one column".into());
940                }
941                uniques.push(cols);
942            }
943            other => {
944                return Err(format!(
945                    "unknown #[model] option on the struct: `{}` (supported: table = \"…\", external = \"alias\", index(\"a\", \"b\"), unique(\"a\", \"b\"), fts(\"a\", \"b\"), spatial(lat = \"…\", lon = \"…\"))",
946                    tokens_text(other)
947                ));
948            }
949        }
950    }
951    Ok(())
952}
953
954fn parse_field_options(items: Vec<Vec<TokenTree>>, f: &mut FieldDef) -> Result<(), String> {
955    for item in items {
956        match item.as_slice() {
957            [TokenTree::Ident(id)] => match id.to_string().as_str() {
958                "id" => f.key = true,
959                "unique" => f.unique = true,
960                "index" => f.indexed = true,
961                "transient" => f.transient = true,
962                "json" => f.json = true,
963                other => {
964                    return Err(format!(
965                        "unknown #[model] option on field `{}`: `{other}` (supported: id, unique, index, transient, json, column = \"…\", with = Codec)",
966                        f.name
967                    ));
968                }
969            },
970            [
971                TokenTree::Ident(id),
972                TokenTree::Punct(eq),
973                TokenTree::Literal(lit),
974            ] if id.to_string() == "column" && eq.as_char() == '=' => {
975                f.column = Some(string_literal(lit)?);
976            }
977            [TokenTree::Ident(id), TokenTree::Punct(eq), rest @ ..]
978                if id.to_string() == "with" && eq.as_char() == '=' && !rest.is_empty() =>
979            {
980                f.with = Some(tokens_text(rest));
981            }
982            [TokenTree::Ident(id), TokenTree::Group(g)]
983                if id.to_string() == "relation" && g.delimiter() == Delimiter::Parenthesis =>
984            {
985                f.relation = Some(parse_relation(g, &f.name)?);
986            }
987            [TokenTree::Ident(id), TokenTree::Group(g)]
988                if id.to_string() == "link" && g.delimiter() == Delimiter::Parenthesis =>
989            {
990                f.link = Some(parse_link(g, &f.name)?);
991            }
992            other => {
993                return Err(format!(
994                    "unknown #[model] option on field `{}`: `{}`",
995                    f.name,
996                    tokens_text(other)
997                ));
998            }
999        }
1000    }
1001    Ok(())
1002}
1003
1004/// `relation(target = Type, inverse = "field", delete = "…", ordered = "field", join = "table")`.
1005fn parse_relation(g: &proc_macro::Group, field: &str) -> Result<RelationAttr, String> {
1006    let mut items = vec![Vec::new()];
1007    for tt in g.stream() {
1008        match &tt {
1009            TokenTree::Punct(p) if p.as_char() == ',' => items.push(Vec::new()),
1010            _ => items.last_mut().expect("never empty").push(tt),
1011        }
1012    }
1013    items.retain(|i| !i.is_empty());
1014
1015    let (mut target, mut inverse, mut delete, mut ordered, mut join) =
1016        (None, None, None, None, None);
1017    for item in items {
1018        match item.as_slice() {
1019            [TokenTree::Ident(k), TokenTree::Punct(eq), rest @ ..]
1020                if eq.as_char() == '=' && !rest.is_empty() =>
1021            {
1022                match k.to_string().as_str() {
1023                    "target" => target = Some(tokens_text(rest)),
1024                    "inverse" => inverse = Some(literal_str(rest, field)?),
1025                    "delete" => delete = Some(literal_str(rest, field)?),
1026                    "ordered" => ordered = Some(literal_str(rest, field)?),
1027                    "join" => join = Some(literal_str(rest, field)?),
1028                    other => {
1029                        return Err(format!(
1030                            "relation(…) on `{field}`: unknown option `{other}` (supported: \
1031                             target = Type, inverse = \"field\", delete = \"…\", \
1032                             ordered = \"field\", join = \"table\")"
1033                        ));
1034                    }
1035                }
1036            }
1037            // Bare `ordered`, the join form: the position lives on the membership row, so
1038            // there is no child field to name (one child can sit at different positions
1039            // under different parents).
1040            [TokenTree::Ident(k)] if k.to_string() == "ordered" => {
1041                ordered = Some("position".into());
1042            }
1043            other => {
1044                return Err(format!(
1045                    "relation(…) on `{field}`: expected `name = …`, found `{}`",
1046                    tokens_text(other)
1047                ));
1048            }
1049        }
1050    }
1051    let Some(target) = target else {
1052        return Err(format!("relation(…) on `{field}` needs target = Type"));
1053    };
1054    let delete = delete.unwrap_or_else(|| "nullify".into());
1055    if !["nullify", "cascade", "deny"].contains(&delete.as_str()) {
1056        return Err(format!(
1057            "relation(…) on `{field}`: delete = {delete:?} — nullify, cascade, or deny"
1058        ));
1059    }
1060    if inverse.is_none() && join.is_none() {
1061        return Err(format!(
1062            "relation(…) on `{field}` needs inverse = \"field\" (the target's One<…> field) \
1063             — or join = \"table\" for a many-to-many"
1064        ));
1065    }
1066    Ok(RelationAttr {
1067        target,
1068        inverse,
1069        delete,
1070        ordered,
1071        join,
1072    })
1073}
1074
1075/// `link(target = Type, local = "column", remote = "column")` is a relation matched by value:
1076/// rows of `Type` whose `remote` column equals this row's `local` column. No foreign key and
1077/// no wiring, which is what lets it cross into an `external` table SQLite cannot constrain.
1078fn parse_link(g: &proc_macro::Group, field: &str) -> Result<LinkAttr, String> {
1079    let mut items = vec![Vec::new()];
1080    for tt in g.stream() {
1081        match &tt {
1082            TokenTree::Punct(p) if p.as_char() == ',' => items.push(Vec::new()),
1083            _ => items.last_mut().expect("never empty").push(tt),
1084        }
1085    }
1086    items.retain(|i| !i.is_empty());
1087    let (mut target, mut local, mut remote) = (None, None, None);
1088    for item in items {
1089        match item.as_slice() {
1090            [TokenTree::Ident(k), TokenTree::Punct(eq), rest @ ..]
1091                if eq.as_char() == '=' && !rest.is_empty() =>
1092            {
1093                match k.to_string().as_str() {
1094                    "target" => target = Some(tokens_text(rest)),
1095                    "local" => local = Some(literal_str(rest, field)?),
1096                    "remote" => remote = Some(literal_str(rest, field)?),
1097                    other => {
1098                        return Err(format!(
1099                            "link(…) on `{field}`: unknown option `{other}` (supported: \
1100                             target = Type, local = \"column\", remote = \"column\")"
1101                        ));
1102                    }
1103                }
1104            }
1105            other => {
1106                return Err(format!(
1107                    "link(…) on `{field}`: expected `name = …`, found `{}`",
1108                    tokens_text(other)
1109                ));
1110            }
1111        }
1112    }
1113    match (target, local, remote) {
1114        (Some(target), Some(local), Some(remote)) => Ok(LinkAttr {
1115            target,
1116            local,
1117            remote,
1118        }),
1119        _ => Err(format!(
1120            "link(…) on `{field}` needs target = Type, local = \"column\", and remote = \"column\""
1121        )),
1122    }
1123}
1124
1125fn literal_str(rest: &[TokenTree], field: &str) -> Result<String, String> {
1126    match rest {
1127        [TokenTree::Literal(l)] => string_literal(l),
1128        other => Err(format!(
1129            "relation(…) on `{field}`: expected a string literal, found `{}`",
1130            tokens_text(other)
1131        )),
1132    }
1133}
1134
1135/// The string a literal token spells: `"…"` with its escapes resolved, or `r"…"`/`r#"…"#`
1136/// as written. The token's text is source text, so `"say \"hi\""` has to become `say "hi"`
1137/// here, not stay three characters longer.
1138fn string_literal(lit: &proc_macro::Literal) -> Result<String, String> {
1139    let s = lit.to_string();
1140    if let Some(raw) = s.strip_prefix('r') {
1141        let hashes = raw.len() - raw.trim_start_matches('#').len();
1142        let body = &raw[hashes..raw.len() - hashes];
1143        if hashes * 2 + 2 <= raw.len() && body.starts_with('"') && body.ends_with('"') {
1144            return Ok(body[1..body.len() - 1].to_string());
1145        }
1146        return Err(format!("expected a string literal, found `{s}`"));
1147    }
1148    if s.len() < 2 || !s.starts_with('"') || !s.ends_with('"') {
1149        return Err(format!("expected a string literal, found `{s}`"));
1150    }
1151    let mut out = String::with_capacity(s.len());
1152    let mut chars = s[1..s.len() - 1].chars();
1153    while let Some(ch) = chars.next() {
1154        if ch != '\\' {
1155            out.push(ch);
1156            continue;
1157        }
1158        match chars.next() {
1159            Some('"') => out.push('"'),
1160            Some('\\') => out.push('\\'),
1161            Some('\'') => out.push('\''),
1162            Some('n') => out.push('\n'),
1163            Some('r') => out.push('\r'),
1164            Some('t') => out.push('\t'),
1165            Some('0') => out.push('\0'),
1166            Some('x') => {
1167                let hex: String = chars.by_ref().take(2).collect();
1168                let code = u8::from_str_radix(&hex, 16)
1169                    .map_err(|_| format!("bad escape `\\x{hex}` in {s}"))?;
1170                out.push(code as char);
1171            }
1172            Some('u') => {
1173                if chars.next() != Some('{') {
1174                    return Err(format!("bad escape `\\u` in {s}"));
1175                }
1176                let hex: String = chars.by_ref().take_while(|c| *c != '}').collect();
1177                let code = u32::from_str_radix(&hex, 16)
1178                    .ok()
1179                    .and_then(char::from_u32)
1180                    .ok_or_else(|| format!("bad escape `\\u{{{hex}}}` in {s}"))?;
1181                out.push(code);
1182            }
1183            // A backslash before a newline joins the lines, eating the leading whitespace.
1184            Some('\n') => {
1185                let rest = chars.as_str();
1186                let trimmed = rest.trim_start();
1187                chars = trimmed.chars();
1188                let _ = rest;
1189            }
1190            other => {
1191                return Err(format!(
1192                    "unsupported escape `\\{}` in {s}",
1193                    other.map_or_default(String::from)
1194                ));
1195            }
1196        }
1197    }
1198    Ok(out)
1199}
1200
1201/// Whether `name` can be a SQL identifier at all: SQLite quotes anything, but an empty name
1202/// is refused and a NUL ends the statement text before the quote does.
1203fn check_name(what: &str, name: &str) -> Result<(), String> {
1204    if name.is_empty() {
1205        return Err(format!("Model: the {what} name is empty"));
1206    }
1207    if name.contains('\0') {
1208        return Err(format!(
1209            "Model: the {what} name {name:?} contains a NUL, which SQL cannot carry"
1210        ));
1211    }
1212    Ok(())
1213}
1214
1215fn tokens_text(tokens: &[TokenTree]) -> String {
1216    tokens.iter().cloned().collect::<TokenStream>().to_string()
1217}
1218
1219/// Split the brace body on top-level commas and read `#[obs|model(...)] vis name : Type` out of
1220/// each. "Top-level" means outside `<…>` too, so a `HashMap<u64, usize>` field stays one chunk;
1221/// brackets and parens are already single `Group` tokens, but angles arrive as bare puncts.
1222fn parse_fields(body: TokenStream) -> Result<Vec<FieldDef>, String> {
1223    let mut fields = Vec::new();
1224    let mut chunk: Vec<TokenTree> = Vec::new();
1225    let mut angle_depth = 0i32;
1226    let mut prev_dash = false;
1227
1228    for tt in body {
1229        match &tt {
1230            TokenTree::Punct(p) if p.as_char() == '<' => {
1231                angle_depth += 1;
1232                chunk.push(tt);
1233            }
1234            // `->` in a fn-pointer type must not close an angle bracket.
1235            TokenTree::Punct(p) if p.as_char() == '>' && !prev_dash => {
1236                angle_depth = (angle_depth - 1).max(0);
1237                chunk.push(tt);
1238            }
1239            TokenTree::Punct(p) if p.as_char() == ',' && angle_depth == 0 => {
1240                if !chunk.is_empty() {
1241                    fields.push(parse_one(&chunk)?);
1242                    chunk.clear();
1243                }
1244            }
1245            _ => chunk.push(tt),
1246        }
1247        prev_dash = matches!(
1248            chunk.last(),
1249            Some(TokenTree::Punct(p)) if p.as_char() == '-'
1250        );
1251    }
1252    if !chunk.is_empty() {
1253        fields.push(parse_one(&chunk)?);
1254    }
1255    Ok(fields)
1256}
1257
1258/// Whether a field's type text is `Blob` (bare or path-qualified), the lazy column type.
1259fn is_blob_type(ty: &str) -> bool {
1260    let last = ty.rsplit("::").next().unwrap_or(ty).trim();
1261    last == "Blob"
1262}
1263
1264fn parse_one(chunk: &[TokenTree]) -> Result<FieldDef, String> {
1265    let mut i = 0;
1266    let mut f = FieldDef::default();
1267    let mut deferred: Vec<Vec<Vec<TokenTree>>> = Vec::new();
1268
1269    // Attributes and doc comments. `#[model(…)]` items need the field name for their errors,
1270    // so they are parsed after it is known.
1271    while i < chunk.len() {
1272        match &chunk[i] {
1273            TokenTree::Punct(p) if p.as_char() == '#' => {
1274                if let Some(TokenTree::Group(g)) = chunk.get(i + 1) {
1275                    if let Some(items) = attr_items(g, "obs") {
1276                        for item in items {
1277                            match item.as_slice() {
1278                                [TokenTree::Ident(id)] if id.to_string() == "key" => f.key = true,
1279                                [TokenTree::Ident(id)] if id.to_string() == "skip" => {
1280                                    f.skip = true;
1281                                }
1282                                other => {
1283                                    return Err(format!(
1284                                        "unknown #[obs] option: `{}` (supported: key, skip)",
1285                                        tokens_text(other)
1286                                    ));
1287                                }
1288                            }
1289                        }
1290                    } else if let Some(items) = attr_items(g, "model") {
1291                        deferred.push(items);
1292                    }
1293                    i += 2;
1294                    continue;
1295                }
1296                return Err("malformed attribute on a field".into());
1297            }
1298            _ => break,
1299        }
1300    }
1301
1302    // Visibility.
1303    if let Some(TokenTree::Ident(id)) = chunk.get(i)
1304        && id.to_string() == "pub"
1305    {
1306        i += 1;
1307        if let Some(TokenTree::Group(g)) = chunk.get(i)
1308            && g.delimiter() == Delimiter::Parenthesis
1309        {
1310            i += 1;
1311        }
1312    }
1313
1314    f.name = match chunk.get(i) {
1315        Some(TokenTree::Ident(id)) => id.to_string(),
1316        _ => return Err("expected a field name".into()),
1317    };
1318    i += 1;
1319    match chunk.get(i) {
1320        Some(TokenTree::Punct(p)) if p.as_char() == ':' => i += 1,
1321        _ => return Err(format!("expected `:` after field `{}`", f.name)),
1322    }
1323
1324    // The type is whatever is left, re-emitted verbatim, never interpreted, except that the
1325    // one type the schema half treats specially, `Blob`, is recognised by its last segment.
1326    f.ty = tokens_text(&chunk[i..]);
1327    if f.ty.is_empty() {
1328        return Err(format!("field `{}` has no type", f.name));
1329    }
1330    f.lazy = is_blob_type(&f.ty);
1331
1332    for items in deferred {
1333        parse_field_options(items, &mut f)?;
1334    }
1335    Ok(f)
1336}