Hi,
Again I am coming with my confusion for this new langage that I am learning ![]()
I am trying to map a struct 'Entity' to an other struct 'Model'.
The 'Entity' represents the data in a business view, and 'Model' represents the data in a storage/database view.
Code : Mapper
pub struct BookMapper {}
impl BookMapper {
pub fn book_entity_to_model(book_entity: &BookEntity) -> BookModel {
BookModel::new(
book_entity.get_id(),
book_entity.get_title(),
book_entity.get_description(),
book_entity.get_year(),
book_entity.get_bar_code(),
book_entity.get_photos(),
book_entity.get_author(),
book_entity.get_editor(),
)
}
pub fn book_model_to_entity(book_model: &BookModel) -> BookEntity {
BookEntity::new(
book_model.get_id(),
book_model.get_title(),
book_model.get_description(),
book_model.get_year(),
book_model.get_bar_code(),
book_model.get_photos(),
book_model.get_author(),
book_model.get_editor(),
)
}
}
Code : Entity and Model structs
#[derive(Clone)]
pub struct BookEntity {
id: String,
title: String,
description: String,
year: String,
bar_code: String,
photos: Vec<String>,
author: String,
editor: String,
}
impl BookEntity {
pub fn new(id: String, title: String, description: String, year: String, bar_code: String, photos: Vec<String>, author: String, editor: String) -> Self {
Self {
id,
title,
description,
year,
bar_code,
photos,
author,
editor,
}
}
pub fn get_id(self) -> String { self.id }
pub fn get_title(self) -> String { self.title }
pub fn get_description(self) -> String { self.description }
pub fn get_year(self) -> String { self.year }
pub fn get_bar_code(self) -> String { self.bar_code }
pub fn get_photos(self) -> Vec<String> { self.photos }
pub fn get_author(self) -> String { self.author }
pub fn get_editor(self) -> String { self.editor }
}
pub struct BookModel {
id: String,
title: String,
description: String,
year: String,
bar_code: String,
photos: Vec<String>,
author: String,
editor: String,
}
impl BookModel {
pub fn new(id: String, title: String, description: String, year: String, bar_code: String, photos: Vec<String>, author: String, editor: String) -> Self {
Self {
id,
title,
description,
year,
bar_code,
photos,
author,
editor,
}
}
pub fn get_id(self) -> String { self.id }
pub fn get_title(self) -> String { self.title }
pub fn get_description(self) -> String { self.description }
pub fn get_year(self) -> String { self.year }
pub fn get_bar_code(self) -> String { self.bar_code }
pub fn get_photos(self) -> Vec<String> { self.photos }
pub fn get_author(self) -> String { self.author }
pub fn get_editor(self) -> String { self.editor }
}
But when I try do that, Rust do not want because of a problem with a shared reference.
However, I use a reference to not own the object and I have try to clone parameters values, but it does not work... ![]()
Rust error
error[E0507]: cannot move out of `*book_entity` which is behind a shared reference
--> infrastructure\src\mappers\book_mapper.rs:10:13
|
10 | book_entity.get_id(),
| ^^^^^^^^^^^ move occurs because `*book_entity` has type `BookEntity`, which does not implement the `Copy` trait
Could you help me to understand what I did wrong please ?