diff --git a/fluence-faas/src/faas_interface.rs b/fluence-faas/src/faas_interface.rs index e128ad81..cc953823 100644 --- a/fluence-faas/src/faas_interface.rs +++ b/fluence-faas/src/faas_interface.rs @@ -15,12 +15,12 @@ */ use super::IType; -use serde::Serialize; +use serde::{Serialize, Serializer}; use std::fmt; use std::collections::HashMap; -#[derive(Debug, Serialize)] +#[derive(Debug)] pub struct FaaSInterface<'a> { pub modules: HashMap<&'a str, HashMap<&'a str, FaaSFunctionSignature<'a>>>, } @@ -48,3 +48,56 @@ impl<'a> fmt::Display for FaaSInterface<'a> { Ok(()) } } + +impl<'a> Serialize for FaaSInterface<'a> { + fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> + where + S: Serializer, + { + #[derive(Serialize)] + pub struct Function<'a> { + pub name: &'a str, + pub input_types: &'a Vec, + pub output_types: &'a Vec, + } + + #[derive(Serialize)] + pub struct Module<'a> { + pub name: &'a str, + pub functions: Vec>, + } + + #[derive(Serialize)] + pub struct Interface<'a> { + pub modules: Vec>, + } + + 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) + } +}