Add PrimaryKey::new_or_insert

This commit is contained in:
Louis Dureuil 2024-10-16 09:26:18 +02:00
parent 152683083b
commit 017757004e
No known key found for this signature in database

View File

@ -65,6 +65,18 @@ impl<'a> PrimaryKey<'a> {
})
}
pub fn new_or_insert(
path: &'a str,
fields: &mut impl MutFieldIdMapper,
) -> StdResult<Self, UserError> {
Ok(if path.contains(PRIMARY_KEY_SPLIT_SYMBOL) {
Self::Nested { name: path }
} else {
let field_id = fields.insert(path).ok_or(UserError::AttributeLimitReached)?;
Self::Flat { name: path, field_id }
})
}
pub fn name(&self) -> &'a str {
match self {
PrimaryKey::Flat { name, .. } => name,