use std::collections::{HashMap, HashSet}; use crate::{FieldsMap, FieldId, SResult, SchemaAttr}; pub type IndexedPos = SchemaAttr; #[derive(Default)] pub struct Schema { fields_map: FieldsMap, identifier: FieldId, ranked: HashSet, displayed: HashSet, indexed: Vec, indexed_map: HashMap, } impl Schema { pub fn with_identifier>(name: S) -> Schema { let mut schema = Schema::default(); let field_id = schema.fields_map.insert(name.into()).unwrap(); schema.identifier = field_id; schema } pub fn identifier(&self) -> String { self.fields_map.get_name(self.identifier).unwrap().to_string() } pub fn get_id>(&self, name: S) -> Option<&FieldId> { self.fields_map.get_id(name) } pub fn get_name>(&self, id: I) -> Option<&String> { self.fields_map.get_name(id) } pub fn contains>(&self, name: S) -> bool { match self.fields_map.get_id(name.into()) { Some(_) => true, None => false, } } pub fn get_or_create_empty>(&mut self, name: S) -> SResult { self.fields_map.insert(name) } pub fn get_or_create + std::clone::Clone>(&mut self, name: S) -> SResult { match self.fields_map.get_id(name.clone()) { Some(id) => { Ok(*id) } None => { self.set_indexed(name.clone())?; self.set_displayed(name) } } } pub fn set_ranked>(&mut self, name: S) -> SResult { let id = self.fields_map.insert(name.into())?; self.ranked.insert(id); Ok(id) } pub fn set_displayed>(&mut self, name: S) -> SResult { let id = self.fields_map.insert(name.into())?; self.displayed.insert(id); Ok(id) } pub fn set_indexed>(&mut self, name: S) -> SResult<(FieldId, IndexedPos)> { let id = self.fields_map.insert(name.into())?; let pos = self.indexed.len() as u16; self.indexed.push(id); self.indexed_map.insert(id, pos.into()); Ok((id, pos.into())) } pub fn is_ranked>(&self, name: S) -> Option<&FieldId> { match self.fields_map.get_id(name.into()) { Some(id) => self.ranked.get(id), None => None, } } pub fn is_displayed>(&self, name: S) -> Option<&FieldId> { match self.fields_map.get_id(name.into()) { Some(id) => self.displayed.get(id), None => None, } } pub fn is_indexed>(&self, name: S) -> Option<&IndexedPos> { match self.fields_map.get_id(name.into()) { Some(id) => self.indexed_map.get(id), None => None, } } pub fn id_is_ranked(&self, id: FieldId) -> bool { self.ranked.get(&id).is_some() } pub fn id_is_displayed(&self, id: FieldId) -> bool { self.displayed.get(&id).is_some() } pub fn id_is_indexed(&self, id: FieldId) -> Option<&IndexedPos> { self.indexed_map.get(&id) } pub fn update_ranked>(&mut self, data: impl IntoIterator) -> SResult<()> { self.ranked = HashSet::new(); for name in data { self.set_ranked(name)?; } Ok(()) } pub fn update_displayed>(&mut self, data: impl IntoIterator) -> SResult<()> { self.displayed = HashSet::new(); for name in data { self.set_displayed(name)?; } Ok(()) } pub fn update_indexed>(&mut self, data: Vec) -> SResult<()> { self.indexed = Vec::new(); self.indexed_map = HashMap::new(); for name in data { self.set_indexed(name)?; } Ok(()) } }