2020-03-19 14:08:38 +03:00
use proxmox_api_macro ::api ;
2020-04-17 19:34:10 +03:00
use anyhow ::Error ;
2020-03-19 14:08:38 +03:00
use serde_json ::{ json , Value } ;
#[ api(
input : {
properties : {
value : {
description : " The optional value with default. " ,
optional : true ,
default : false ,
}
}
}
) ]
/// Print the given message.
///
/// Returns: the input.
pub fn test_option ( value : bool ) -> Result < bool , Error > {
Ok ( value )
}
2020-03-19 17:49:34 +03:00
#[ api(
input : {
properties : {
value : {
description : " The optional value with default. " ,
optional : true ,
default : 5 ,
}
}
}
) ]
/// Print the given message.
///
/// Returns: the input.
pub fn test_default_macro ( value : Option < isize > ) -> Result < isize , Error > {
Ok ( value . unwrap_or ( api_get_default! ( " value " ) ) )
}
2020-03-19 14:08:38 +03:00
struct RpcEnv ;
2021-10-07 10:36:06 +03:00
impl proxmox_router ::RpcEnvironment for RpcEnv {
2020-05-18 10:36:33 +03:00
fn result_attrib_mut ( & mut self ) -> & mut Value {
panic! ( " result_attrib_mut called " ) ;
2020-03-19 14:08:38 +03:00
}
2020-05-18 10:36:33 +03:00
fn result_attrib ( & self ) -> & Value {
panic! ( " result_attrib called " ) ;
2020-03-19 14:08:38 +03:00
}
/// The environment type
2021-10-07 10:36:06 +03:00
fn env_type ( & self ) -> proxmox_router ::RpcEnvironmentType {
2020-03-19 14:08:38 +03:00
panic! ( " env_type called " ) ;
}
2020-10-29 16:51:06 +03:00
/// Set authentication id
fn set_auth_id ( & mut self , user : Option < String > ) {
2020-03-19 14:08:38 +03:00
let _ = user ;
2020-10-29 16:51:06 +03:00
panic! ( " set_auth_id called " ) ;
2020-03-19 14:08:38 +03:00
}
2020-10-29 16:51:06 +03:00
/// Get authentication id
fn get_auth_id ( & self ) -> Option < String > {
panic! ( " get_auth_id called " ) ;
2020-03-19 14:08:38 +03:00
}
}
#[ test ]
fn test_invocations ( ) {
let mut env = RpcEnv ;
let value = api_function_test_option ( json! ( { } ) , & API_METHOD_TEST_OPTION , & mut env )
. expect ( " func with option should work " ) ;
assert_eq! ( value , false ) ;
let value = api_function_test_option ( json! ( { " value " : true } ) , & API_METHOD_TEST_OPTION , & mut env )
. expect ( " func with option should work " ) ;
assert_eq! ( value , true ) ;
2020-07-16 15:13:13 +03:00
let value =
api_function_test_option ( json! ( { " value " : false } ) , & API_METHOD_TEST_OPTION , & mut env )
. expect ( " func with option should work " ) ;
2020-03-19 14:08:38 +03:00
assert_eq! ( value , false ) ;
2020-03-19 17:49:34 +03:00
2020-07-16 15:13:13 +03:00
let value =
api_function_test_default_macro ( json! ( { } ) , & API_METHOD_TEST_DEFAULT_MACRO , & mut env )
. expect ( " func with option should work " ) ;
2020-03-19 17:49:34 +03:00
assert_eq! ( value , 5 ) ;
2020-03-19 14:08:38 +03:00
}