use struct_mapper::MapFrom;
struct AddressEntity {
street: String,
city: String,
}
struct OrderEntity {
order_id: u64,
user_name: String,
total_cents: u64,
address: AddressEntity,
}
#[derive(Debug)]
#[allow(dead_code)]
struct AddressResponse {
street: String,
city: String,
}
impl From<AddressEntity> for AddressResponse {
fn from(e: AddressEntity) -> Self {
AddressResponse {
street: e.street,
city: e.city,
}
}
}
fn cents_to_dollars(cents: u64) -> f64 {
cents as f64 / 100.0
}
#[derive(Debug, MapFrom)]
#[map_from(OrderEntity)]
struct OrderResponse {
order_id: u64,
#[map(from = "user_name")]
name: String,
#[map(with = "cents_to_dollars", from = "total_cents")]
total_dollars: f64,
#[map(into)]
address: AddressResponse,
#[map(skip, default)]
request_id: String,
}
fn main() {
let order = OrderEntity {
order_id: 42,
user_name: "Deendayal".to_string(),
total_cents: 1999,
address: AddressEntity {
street: "123 Jaipur".to_string(),
city: "Springfield".to_string(),
},
};
let response: OrderResponse = order.into();
println!("{:#?}", response);
assert_eq!(response.order_id, 42);
assert_eq!(response.name, "Deendayal");
assert!((response.total_dollars - 19.99).abs() < f64::EPSILON);
assert_eq!(response.address.city, "Springfield");
assert_eq!(response.request_id, "");
println!("✅ Advanced mapping works!");
}