use component_model ::ComponentModel;
use component_model_types ::Assign;
#[ derive( Default, ComponentModel, PartialEq, Debug ) ]
struct ConfigWithUniqueTypes
{
host: String,
port: i32,
enabled: bool,
}
#[ derive( Default, ComponentModel, PartialEq, Debug ) ]
struct ConfigWithMultipleBools
{
enabled: bool,
debug: bool,
verbose: bool,
}
#[ derive( Default, PartialEq, Debug, Clone ) ]
struct CustomType( String );
impl From< &str > for CustomType
{
fn from( s: &str ) -> Self { CustomType( s.to_string() ) }
}
#[ derive( Default, ComponentModel, PartialEq, Debug ) ]
struct ConfigWithDistinctTypes
{
host: String,
port: i32,
custom: CustomType,
}
#[ derive( Default, ComponentModel, PartialEq, Debug ) ]
struct ConfigSingleBool
{
enabled: bool,
}
#[ test ]
fn test_non_boolean_assignment_still_works()
{
let mut config = ConfigWithUniqueTypes ::default();
config.assign( "localhost".to_string() );
assert_eq!( config.host, "localhost" );
config.assign( 8080i32 );
assert_eq!( config.port, 8080 );
}
#[ test ]
fn test_fluent_builder_non_boolean()
{
let config = ConfigWithUniqueTypes ::default()
.impute( "api.example.com".to_string() )
.impute( 3000i32 );
assert_eq!( config.host, "api.example.com" );
assert_eq!( config.port, 3000 );
}
#[ test ]
fn test_multiple_bool_fields_generate_single_impl()
{
let mut config = ConfigWithMultipleBools ::default();
config.assign( true );
}
#[ test ]
fn test_struct_with_distinct_types()
{
let mut config = ConfigWithDistinctTypes ::default();
config.assign( "localhost".to_string() );
config.assign( 8080i32 );
config.assign( CustomType ::from( "test" ) );
assert_eq!( config.host, "localhost" );
assert_eq!( config.port, 8080 );
assert_eq!( config.custom.0, "test" );
}
#[ test ]
fn test_single_bool_field()
{
let mut config = ConfigSingleBool ::default();
Assign :: < bool, bool > ::assign( &mut config, true );
assert!( config.enabled );
}
#[ test ]
fn test_explicit_type_annotation_workaround()
{
let mut config = ConfigWithUniqueTypes ::default();
Assign :: < String, String > ::assign( &mut config, "test".to_string() );
Assign :: < i32, i32 > ::assign( &mut config, 1234i32 );
Assign :: < bool, bool > ::assign( &mut config, true );
assert_eq!( config.host, "test" );
assert_eq!( config.port, 1234 );
assert!( config.enabled );
}
#[ test ]
fn test_fluent_with_explicit_types()
{
let config = ConfigWithUniqueTypes ::default()
.impute( "test".to_string() )
.impute( 9999i32 );
assert_eq!( config.host, "test" );
assert_eq!( config.port, 9999 );
let mut config = config;
Assign :: < bool, bool > ::assign( &mut config, true );
assert!( config.enabled );
}