sigalign_core/results/
to_json.rs

1use std::io::{Write, Error};
2use serde::Deserialize;
3use serde_json::{
4    to_string,
5    to_string_pretty,
6    to_writer,
7    to_writer_pretty,
8    from_str,
9};
10
11fn translate_str_to_result<'a, T>(s: &'a str) -> Result<T, Error> where
12    T: Deserialize<'a>
13{
14    match from_str(s) {
15        Ok(v) => Ok(v),
16        Err(_) => Err(std::io::ErrorKind::InvalidData.into()),
17    }
18}
19
20macro_rules! impl_translate_between_json {
21    ( $st: ident ) => {
22        impl $st {
23            pub fn to_json(&self) -> String {
24                to_string(self).unwrap()
25            }
26            pub fn to_json_pretty(&self) -> String {
27                to_string_pretty(self).unwrap()
28            }
29            pub fn write_as_json<W: Write>(&self, writer: W) {
30                to_writer(writer, self).unwrap()
31            }
32            pub fn write_as_json_pretty<W: Write>(&self, writer: W) {
33                to_writer_pretty(writer, self).unwrap()
34            }
35            pub fn from_json(s: &str) -> Result<Self, Error> {
36                translate_str_to_result(s)
37            }
38        }
39    };
40}
41
42use super::{
43    QueryAlignment,
44    TargetAlignment,
45    Alignment,
46};
47impl_translate_between_json!(QueryAlignment);
48impl_translate_between_json!(TargetAlignment);
49impl_translate_between_json!(Alignment);