Guest User

Training fetch code

a guest
Jul 31st, 2023
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.65 KB | None | 0 0
  1. for training_id in training_iter {
  2.     let training_id = training_id.map_err(|e| format!("Error pulling worker trainings: {}", e))?;
  3.  
  4.     // get training from library
  5.     let training = match TRAINING_LIB.with_lock(|lib| lib.get(&training_id.to_string())) {
  6.         // if training is already in library, return i
  7.         Some(training) => training,
  8.         // if training is not in library, pull from database and add to library
  9.         None => {
  10.             match TRAINING_LIB.with_lock(|lib| {
  11.                 // pull training from database
  12.                 let training = Training::pull_new(&training_id.to_string(), &conn).map_err(|e| format!("Error pulling worker trainings: {}", e));
  13.                 // add training to library
  14.                 Ok(match training {
  15.                     Ok(training) => lib.add(&training_id.to_string(), training),
  16.                     Err(e) => {
  17.                         return Err(e);
  18.                     }
  19.                 })
  20.             }) {
  21.                 // if training was successfully added to library, return it
  22.                 Ok(opt) => match opt {
  23.                     Some(training) => training,
  24.                     // if training was not found in database, return an error
  25.                     None => return Err(format!("Error pulling worker trainings: training with id {} not found", training_id).into()),
  26.                 },
  27.                 // if training could not be added to library, return an error
  28.                 Err(e) => {
  29.                     return Err(format!("Error adding training to library: {}", e).into());
  30.                 }
  31.             }
  32.         }
  33.     };
  34.  
  35.     worker_trainings.push(training);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment