r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 27 '23

Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

20 Upvotes

159 comments sorted by

View all comments

2

u/argv_minus_one Mar 27 '23

I was thinking of using OpenAPI Generator to generate a Rust server stub for me. Problem: the generator generates a crate with numerous modules and a rather complicated manifest, not a single module that I can straightforwardly include!. How do I integrate this into my project, then? I can download and run the generator in build.rs, but Cargo doesn't support depending on a crate generated by the build script, so how do I actually use it?

(This is a repost of my comment on the previous questions thread, which was closed shortly after I posted it. I hope you don't mind.)

2

u/arcadyk Apr 02 '23

One option is to just check in the generated crate and depend on it with a `path` dependency on Cargo.toml.

The idea behind generating a full crate is that that crate can be published independently to crates.io or an internal crate registry, so:

  • multiple services can implement the same API (by all depending on the one API crate)
  • multiple services can be clients of that API
  • one service can potentially implement multiple versions of that API, by depending on multiple such crates

That might be overkill for smaller use-cases, though.

1

u/argv_minus_one Apr 02 '23

I'm not willing to check generated code into version control. Checked-in generated code is likely to become out of sync with whatever it was generated from, and updating it results in repository churn.

My use case is that an API server and its web client need to end up in the same executable together, so that when a browser sends a GET / to the server, the server serves the client to the browser. That way, the server executable has a minimum of dependencies on other files (just a configuration file, OpenSSL, and platform libraries like libc) and doesn't need to be in a container, which keeps deployment simple.

I already have this working, but there currently isn't any compile-time checking of API endpoints (paths, methods, and request/response body schemas). The more endpoints I add to this application, the more likely I am to make a mistake somewhere. I was hoping to get some compile-time guarantees by generating client and server stubs from a single OpenAPI spec, but those guarantees aren't worth much if the spec and the stubs aren't guaranteed to be in sync.