I have this data structure:
#[derive(Debug, FromForm, Serialize, Deserialize)]
pub struct PhotoDataInput {
pub filename: String,
pub title_en: String,
pub description_en: String,
pub title_ja: String,
pub description_ja: String,
pub tags: String,
}
impl PhotoDataInput {
pub fn replace_tags(&mut self, replace_tags: String) {
self.tags = replace_tags;
}
}
The data is accepted from web HTML post:
#[post("/publish_photo", data = "<photo_data>")]
fn publish_photo<'r>(photo_data: Result<Form<'r, PhotoDataInput>, Option<String>>) -> Redirect {
let photo_data_input_form = photo_data.expect("Error processing submitted form data");
let photo_data_input = photo_data_input_form.get();
photo_actions::process_photo_data(&photo_data_input); // todo get and process result
...
and the function to process it:
`pub fn process_photo_data(photo_data: &web::PhotoDataInput) {`
...
let tags_vector: Vec<&str> = photo_data.tags.split(',').collect();
let tags_json = super::serde_json::to_value(tags_vector).expect("error");
println!("tags_json: {}", tags_json); // OK, this is correct so far
// THIS DOESN'T WORK
photo_data.replace_tags(tags_json.to_string());
let connection = super::database::establish_connection();
let photo_data_doc = super::serde_json::to_value(photo_data).expect("error converting to value");
super::database::new_photo(&connection, photo_data_doc);
Explained:
I have a web form to take in user inputs. User will typically type something like "food, movies, music" into the tags field. However, I need to convert this input into JSON array format, thus, something like "["music", "movies", "music"]" and then, replace the new JSON array string into the data structure and save into the database.
I tried to do photo_data.clone() and the compiler complained as well. I just could not get it work.
I'd appreciate if someone could show me how to replace the 'tags' in the PhotoDataInput data structure and then successfully save it into the db.
(I'm using Rocket and Diesel)