• 11 Posts
  • 25 Comments
Joined 1 year ago
cake
Cake day: June 25th, 2023

help-circle





  • I managed to get this working, but there has to be a better way. How else could I write this?

      pub async fn insert_or_return_user(
            db: &DbConn,
            partial_user: Auth0UserPart,
        ) -> Result {
            let user = users::ActiveModel {
                email: Set(partial_user.email.to_owned()),
                email_verified: Set(partial_user.email_verified.to_owned()),
                auth0_sub: Set(partial_user.sub.to_owned()),
                ..Default::default()
            };
    
            let result = user.clone().insert(db).await;
    
            match result {
                Ok(u) => {
                    println!("{u:#?}");
                    Ok(u.try_into_model().unwrap() as UsersModel)
                }
                Err(error) => {
                    let user = Users::find()
                        .filter(users::Column::Auth0Sub.eq(&partial_user.sub))
                        .one(db)
                        .await?;
    
                    Ok(user.unwrap() as UsersModel)
                }
            }
        }
    




















  • Error handling was kind of a pain to wrap my head around within the Rust Ecosystem, between the different crates, custom enums and learning about Box<dyn Error>. I do enjoy errors now that I understand how the community is using them a little better, and the idea of there being one control flow with errors being a possible ‘result’.

    Author seems to have some good experience composing errors in Rust applications… good read!



  • I’d provide more code but it’s a mess and full of old commented-out code. Your examples are perfect! combining the DB fields into it’s own struct is something I hadn’t thought of… and I totally get why having a bunch of options sitting in the Army struct would be problematic. I’m really excited about rust for moving these sorts of errors to compile time.

    The INTO example seems great too. I’m ok with the performance hit of cloning for now… lifetimes and pointers feel like a tier above where am at with my rust skills, and I’ll circle back to get a better handle on them later.

    One question about the INTO example… I always hear it’s better to just implement FROM and get INTO for free. Does that not make sense for my use case? If I did it, would it look something like:

    impl From<ArmyWithDbProps> for Army { fn from(self) -> ArmyWithDbProps { self.armyWithDbProps } }