Skip to content

Commit

Permalink
🎨 Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rster2002 committed Oct 7, 2023
1 parent 4070d02 commit 9a0aaca
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 157 deletions.
4 changes: 2 additions & 2 deletions src/field.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::{Deserialize, Serialize};
use crate::errors::validation_error::ValidationError;
use crate::field::custom_field::CustomField;
use crate::field::object_field::ObjectField;
use crate::field::optional_field::OptionalField;
use crate::field::string_field::StringField;
use crate::{Validator, validator_impl};
use crate::{validator_impl, Validator};
use serde::{Deserialize, Serialize};
use serde_json::Value;

pub mod custom_field;
Expand Down
40 changes: 24 additions & 16 deletions src/field/custom_field.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::errors::validation_error::ValidationError;
use crate::{Validator, validator_impl};
use crate::{validator_impl, Validator};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::{Debug, Formatter};
use serde::{Deserialize, Serialize};

/// Custom fields allow you to use your own validators within other fields, like an `ObjectField`
/// as shown here:
Expand Down Expand Up @@ -80,16 +80,16 @@ impl Debug for CustomField {

#[cfg(test)]
mod tests {
use crate::errors::validation_error::ValidationError;
use crate::field::custom_field::CustomField;
use crate::field::object_field::ObjectField;
use crate::field::Field;
use crate::{validator_impl, Deserialize, Serialize, Validator};
use serde_json::{json, Value};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use serde_json::{json, Value};
use thiserror::Error;
use uuid::Uuid;
use crate::{Deserialize, Serialize, Validator, validator_impl};
use crate::errors::validation_error::ValidationError;
use crate::field::custom_field::CustomField;
use crate::field::Field;
use crate::field::object_field::ObjectField;

#[derive(Debug, Error)]
struct StrError(pub String);
Expand All @@ -115,11 +115,14 @@ mod tests {
impl Validator for UuidValidator {
fn validate(&self, value: &Value) -> Result<(), ValidationError> {
let Value::String(string) = value else {
return Err(ValidationError::new_custom(StrError::from_str("not a string").unwrap()));
return Err(ValidationError::new_custom(
StrError::from_str("not a string").unwrap(),
));
};

Uuid::from_str(string)
.map_err(|_| ValidationError::new_custom(StrError::from_str("invalid uuid").unwrap()))?;
Uuid::from_str(string).map_err(|_| {
ValidationError::new_custom(StrError::from_str("invalid uuid").unwrap())
})?;

Ok(())
}
Expand All @@ -132,11 +135,15 @@ mod tests {
impl Validator for ExactStringValidator {
fn validate(&self, value: &Value) -> Result<(), ValidationError> {
let Value::String(string) = value else {
return Err(ValidationError::new_custom(StrError::from_str("not a string").unwrap()));
return Err(ValidationError::new_custom(
StrError::from_str("not a string").unwrap(),
));
};

if string != &self.0 {
return Err(ValidationError::new_custom(StrError::from_str("not a").unwrap()));
return Err(ValidationError::new_custom(
StrError::from_str("not a").unwrap(),
));
}

Ok(())
Expand Down Expand Up @@ -195,9 +202,10 @@ mod tests {
fn custom_field_can_is_serialized_correctly_from_within_another_field() {
let exact_validator = ExactStringValidator("a".to_string());

let object_field = Field::Object(ObjectField::from([
("uuid", Field::CustomValidator(CustomField::new(exact_validator)))
]));
let object_field = Field::Object(ObjectField::from([(
"uuid",
Field::CustomValidator(CustomField::new(exact_validator)),
)]));

let string_result = serde_json::to_string(&object_field);
assert!(string_result.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion src/field/object_field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::errors::validation_error::ValidationError;
use crate::field::Field;
use crate::{Validator, validator_impl};
use crate::{validator_impl, Validator};
use serde_json::Value;
use std::collections::HashMap;

Expand Down
4 changes: 2 additions & 2 deletions src/field/optional_field.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::errors::validation_error::ValidationError;
use crate::field::Field;
use crate::{Validator, validator_impl};
use crate::{validator_impl, Validator};
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize)]
Expand Down
26 changes: 19 additions & 7 deletions src/field/string_field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use crate::errors::validation_error::ValidationError;
use crate::{Validator, validator_impl};
use crate::{validator_impl, Validator};
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Default, Serialize, Deserialize)]
Expand All @@ -24,13 +24,19 @@ impl Validator for StringField {

if let Some(min_length) = self.min_length {
if string.len() < min_length {
return Err(ValidationError::StringNotMinLength(min_length, string.len()));
return Err(ValidationError::StringNotMinLength(
min_length,
string.len(),
));
}
}

if let Some(max_length) = self.max_length {
if string.len() > max_length {
return Err(ValidationError::StringExceedsMaxLength(max_length, string.len()));
return Err(ValidationError::StringExceedsMaxLength(
max_length,
string.len(),
));
}
}

Expand All @@ -40,10 +46,10 @@ impl Validator for StringField {

#[cfg(test)]
mod tests {
use serde_json::json;
use crate::errors::validation_error::ValidationError;
use crate::field::string_field::StringField;
use crate::Validator;
use serde_json::json;

#[test]
fn filled_check_is_checked_correctly() {
Expand Down Expand Up @@ -74,7 +80,10 @@ mod tests {

let failure = string_field.validate(&json!("ab"));

assert!(matches!(failure, Err(ValidationError::StringNotMinLength(3, 2))));
assert!(matches!(
failure,
Err(ValidationError::StringNotMinLength(3, 2))
));
}

#[test]
Expand All @@ -92,6 +101,9 @@ mod tests {

let failure = string_field.validate(&json!("abcdefg"));

assert!(matches!(failure, Err(ValidationError::StringExceedsMaxLength(6, 7))));
assert!(matches!(
failure,
Err(ValidationError::StringExceedsMaxLength(6, 7))
));
}
}
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
pub mod errors;
pub mod field;
pub mod migration;
mod schema;
pub(crate) mod traits;
pub mod migration;

pub use serde::{Deserialize, Serialize};
pub use traits::validator::Validator;
pub use typetag::serde as validator_impl;
pub use serde::{Serialize, Deserialize};

#[cfg(test)]
mod tests {
use crate::field::string_field::StringField;
use crate::field::Field;
use crate::schema::Schema;
use serde_json::json;
use crate::Validator;
use serde_json::json;

#[test]
fn main() {
Expand Down
26 changes: 14 additions & 12 deletions src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use serde_json::Value;
use thiserror::Error;
use crate::migration::json_path::JsonPathError;
use crate::migration::operation::Operation;
use crate::migration::operation_kind::OperationKind;
use serde_json::Value;
use thiserror::Error;

use crate::migration::set_path::{SetPath, SetPathError};

mod json_path;
pub mod operation;
pub mod operation_kind;
mod set_path;
mod json_path;

#[derive(Debug, Error)]
pub enum MigrationError {
Expand Down Expand Up @@ -70,9 +70,8 @@ impl Migration {

match target_value {
Value::Array(array) => {
let index = last
.parse()
.map_err(|_| MigrationError::NotAnIndex(last))?;
let index =
last.parse().map_err(|_| MigrationError::NotAnIndex(last))?;

array.remove(index);
}
Expand All @@ -95,22 +94,25 @@ impl Migration {

#[cfg(test)]
mod tests {
use std::str::FromStr;
use serde_json::json;
use crate::migration::json_path::JsonPath;
use crate::migration::Migration;
use crate::migration::operation::Operation;
use crate::migration::operation_kind::OperationKind;
use crate::migration::Migration;
use serde_json::json;
use std::str::FromStr;

#[test]
fn key_can_be_renamed() {
let from = json!({ "a": 10 });
let to = json!({ "b": 10 });

let migration = Migration::with_operations([
Operation::new(JsonPath::from_str("$.a").unwrap(), OperationKind::Copy {
new_path: JsonPath::from_str("$.b").unwrap(),
}),
Operation::new(
JsonPath::from_str("$.a").unwrap(),
OperationKind::Copy {
new_path: JsonPath::from_str("$.b").unwrap(),
},
),
Operation::new(JsonPath::from_str("$.a").unwrap(), OperationKind::Delete),
]);

Expand Down
Loading

0 comments on commit 9a0aaca

Please sign in to comment.