Removed unneeded code from FieldSettings.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
This commit is contained in:
parent
8efa797494
commit
a157d5cc67
@ -1,3 +1,4 @@
|
|||||||
|
use chrono::prelude::*;
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
sync::{
|
sync::{
|
||||||
@ -950,6 +951,7 @@ impl CreateDoc {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
enum FieldType {
|
enum FieldType {
|
||||||
|
DateTime,
|
||||||
None,
|
None,
|
||||||
StaticString,
|
StaticString,
|
||||||
Uuid,
|
Uuid,
|
||||||
@ -958,6 +960,7 @@ enum FieldType {
|
|||||||
impl FieldType {
|
impl FieldType {
|
||||||
fn get_default(&self) -> Field {
|
fn get_default(&self) -> Field {
|
||||||
match self {
|
match self {
|
||||||
|
FieldType::DateTime => Utc::now().into(),
|
||||||
FieldType::None => Field::None,
|
FieldType::None => Field::None,
|
||||||
FieldType::StaticString => "".into(),
|
FieldType::StaticString => "".into(),
|
||||||
FieldType::Uuid => Uuid::new_v4().into(),
|
FieldType::Uuid => Uuid::new_v4().into(),
|
||||||
@ -968,6 +971,7 @@ impl FieldType {
|
|||||||
impl From<&Field> for FieldType {
|
impl From<&Field> for FieldType {
|
||||||
fn from(value: &Field) -> Self {
|
fn from(value: &Field) -> Self {
|
||||||
match value {
|
match value {
|
||||||
|
Field::DateTime(_) => Self::DateTime,
|
||||||
Field::None => Self::None,
|
Field::None => Self::None,
|
||||||
Field::StaticString(_) => Self::StaticString,
|
Field::StaticString(_) => Self::StaticString,
|
||||||
Field::Uuid(_) => Self::Uuid,
|
Field::Uuid(_) => Self::Uuid,
|
||||||
@ -1013,6 +1017,7 @@ mod fieldtypes {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
enum Field {
|
enum Field {
|
||||||
|
DateTime(DateTime<Utc>),
|
||||||
None,
|
None,
|
||||||
StaticString(String),
|
StaticString(String),
|
||||||
Uuid(Uuid),
|
Uuid(Uuid),
|
||||||
@ -1024,6 +1029,12 @@ impl Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<DateTime<Utc>> for Field {
|
||||||
|
fn from(value: DateTime<Utc>) -> Self {
|
||||||
|
Self::DateTime(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<String> for Field {
|
impl From<String> for Field {
|
||||||
fn from(value: String) -> Self {
|
fn from(value: String) -> Self {
|
||||||
Self::StaticString(value)
|
Self::StaticString(value)
|
||||||
@ -1086,9 +1097,7 @@ mod fields {
|
|||||||
struct FieldSetting {
|
struct FieldSetting {
|
||||||
fieldtype: FieldType,
|
fieldtype: FieldType,
|
||||||
use_default: bool,
|
use_default: bool,
|
||||||
use_unique: bool,
|
|
||||||
default_value: Field,
|
default_value: Field,
|
||||||
unique: HashSet<Field>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FieldSetting {
|
impl FieldSetting {
|
||||||
@ -1096,9 +1105,7 @@ impl FieldSetting {
|
|||||||
Self {
|
Self {
|
||||||
fieldtype: ftype,
|
fieldtype: ftype,
|
||||||
use_default: false,
|
use_default: false,
|
||||||
use_unique: false,
|
|
||||||
default_value: Field::None,
|
default_value: Field::None,
|
||||||
unique: HashSet::new(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1117,23 +1124,6 @@ impl FieldSetting {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_unique(&mut self) -> Result<(), MTTError> {
|
|
||||||
self.use_unique = true;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn use_unique_value(&mut self, field: Field) {
|
|
||||||
if self.use_unique {
|
|
||||||
self.unique.insert(field);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_unique_value(&mut self, field: &Field) {
|
|
||||||
if self.use_unique {
|
|
||||||
self.unique.remove(field);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn validate(&self, value: Option<Field>) -> Result<Field, MTTError> {
|
fn validate(&self, value: Option<Field>) -> Result<Field, MTTError> {
|
||||||
match value {
|
match value {
|
||||||
Some(data) => {
|
Some(data) => {
|
||||||
@ -1144,11 +1134,6 @@ impl FieldSetting {
|
|||||||
vft,
|
vft,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
if self.use_unique {
|
|
||||||
if self.unique.get(&data).is_some() {
|
|
||||||
return Err(MTTError::FieldDuplicate("".to_string(), data.clone()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(data.clone())
|
Ok(data.clone())
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@ -1227,24 +1212,6 @@ mod fieldsettings {
|
|||||||
Err(err) => unreachable!("got {:?}: should have gotten a value", err),
|
Err(err) => unreachable!("got {:?}: should have gotten a value", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn validates_for_unique_value() {
|
|
||||||
let mut fset = FieldSetting::new(FieldType::Uuid);
|
|
||||||
let field: Field = Uuid::new_v4().into();
|
|
||||||
fset.set_unique();
|
|
||||||
fset.use_unique_value(field.clone());
|
|
||||||
match fset.validate(Some(field.clone())) {
|
|
||||||
Ok(data) => unreachable!("got {:?}: should have been error", data),
|
|
||||||
Err(err) => match err {
|
|
||||||
MTTError::FieldDuplicate(key, result) => {
|
|
||||||
assert_eq!(key, "");
|
|
||||||
assert_eq!(result, field);
|
|
||||||
}
|
|
||||||
_ => unreachable!("got {:?}: should be a duplicate field", err),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@ -1542,13 +1509,13 @@ mod operands {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
struct Specifier {
|
struct Operation {
|
||||||
field_name: String,
|
field_name: String,
|
||||||
operation: Operand,
|
operation: Operand,
|
||||||
value: Field,
|
value: Field,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Specifier {
|
impl Operation {
|
||||||
fn new<F>(name: String, op: Operand, value: F) -> Self
|
fn new<F>(name: String, op: Operand, value: F) -> Self
|
||||||
where
|
where
|
||||||
F: Into<Field>,
|
F: Into<Field>,
|
||||||
@ -1571,7 +1538,7 @@ impl Specifier {
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
struct Query {
|
struct Query {
|
||||||
specifiers: Vec<Specifier>,
|
specifiers: Vec<Operation>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Query {
|
impl Query {
|
||||||
@ -1585,11 +1552,11 @@ impl Query {
|
|||||||
where
|
where
|
||||||
F: Into<Field>,
|
F: Into<Field>,
|
||||||
{
|
{
|
||||||
let spec = Specifier::new(name, op, value);
|
let spec = Operation::new(name, op, value);
|
||||||
self.specifiers.push(spec);
|
self.specifiers.push(spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iter(&self) -> impl Iterator<Item = &Specifier> {
|
fn iter(&self) -> impl Iterator<Item = &Operation> {
|
||||||
self.specifiers.iter()
|
self.specifiers.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1803,7 +1770,7 @@ impl Index {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, spec: &Specifier) -> Vec<Oid> {
|
fn get(&self, spec: &Operation) -> Vec<Oid> {
|
||||||
let mut output = Vec::new();
|
let mut output = Vec::new();
|
||||||
for (field, oids) in self.data.iter() {
|
for (field, oids) in self.data.iter() {
|
||||||
if spec.validate(field) {
|
if spec.validate(field) {
|
||||||
@ -1915,7 +1882,7 @@ mod indexes {
|
|||||||
index.add(fields[i].clone(), oids[i].clone());
|
index.add(fields[i].clone(), oids[i].clone());
|
||||||
}
|
}
|
||||||
for i in 0..count {
|
for i in 0..count {
|
||||||
let spec = Specifier::new("stuff".to_string(), Operand::Equal, fields[i].clone());
|
let spec = Operation::new("stuff".to_string(), Operand::Equal, fields[i].clone());
|
||||||
let result = index.get(&spec);
|
let result = index.get(&spec);
|
||||||
assert_eq!(result.len(), 1);
|
assert_eq!(result.len(), 1);
|
||||||
assert_eq!(result[0], oids[i]);
|
assert_eq!(result[0], oids[i]);
|
||||||
@ -1931,7 +1898,7 @@ mod indexes {
|
|||||||
for i in 0..count {
|
for i in 0..count {
|
||||||
index.add(fields[0].clone(), oids[i].clone());
|
index.add(fields[0].clone(), oids[i].clone());
|
||||||
}
|
}
|
||||||
let spec = Specifier::new("unimportant".to_string(), Operand::Equal, fields[0].clone());
|
let spec = Operation::new("unimportant".to_string(), Operand::Equal, fields[0].clone());
|
||||||
let result = index.get(&spec);
|
let result = index.get(&spec);
|
||||||
assert_eq!(result.len(), 3);
|
assert_eq!(result.len(), 3);
|
||||||
for oid in oids {
|
for oid in oids {
|
||||||
@ -1950,7 +1917,7 @@ mod indexes {
|
|||||||
index.add(fields[0].clone(), oids[i].clone());
|
index.add(fields[0].clone(), oids[i].clone());
|
||||||
}
|
}
|
||||||
index.remove(&fields[0], &oids[pos]);
|
index.remove(&fields[0], &oids[pos]);
|
||||||
let spec = Specifier::new("x".to_string(), Operand::Equal, fields[0].clone());
|
let spec = Operation::new("x".to_string(), Operand::Equal, fields[0].clone());
|
||||||
let result = index.get(&spec);
|
let result = index.get(&spec);
|
||||||
assert!(!result.contains(&oids[pos]), "should have removed oid");
|
assert!(!result.contains(&oids[pos]), "should have removed oid");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user