Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Meta: Collect relevant use cases from the cookbook #26

Open
killercup opened this issue Jan 27, 2018 · 11 comments
Open

Meta: Collect relevant use cases from the cookbook #26

killercup opened this issue Jan 27, 2018 · 11 comments
Labels
help wanted Extra attention is needed looking for feedback

Comments

@killercup
Copy link
Owner

The Rust Cookbook has a bunch of

simple examples that demonstrate good practices to accomplish common programming tasks, using the crates of the Rust ecosystem.

Many of these examples can work as CLI apps. We should go through them and see if there are some common patterns that we want to offer abstractions for.

@killercup killercup added help wanted Extra attention is needed looking for feedback labels Jan 27, 2018
@killercup
Copy link
Owner Author

My first try: I implemented this script to resize JPGs in #23

It uses glob, rayon, and image. I think of these three, glob and rayon are general purpose for CLI tools but dealing with image is more special purpose.

@mattgathu
Copy link
Contributor

I can try implementing a simple archiver app based on the decompress a tarball & compress directory into tarball cookbooks.
This will use the flate2 and tar crates.

@killercup
Copy link
Owner Author

Sure, go ahead! Feel free to post a link to a repo here. I'd love to hear what surprised you with quicli and what was missing :)

@deg4uss3r
Copy link

I can write a simple nc-like tool emulating Listen on unused port TCP/IP cookbook example :)

@killercup
Copy link
Owner Author

Go right ahead! I'd to get feedback on this :) Even just a gist with a few lines of Rust code and a Cargo.toml work!

@deg4uss3r
Copy link

deg4uss3r commented Feb 1, 2018

Here's the main.rs and the Cargo.toml

@killercup
Copy link
Owner Author

Looks good, @deg4uss3r!

This waits for the connection to close before printing, right? I think you could wrap tcp_listener in a std::io::BufReader and then use for line in buffered_tcp_listener.line() to print the lines as they come it. Might be a nice experiment (and a cool example for a tutorial), haven't tested that, though!

By the way: You put these files into the same gist – and have it compile – by telling cargo the explicit path to your main.rs. If you do this:

[[bin]]
path = "main.rs"

you can just drop the two files into the same directory.

I'm pretty sure you can specify the Ipv4Addr type directly in your Cli struct, by the way.

@deg4uss3r
Copy link

deg4uss3r commented Feb 4, 2018

I updated the main.rs gist per your suggestions. I like it a lot more now, but it does deviate a bit from the Cookbook example now.

@vitiral
Copy link
Contributor

vitiral commented Feb 5, 2018

@killercup feel free to steal ideas from the ergo cookbook examples.

I just pushed all of the major "core crates" and am going to start working on getting the ergo cookbook started.

@mattgathu
Copy link
Contributor

I've been writing a simple cli tool using quicli for zip and tar files packing and unpacking:

My thoughts so far:

  • Supporting the use ? in main! is heaven sent! ✅
  • The wildcard import use quicli::prelude::*; IMO hides information. I'm never sure what is available (in scope) and tracking imports is a bit hard. I had to consult the docs a lot! I'd prefer encouraging the use of explicit imports.
  • I hadn't used StructOpt before and it slowed me down figuring how it fits with clap, more so when adding extra argument options e.g. argument x conflicts with argument y. But I'm happy clap 3 will be adopting what StructOpt has done.
  • The getting started guide uses struct Cli to define the arguments and uses it as args in main!. I fumbled a bit before I realised args refers to struct Cli. Maybe renaming struct Cli to struct Args? 🤷‍♂️ (In hindsight this seems so trivial)

@killercup
Copy link
Owner Author

Thanks for the great feedback, @mattgathu!

I've been writing a simple cli tool using quicli for zip and tar files packing and unpacking

Cool! Do you have a public repo? I'd love to collect some "quicli in the wild" links… and opened #62 for that :)

The wildcard import use quicli::prelude::*; IMO hides information.

Very good point. This because of two things:

  • I'm lazy (we can fix that)
  • Exporting (proc-)macros is AFAIK only possible with pub foo::* syntax (we can't easily fix that)

I hadn't used StructOpt before and it slowed me down figuring how it fits with clap

Ah, interesting! Do you think it'd make sense to cover more advanced structopt usage in a guide? (How to do subcommands was requested, too.)

The getting started guide uses struct Cli to define the arguments and uses it as args in main!. I fumbled a bit before I realised args refers to struct Cli.

Yeah, sorry, that's not explicitly mentioned I believe. The closure-like syntax in main!(|args: Cli| { … }); was used to suggest that args is of type Cli, but that may not be obvious, especially because you usually don't need type annotations in Rust's closures.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed looking for feedback
Projects
None yet
Development

No branches or pull requests

4 participants