Skip to content

Commit

Permalink
changed response message type
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge committed Sep 5, 2023
1 parent df0a750 commit 7bca6f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
10 changes: 10 additions & 0 deletions src/register/field_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ impl IntoFieldName for [usize; 1] {
// Ok(vec![FieldName::StructVariant(self[0].to_string())])
// }
// }
impl<'a, T> IntoFieldName for &'a T
where
T: IntoFieldName + Copy,
{
type Error = T::Error;

fn into_field(self) -> Result<FieldNames, Self::Error> {
T::into_field(*self)
}
}

pub(crate) struct Parser<'a> {
source: &'a str,
Expand Down
15 changes: 10 additions & 5 deletions src/register/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ impl<'a> Validator<'a> {

#[derive(Debug)]
pub struct Response {
message: Vec<(FieldNames, Vec<String>)>,
message: HashMap<FieldNames, Vec<String>>,
}

impl Deref for Response {
type Target = Vec<(FieldNames, Vec<String>)>;
type Target = HashMap<FieldNames, Vec<String>>;
fn deref(&self) -> &Self::Target {
&self.message
}
Expand All @@ -125,20 +125,25 @@ impl Deref for Response {
impl Response {
fn new() -> Self {
Self {
message: Vec::new(),
message: HashMap::new(),
}
}
fn with_capacity(capacity: usize) -> Self {
Self {
message: Vec::with_capacity(capacity),
message: HashMap::with_capacity(capacity),
}
}

fn push(&mut self, field_name: FieldNames, message: Vec<String>) {
if !message.is_empty() {
self.message.push((field_name, message));
self.message.insert(field_name, message);
}
}

pub fn get<K: IntoFieldName>(&self, key: K) -> Option<&Vec<String>> {
let k = key.into_field().ok()?;
self.message.get(&k)
}
}

impl From<Response> for Result<(), Response> {
Expand Down
33 changes: 16 additions & 17 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde::{Deserialize, Serialize};
use valitron::{
register::FieldName,
rule::{custom, Required, RuleExt, StartWith},
Validator, Value,
};
Expand Down Expand Up @@ -29,14 +28,14 @@ fn test_validator() {
let res = validator.validate(person).unwrap_err();

assert!(res.len() == 2);
assert!(res.contains(&(
vec![FieldName::Literal("age".into())].into(),
vec!["age should be between 25 and 45".to_string()],
)));
assert!(res.contains(&(
vec![FieldName::Literal("name".into())].into(),
vec!["name should be starts with `hello`".to_string()],
)));
assert_eq!(
res.get("age"),
Some(&vec!["age should be between 25 and 45".to_string()])
);
assert_eq!(
res.get("name"),
Some(&vec!["name should be starts with `hello`".to_string()])
);

//println!("{res:?}");
}
Expand All @@ -62,10 +61,10 @@ fn test_has_tuple() {
let res = validator.validate(Foo("heoo", "bar")).unwrap_err();
assert!(res.len() == 1);

assert!(res.contains(&(
vec![FieldName::Tuple(0)].into(),
vec!["first item should be start with `hello`".to_string()],
)));
assert_eq!(
res.get(0),
Some(&vec!["first item should be start with `hello`".to_string()])
);
}

#[test]
Expand All @@ -75,8 +74,8 @@ fn test_has_array() {
let res = validator.validate(vec!["foo", "bar"]).unwrap_err();

assert!(res.len() == 1);
assert!(res.contains(&(
vec![FieldName::Array(1)].into(),
vec!["this field must be start with {}".to_string()],
)));
assert_eq!(
res.get([1]),
Some(&vec!["this field must be start with {}".to_string()])
);
}

0 comments on commit 7bca6f4

Please sign in to comment.