summaryrefslogtreecommitdiff
path: root/excluded/validation.rs
diff options
context:
space:
mode:
authorjelemux <jeremias.weber@protonmail.com>2020-10-19 22:46:45 +0200
committerjelemux <jeremias.weber@protonmail.com>2020-10-19 22:46:45 +0200
commit3a0a55362fd98c74bfb14df320bcacafd0c57835 (patch)
treead7f91af95df638bfbc58c48c97f34340ae37a8c /excluded/validation.rs
parent84c2dab4200c37c818d83c95f85445ee00d83bf6 (diff)
downloadwasm-card-3a0a55362fd98c74bfb14df320bcacafd0c57835.tar.gz
wasm-card-3a0a55362fd98c74bfb14df320bcacafd0c57835.tar.bz2
break things down into their most basic parts
Diffstat (limited to 'excluded/validation.rs')
-rw-r--r--excluded/validation.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/excluded/validation.rs b/excluded/validation.rs
new file mode 100644
index 0000000..6258dcf
--- /dev/null
+++ b/excluded/validation.rs
@@ -0,0 +1,37 @@
+
+
+pub trait Validation {
+ fn validate(&self) -> Result<(), ValidationError>;
+}
+
+#[derive(Debug)]
+pub struct ValidationError {
+ pub messages: Vec<String>,
+}
+
+impl std::fmt::Display for ValidationError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ for msg in &self.messages {
+ write!(f, "{}\n", msg)?;
+ }
+ Ok(())
+ }
+}
+
+impl std::error::Error for ValidationError { }
+
+pub fn add_results(first: Result<(), ValidationError>, second: Result<(), ValidationError>) -> Result<(), ValidationError> {
+ if first.is_ok() && second.is_ok() {
+ Ok(())
+ } else if first.is_ok() && second.is_err() {
+ second
+ } else if first.is_err() && second.is_ok() {
+ first
+ } else {
+ let mut first = first.err().unwrap();
+ let mut second = second.err().unwrap();
+ first.messages.append(&mut second.messages);
+
+ Err( ValidationError{ messages: first.messages } )
+ }
+} \ No newline at end of file