idl2json/
polyfill.rs

1//! Code that should be pushed upstream.
2
3/// Polyfills for the candid IDLProg struct.
4pub mod idl_prog {
5    use candid_parser::{
6        types::{Dec, IDLType, IDLTypes},
7        IDLProg,
8    };
9
10    /// Gets a type defined in a program declarations section.
11    #[deprecated(since = "0.8.6", note = "Please use `get_type()` instead.")]
12    pub fn get(prog: &IDLProg, key: &str) -> Option<IDLType> {
13        get_type(prog, key)
14    }
15
16    /// Gets a type defined in a program declarations section.
17    pub fn get_type(prog: &IDLProg, key: &str) -> Option<IDLType> {
18        prog.decs.iter().find_map(|x| {
19            if let Dec::TypD(y) = x {
20                if y.id == key {
21                    return Some(y.typ.clone());
22                }
23            }
24            None
25        })
26    }
27
28    /// Gets the arguments for creating a service.
29    ///
30    /// This will return None if the prog contains no service aka actor of type ClassT.
31    pub fn get_init_arg_type(prog: &IDLProg) -> Option<IDLTypes> {
32        if let Some(IDLType::ClassT(args, _)) = &prog.actor {
33            Some(IDLTypes { args: args.clone() })
34        } else {
35            None
36        }
37    }
38}