Skip to content

Commit 46f9d48

Browse files
meskilltusharmath
andauthored
feat: use Into trait for Valid::trace (#23)
Co-authored-by: Tushar Mathur <[email protected]>
1 parent 5d9579f commit 46f9d48

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
/.idea
3-
/build
3+
/.vscode
4+
/build

src/valid.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,12 @@ pub trait Validator<A, E, T>: Sized {
5050
Fusion(self.zip(other))
5151
}
5252

53-
fn trace(self, trace: T) -> Valid<A, E, T>
54-
where
55-
T: Clone,
56-
{
53+
fn trace(self, trace: impl Into<T> + Clone) -> Valid<A, E, T> {
5754
let valid = self.to_result();
5855
if let Err(error) = valid {
5956
return Valid(Err(error
6057
.into_iter()
61-
.map(|cause| cause.trace(trace.clone()))
58+
.map(|cause| cause.trace(trace.clone().into()))
6259
.collect()));
6360
}
6461

@@ -121,10 +118,6 @@ impl<A, E, T> Valid<A, E, T> {
121118
Valid(Err(vec![cause]))
122119
}
123120

124-
pub fn from(error: Vec<Cause<E, T>>) -> Self {
125-
Valid(Err(error))
126-
}
127-
128121
pub fn succeed(a: A) -> Valid<A, E, T> {
129122
Valid(Ok(a))
130123
}
@@ -306,9 +299,9 @@ mod tests {
306299
#[test]
307300
fn test_trace() {
308301
let result = Valid::<(), i32, String>::fail(1)
309-
.trace("A".into())
310-
.trace("B".into())
311-
.trace("C".into());
302+
.trace("A")
303+
.trace("B")
304+
.trace("C");
312305

313306
let expected = Valid::from(vec![Cause {
314307
error: 1,
@@ -403,4 +396,20 @@ mod tests {
403396
assert_eq!(result, Valid::fail(1));
404397
assert_eq!(a, 0);
405398
}
399+
400+
#[test]
401+
fn test_trace_owned_referenced() {
402+
let trace_value = "inner".to_string();
403+
404+
let valid: Valid<((), ()), &str, String> = Valid::fail("fail")
405+
.trace(&trace_value)
406+
.zip(Valid::fail("fail 2").trace(trace_value))
407+
.trace("outer");
408+
409+
let causes = valid.to_result().unwrap_err();
410+
411+
assert_eq!(causes.len(), 2);
412+
assert_eq!(causes[0].to_string(), "[outer, inner] fail");
413+
assert_eq!(causes[1].to_string(), "[outer, inner] fail 2");
414+
}
406415
}

0 commit comments

Comments
 (0)