Skip Navigation

InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)NE
Posts
11
Comments
33
Joined
2 yr. ago

Rust @programming.dev

Hoping for clarity on how Rust works in these situations

  • 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)
                }
            }
        }
    
    
      
  • Rust @programming.dev

    Is there a sea-orm equivalent to findOrCreate? How else would I do this?

    Rust @programming.dev

    Second attempt: What's wrong with how I'm deserializing JSON here?

  • I got the response wrong, here's what I'm using that isn't working:

     
        
    enum NationResponse {
        Nation(Nation),
        People(Vec),
    }
    
      

    Why the heck can't I edit the original post after a comment is made?

  • Oh man, I didn't know debug_handler existed. Sure enough I had a missing derived attribute... not sure how but Serde serialize and deserialize were missing, so when I was trying to return Ok(Json(army)) it was failing. Thanks so much!

  • Rust @programming.dev

    Lost on this Axum error... any thoughts?

    Rust @programming.dev

    How can I create a mutable vector singleton?

  • Also, move out special types to types.rs, error types to errors.rs to keep the area with the actual algorithms more clear.

    Ok this is totally something my code base needs. Very actionable feedback.

    And yeah that's one of the things I love about rust; it will tell me everywhere things are out of wack. It's such a different experience from back when I had large JavaScript code bases. Make changes and pray lol.

  • This is really good to hear, I don't think I'm as far off base as I thought; maybe I've been over thinking it a bit. And thanks for that refactoring resource. I'm very big into making my TS code clean and maintainable. I'm just thrown off a bit with the new paradigm.

  • Rust @programming.dev

    Where do I put stuff in a growing codebase?

    Rust @programming.dev

    What's the procedure for mocking structs?

    Rust @programming.dev

    Can I not use dot in filename for a Rust module?

    Rust @programming.dev

    Questions about how to organize custom types / structs

    Rust @programming.dev

    What's the Rust way of storing fetched data from runtime for later use in the program?

    Rust @programming.dev

    Is there something like /r/learnrust around here?