Mapper

Trait Mapper 

Source
pub trait Mapper<From, To> {
    // Required method
    fn map(&self, from: From) -> HexResult<To>;
}
Expand description

Trait for mapping data between different representations.

Mappers transform data as it crosses architectural boundaries, converting between domain models and external representations.

§Type Parameters

  • From - The source data type
  • To - The target data type

§Example

use hexser::adapters::Mapper;
use hexser::HexResult;

struct DomainUser {
    id: String,
    email: String,
}

struct DbUserRow {
    user_id: String,
    user_email: String,
}

struct UserMapper;

impl Mapper<DomainUser, DbUserRow> for UserMapper {
    fn map(&self, from: DomainUser) -> HexResult<DbUserRow> {
        Ok(DbUserRow {
            user_id: from.id,
            user_email: from.email,
        })
    }
}

impl Mapper<DbUserRow, DomainUser> for UserMapper {
    fn map(&self, from: DbUserRow) -> HexResult<DomainUser> {
        Ok(DomainUser {
            id: from.user_id,
            email: from.user_email,
        })
    }
}

Required Methods§

Source

fn map(&self, from: From) -> HexResult<To>

Map from one representation to another.

Returns the mapped value if successful, or an error if the mapping fails.

Implementors§