#cycle #arguments #argv #io

clircle

Detect IO circles in your CLI apps arguments

6 releases

0.3.0 Feb 28, 2021
0.2.0 Nov 24, 2020
0.1.3 Nov 8, 2020
0.1.0 Oct 31, 2020

#112 in Filesystem

Download history 2339/week @ 2021-08-11 2930/week @ 2021-08-18 2902/week @ 2021-08-25 2547/week @ 2021-09-01 2530/week @ 2021-09-08 2665/week @ 2021-09-15 2924/week @ 2021-09-22 3423/week @ 2021-09-29 2793/week @ 2021-10-06 2799/week @ 2021-10-13 2907/week @ 2021-10-20 3078/week @ 2021-10-27 2332/week @ 2021-11-03 2323/week @ 2021-11-10 2416/week @ 2021-11-17 2419/week @ 2021-11-24

9,867 downloads per month
Used in 16 crates (2 directly)

MIT/Apache

22KB
367 lines

Clircle

CI crates.io version MSRV

Clircle provides a cross-platform API to detect read / write cycles from your user-supplied arguments. You can get the important identifiers of a file (from a path) and for all three stdio streams, if they are piped from or to a file as well.

Why?

Imagine you want to read data from a couple of files and output something according to the contents of these files. If the user redirects the output of your program to one of the input files, you might end up in an infinite circle of reading and writing.

The crate provides the struct Identifier which is a platform dependent type alias, so that you can use it on all platforms and do not need to introduce any conditional compilation yourself. On both Unix and Windows systems, Identifier holds information to identify a file on a disk.

The Clircle trait is implemented on both of these structs and requires TryFrom for the clircle::Stdio enum and for &Path, so that all possible inputs can be represented as an Identifier. Finally, Clircle is a subtrait of Eq, so that the identifiers can be conveniently compared and circles can be detected. The clircle crate also provides some convenience functions around the comparison of Clircle implementors.

Why should I use this and not just fs::Metadata?

The clircle crate seamlessly works on Linux and Windows through a single API, so no conditional compilation is needed at all. Furthermore, MetadataExt is not stable on Windows yet, meaning you would have to dig into the Windows APIs yourself to get the information needed to identify a file.

Where did this crate come from?

This crate originated in a pull request to the bat project. The bat tool strives to be a drop-in replacement for the unix tool cat. Since cat detects these cycles, bat has to do so too, which is where most of this code came into play. However, it was decided, that the new logic was

  • useful for other projects and
  • too platform specific for bats scope.

So now, you can use clircle too!

Dependencies

~0.5–1.2MB
~28K SLoC