fix for review

This commit is contained in:
qdequele 2020-01-29 18:30:21 +01:00
parent 14b5fc4d6c
commit a5b0e468ee
No known key found for this signature in database
GPG key ID: B3F0A000EBF11745
48 changed files with 558 additions and 1216 deletions

View file

@ -13,8 +13,8 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*;
match self {
FieldNameNotFound(field) => write!(f, "The field {} doesn't exist", field),
MaxFieldsLimitExceeded => write!(f, "The maximum of possible reatributed field id has been reached"),
FieldNameNotFound(field) => write!(f, "The field {:?} doesn't exist", field),
MaxFieldsLimitExceeded => write!(f, "The maximum of possible reattributed field id has been reached"),
}
}
}

View file

@ -1,11 +1,9 @@
use std::io::{Read, Write};
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::{SResult, FieldId};
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FieldsMap {
name_map: HashMap<String, FieldId>,
@ -22,41 +20,30 @@ impl FieldsMap {
self.name_map.is_empty()
}
pub fn insert<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
let name = name.into();
if let Some(id) = self.name_map.get(&name) {
pub fn insert(&mut self, name: &str) -> SResult<FieldId> {
if let Some(id) = self.name_map.get(name) {
return Ok(*id)
}
let id = self.next_id.into();
self.next_id = self.next_id.next()?;
self.name_map.insert(name.clone(), id);
self.id_map.insert(id, name);
self.name_map.insert(name.to_string(), id);
self.id_map.insert(id, name.to_string());
Ok(id)
}
pub fn remove<S: Into<String>>(&mut self, name: S) {
let name = name.into();
if let Some(id) = self.name_map.get(&name) {
pub fn remove(&mut self, name: &str) {
if let Some(id) = self.name_map.get(name) {
self.id_map.remove(&id);
}
self.name_map.remove(&name);
self.name_map.remove(name);
}
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<FieldId> {
let name = name.into();
self.name_map.get(&name).map(|s| *s)
pub fn id(&self, name: &str) -> Option<FieldId> {
self.name_map.get(name).map(|s| *s)
}
pub fn get_name<I: Into<FieldId>>(&self, id: I) -> Option<String> {
self.id_map.get(&id.into()).map(|s| s.to_string())
}
pub fn read_from_bin<R: Read>(reader: R) -> bincode::Result<FieldsMap> {
bincode::deserialize_from(reader)
}
pub fn write_to_bin<W: Write>(&self, writer: W) -> bincode::Result<()> {
bincode::serialize_into(writer, &self)
pub fn name<I: Into<FieldId>>(&self, id: I) -> Option<&str> {
self.id_map.get(&id.into()).map(|s| s.as_str())
}
}
@ -73,17 +60,17 @@ mod tests {
assert_eq!(fields_map.insert("id").unwrap(), 0.into());
assert_eq!(fields_map.insert("title").unwrap(), 1.into());
assert_eq!(fields_map.insert("descritpion").unwrap(), 2.into());
assert_eq!(fields_map.get_id("id"), Some(0.into()));
assert_eq!(fields_map.get_id("title"), Some(1.into()));
assert_eq!(fields_map.get_id("descritpion"), Some(2.into()));
assert_eq!(fields_map.get_id("date"), None);
assert_eq!(fields_map.id("id"), Some(0.into()));
assert_eq!(fields_map.id("title"), Some(1.into()));
assert_eq!(fields_map.id("descritpion"), Some(2.into()));
assert_eq!(fields_map.id("date"), None);
assert_eq!(fields_map.len(), 3);
assert_eq!(fields_map.get_name(0), Some("id".to_owned()));
assert_eq!(fields_map.get_name(1), Some("title".to_owned()));
assert_eq!(fields_map.get_name(2), Some("descritpion".to_owned()));
assert_eq!(fields_map.get_name(4), None);
assert_eq!(fields_map.name(0), Some("id"));
assert_eq!(fields_map.name(1), Some("title"));
assert_eq!(fields_map.name(2), Some("descritpion"));
assert_eq!(fields_map.name(4), None);
fields_map.remove("title");
assert_eq!(fields_map.get_id("title"), None);
assert_eq!(fields_map.id("title"), None);
assert_eq!(fields_map.insert("title").unwrap(), 3.into());
assert_eq!(fields_map.len(), 3);
}

View file

@ -22,14 +22,6 @@ impl IndexedPos {
pub const fn max() -> IndexedPos {
IndexedPos(u16::max_value())
}
pub fn next(self) -> SResult<IndexedPos> {
self.0.checked_add(1).map(IndexedPos).ok_or(Error::MaxFieldsLimitExceeded)
}
pub fn prev(self) -> SResult<IndexedPos> {
self.0.checked_sub(1).map(IndexedPos).ok_or(Error::MaxFieldsLimitExceeded)
}
}
impl From<u16> for IndexedPos {
@ -44,7 +36,6 @@ impl Into<u16> for IndexedPos {
}
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct FieldId(pub u16);
@ -64,10 +55,6 @@ impl FieldId {
pub fn next(self) -> SResult<FieldId> {
self.0.checked_add(1).map(FieldId).ok_or(Error::MaxFieldsLimitExceeded)
}
pub fn prev(self) -> SResult<FieldId> {
self.0.checked_sub(1).map(FieldId).ok_or(Error::MaxFieldsLimitExceeded)
}
}
impl From<u16> for FieldId {

View file

@ -15,12 +15,11 @@ pub struct Schema {
indexed: Vec<FieldId>,
indexed_map: HashMap<FieldId, IndexedPos>,
must_index_new_fields: bool,
index_new_fields: bool,
}
impl Schema {
pub fn with_identifier<S: Into<String>>(name: S) -> Schema {
pub fn with_identifier(name: &str) -> Schema {
let mut fields_map = FieldsMap::default();
let field_id = fields_map.insert(name.into()).unwrap();
@ -31,47 +30,47 @@ impl Schema {
displayed: HashSet::new(),
indexed: Vec::new(),
indexed_map: HashMap::new(),
must_index_new_fields: true,
index_new_fields: true,
}
}
pub fn identifier(&self) -> String {
self.fields_map.get_name(self.identifier).unwrap().to_string()
pub fn identifier(&self) -> &str {
self.fields_map.name(self.identifier).unwrap()
}
pub fn set_identifier(&mut self, id: String) -> SResult<()> {
match self.get_id(id.clone()) {
pub fn set_identifier(&mut self, id: &str) -> SResult<()> {
match self.id(id) {
Some(id) => {
self.identifier = id;
Ok(())
},
None => Err(Error::FieldNameNotFound(id))
None => Err(Error::FieldNameNotFound(id.to_string()))
}
}
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<FieldId> {
self.fields_map.get_id(name)
pub fn id(&self, name: &str) -> Option<FieldId> {
self.fields_map.id(name)
}
pub fn get_name<I: Into<FieldId>>(&self, id: I) -> Option<String> {
self.fields_map.get_name(id)
pub fn name<I: Into<FieldId>>(&self, id: I) -> Option<&str> {
self.fields_map.name(id)
}
pub fn contains<S: Into<String>>(&self, name: S) -> bool {
self.fields_map.get_id(name.into()).is_some()
pub fn contains(&self, name: &str) -> bool {
self.fields_map.id(name.into()).is_some()
}
pub fn get_or_create_empty<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
pub fn get_or_create_empty(&mut self, name: &str) -> SResult<FieldId> {
self.fields_map.insert(name)
}
pub fn get_or_create<S: Into<String> + std::clone::Clone>(&mut self, name: S) -> SResult<FieldId> {
match self.fields_map.get_id(name.clone()) {
pub fn get_or_create(&mut self, name: &str) -> SResult<FieldId> {
match self.fields_map.id(name.clone()) {
Some(id) => {
Ok(id)
}
None => {
if self.must_index_new_fields {
if self.index_new_fields {
self.set_indexed(name.clone())?;
self.set_displayed(name)
} else {
@ -81,43 +80,43 @@ impl Schema {
}
}
pub fn get_ranked(&self) -> HashSet<FieldId> {
pub fn ranked(&self) -> HashSet<FieldId> {
self.ranked.clone()
}
pub fn get_ranked_name(&self) -> HashSet<String> {
self.ranked.iter().filter_map(|a| self.get_name(*a)).collect()
pub fn ranked_name(&self) -> HashSet<&str> {
self.ranked.iter().filter_map(|a| self.name(*a)).collect()
}
pub fn get_displayed(&self) -> HashSet<FieldId> {
pub fn displayed(&self) -> HashSet<FieldId> {
self.displayed.clone()
}
pub fn get_displayed_name(&self) -> HashSet<String> {
self.displayed.iter().filter_map(|a| self.get_name(*a)).collect()
pub fn displayed_name(&self) -> HashSet<&str> {
self.displayed.iter().filter_map(|a| self.name(*a)).collect()
}
pub fn get_indexed(&self) -> Vec<FieldId> {
pub fn indexed(&self) -> Vec<FieldId> {
self.indexed.clone()
}
pub fn get_indexed_name(&self) -> Vec<String> {
self.indexed.iter().filter_map(|a| self.get_name(*a)).collect()
pub fn indexed_name(&self) -> Vec<&str> {
self.indexed.iter().filter_map(|a| self.name(*a)).collect()
}
pub fn set_ranked<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
pub fn set_ranked(&mut self, name: &str) -> SResult<FieldId> {
let id = self.fields_map.insert(name.into())?;
self.ranked.insert(id);
Ok(id)
}
pub fn set_displayed<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
pub fn set_displayed(&mut self, name: &str) -> SResult<FieldId> {
let id = self.fields_map.insert(name.into())?;
self.displayed.insert(id);
Ok(id)
}
pub fn set_indexed<S: Into<String>>(&mut self, name: S) -> SResult<(FieldId, IndexedPos)> {
pub fn set_indexed(&mut self, name: &str) -> SResult<(FieldId, IndexedPos)> {
let id = self.fields_map.insert(name.into())?;
if let Some(indexed_pos) = self.indexed_map.get(&id) {
return Ok((id, *indexed_pos))
@ -128,55 +127,34 @@ impl Schema {
Ok((id, pos.into()))
}
pub fn remove_ranked<S: Into<String>>(&mut self, name: S) {
if let Some(id) = self.fields_map.get_id(name.into()) {
pub fn remove_ranked(&mut self, name: &str) {
if let Some(id) = self.fields_map.id(name.into()) {
self.ranked.remove(&id);
}
}
pub fn remove_displayed<S: Into<String>>(&mut self, name: S) {
if let Some(id) = self.fields_map.get_id(name.into()) {
pub fn remove_displayed(&mut self, name: &str) {
if let Some(id) = self.fields_map.id(name.into()) {
self.displayed.remove(&id);
}
}
pub fn remove_indexed<S: Into<String>>(&mut self, name: S) {
if let Some(id) = self.fields_map.get_id(name.into()) {
pub fn remove_indexed(&mut self, name: &str) {
if let Some(id) = self.fields_map.id(name.into()) {
self.indexed_map.remove(&id);
self.indexed.retain(|x| *x != id);
}
}
pub fn is_ranked<S: Into<String>>(&self, name: S) -> Option<FieldId> {
match self.fields_map.get_id(name.into()) {
Some(id) => self.ranked.get(&id).map(|s| *s),
None => None,
}
}
pub fn is_displayed<S: Into<String>>(&self, name: S) -> Option<FieldId> {
match self.fields_map.get_id(name.into()) {
Some(id) => self.displayed.get(&id).map(|s| *s),
None => None,
}
}
pub fn is_indexed<S: Into<String>>(&self, name: S) -> Option<IndexedPos> {
match self.fields_map.get_id(name.into()) {
Some(id) => self.indexed_map.get(&id).map(|s| *s),
None => None,
}
}
pub fn id_is_ranked(&self, id: FieldId) -> bool {
pub fn is_ranked(&self, id: FieldId) -> bool {
self.ranked.get(&id).is_some()
}
pub fn id_is_displayed(&self, id: FieldId) -> bool {
pub fn is_displayed(&self, id: FieldId) -> bool {
self.displayed.get(&id).is_some()
}
pub fn id_is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
pub fn is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
self.indexed_map.get(&id)
}
@ -189,36 +167,36 @@ impl Schema {
}
}
pub fn update_ranked<S: Into<String>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
pub fn update_ranked<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
self.ranked = HashSet::new();
for name in data {
self.set_ranked(name)?;
self.set_ranked(name.as_ref())?;
}
Ok(())
}
pub fn update_displayed<S: Into<String>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
pub fn update_displayed<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
self.displayed = HashSet::new();
for name in data {
self.set_displayed(name)?;
self.set_displayed(name.as_ref())?;
}
Ok(())
}
pub fn update_indexed<S: Into<String>>(&mut self, data: Vec<S>) -> SResult<()> {
pub fn update_indexed<S: AsRef<str>>(&mut self, data: Vec<S>) -> SResult<()> {
self.indexed = Vec::new();
self.indexed_map = HashMap::new();
for name in data {
self.set_indexed(name)?;
self.set_indexed(name.as_ref())?;
}
Ok(())
}
pub fn must_index_new_fields(&self) -> bool {
self.must_index_new_fields
pub fn index_new_fields(&self) -> bool {
self.index_new_fields
}
pub fn set_must_index_new_fields(&mut self, value: bool) {
self.must_index_new_fields = value;
pub fn set_index_new_fields(&mut self, value: bool) {
self.index_new_fields = value;
}
}