review the internal schema to allow to create schema without identifier; fix #513

This commit is contained in:
qdequele 2020-03-05 18:29:10 +01:00
parent 16a99aa95e
commit 86c3482cbd
No known key found for this signature in database
GPG key ID: B3F0A000EBF11745
11 changed files with 312 additions and 129 deletions

View file

@ -6,6 +6,7 @@ pub type SResult<T> = Result<T, Error>;
#[derive(Debug)]
pub enum Error {
FieldNameNotFound(String),
IdentifierAlreadyPresent,
MaxFieldsLimitExceeded,
}
@ -14,6 +15,7 @@ impl fmt::Display for Error {
use self::Error::*;
match self {
FieldNameNotFound(field) => write!(f, "The field {:?} doesn't exist", field),
IdentifierAlreadyPresent => write!(f, "The schema already have an identifier. It's impossible to update it"),
MaxFieldsLimitExceeded => write!(f, "The maximum of possible reattributed field id has been reached"),
}
}

View file

@ -6,7 +6,7 @@ use std::collections::{HashMap, HashSet};
pub struct Schema {
fields_map: FieldsMap,
identifier: FieldId,
identifier: Option<FieldId>,
ranked: HashSet<FieldId>,
displayed: HashSet<FieldId>,
@ -17,6 +17,18 @@ pub struct Schema {
}
impl Schema {
pub fn new() -> Schema {
Schema {
fields_map: FieldsMap::default(),
identifier: None,
ranked: HashSet::new(),
displayed: HashSet::new(),
indexed: Vec::new(),
indexed_map: HashMap::new(),
accept_new_fields: true,
}
}
pub fn with_identifier(name: &str) -> Schema {
let mut fields_map = FieldsMap::default();
let field_id = fields_map.insert(name).unwrap();
@ -31,7 +43,7 @@ impl Schema {
Schema {
fields_map,
identifier: field_id,
identifier: Some(field_id),
ranked: HashSet::new(),
displayed,
indexed,
@ -40,18 +52,21 @@ impl Schema {
}
}
pub fn identifier(&self) -> &str {
self.fields_map.name(self.identifier).unwrap()
pub fn identifier(&self) -> Option<&str> {
self.identifier.map(|id| self.fields_map.name(id).unwrap())
}
pub fn set_identifier(&mut self, id: &str) -> SResult<()> {
match self.id(id) {
Some(id) => {
self.identifier = id;
Ok(())
},
None => Err(Error::FieldNameNotFound(id.to_string()))
pub fn set_identifier(&mut self, name: &str) -> SResult<FieldId> {
if self.identifier.is_some() {
return Err(Error::IdentifierAlreadyPresent)
}
let id = self.insert(name)?;
self.identifier = Some(id);
self.set_indexed(name)?;
self.set_displayed(name)?;
Ok(id)
}
pub fn id(&self, name: &str) -> Option<FieldId> {