cratestack_sql/descriptor/column_lookup.rs
1//! Column-name translation: a descriptor holds SQL names (`@map`) where a
2//! serialized model is keyed by field names (cratestack#1375).
3
4use super::ModelDescriptor;
5
6impl<M, PK> ModelDescriptor<M, PK> {
7 /// The field name of the stored column called `sql`, or `None` when this
8 /// model has no such column. A model's JSON snapshot is keyed by the field
9 /// name; only the SQL name is mapped (`@map`), so a caller holding a
10 /// `pii_columns` / `primary_key` entry translates it here first.
11 pub fn rust_name_of(&self, sql: &str) -> Option<&'static str> {
12 self.columns
13 .iter()
14 .find(|column| column.sql_name == sql)
15 .map(|column| column.rust_name)
16 }
17
18 /// [`Self::rust_name_of`], or `sql` itself when the column is unknown.
19 pub fn rust_name_or<'a>(&self, sql: &'a str) -> &'a str {
20 self.rust_name_of(sql).unwrap_or(sql)
21 }
22}