custom interface serialization (#12)

This commit is contained in:
folex 2020-07-28 16:29:09 +03:00 committed by GitHub
parent 0a32104cbc
commit ea7219f477
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,12 +15,12 @@
*/ */
use super::IType; use super::IType;
use serde::Serialize; use serde::{Serialize, Serializer};
use std::fmt; use std::fmt;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, Serialize)] #[derive(Debug)]
pub struct FaaSInterface<'a> { pub struct FaaSInterface<'a> {
pub modules: HashMap<&'a str, HashMap<&'a str, FaaSFunctionSignature<'a>>>, pub modules: HashMap<&'a str, HashMap<&'a str, FaaSFunctionSignature<'a>>>,
} }
@ -48,3 +48,56 @@ impl<'a> fmt::Display for FaaSInterface<'a> {
Ok(()) Ok(())
} }
} }
impl<'a> Serialize for FaaSInterface<'a> {
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
#[derive(Serialize)]
pub struct Function<'a> {
pub name: &'a str,
pub input_types: &'a Vec<IType>,
pub output_types: &'a Vec<IType>,
}
#[derive(Serialize)]
pub struct Module<'a> {
pub name: &'a str,
pub functions: Vec<Function<'a>>,
}
#[derive(Serialize)]
pub struct Interface<'a> {
pub modules: Vec<Module<'a>>,
}
let modules: Vec<_> = self
.modules
.iter()
.map(|(name, functions)| {
let functions = functions
.iter()
.map(
|(
name,
FaaSFunctionSignature {
input_types,
output_types,
},
)| {
Function {
name,
input_types,
output_types,
}
},
)
.collect();
Module { name, functions }
})
.collect();
Interface { modules }.serialize(serializer)
}
}