on master help sign in
Add cycle detection with clircle, now v0.2
commit21ae26cb17ad0dfe815ce001be84f40078c5a4a7 authorNiklas Mohrin <niklas.mohrin@gmail.com> dateWed 25 Nov 2020, 00:24 (6 years ago) parent8381945
4 files changed+65−5
Cargo.lock+36−5 Cargo.toml+1 src/controller.rs+14 src/input.rs+14
Cargo.lock+36−5
@@ -99,6 +99,7 @@ dependencies = [
9999 "assert_cmd",
100100 "atty",
101101 "clap",
102+ "clircle",
102103 "console",
103104 "content_inspector",
104105 "dirs",
@@ -218,6 +219,12 @@ version = "0.1.10"
218219source = "registry+https://github.com/rust-lang/crates.io-index"
219220checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
220221
222+[[package]]
223+name = "cfg-if"
224+version = "1.0.0"
225+source = "registry+https://github.com/rust-lang/crates.io-index"
226+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
227+
221228[[package]]
222229name = "chrono"
223230version = "0.4.19"
@@ -244,6 +251,18 @@ dependencies = [
244251 "vec_map",
245252]
246253
254+[[package]]
255+name = "clircle"
256+version = "0.2.0"
257+source = "registry+https://github.com/rust-lang/crates.io-index"
258+checksum = "e27a01e782190a8314e65cc94274d9bbcd52e05a9d15b437fe2b31259b854b0d"
259+dependencies = [
260+ "cfg-if 1.0.0",
261+ "nix",
262+ "serde",
263+ "winapi",
264+]
265+
247266[[package]]
248267name = "console"
249268version = "0.14.0"
@@ -280,7 +299,7 @@ version = "1.2.0"
280299source = "registry+https://github.com/rust-lang/crates.io-index"
281300checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
282301dependencies = [
283− "cfg-if",
302+ "cfg-if 0.1.10",
284303]
285304
286305[[package]]
@@ -290,7 +309,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
290309checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
291310dependencies = [
292311 "autocfg",
293− "cfg-if",
312+ "cfg-if 0.1.10",
294313 "lazy_static",
295314]
296315
@@ -442,7 +461,7 @@ version = "1.0.18"
442461source = "registry+https://github.com/rust-lang/crates.io-index"
443462checksum = "da80be589a72651dcda34d8b35bcdc9b7254ad06325611074d9cc0fbb19f60ee"
444463dependencies = [
445− "cfg-if",
464+ "cfg-if 0.1.10",
446465 "crc32fast",
447466 "libc",
448467 "miniz_oxide",
@@ -484,7 +503,7 @@ version = "0.1.15"
484503source = "registry+https://github.com/rust-lang/crates.io-index"
485504checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
486505dependencies = [
487− "cfg-if",
506+ "cfg-if 0.1.10",
488507 "libc",
489508 "wasi",
490509]
@@ -635,7 +654,7 @@ version = "0.4.11"
635654source = "registry+https://github.com/rust-lang/crates.io-index"
636655checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
637656dependencies = [
638− "cfg-if",
657+ "cfg-if 0.1.10",
639658]
640659
641660[[package]]
@@ -666,6 +685,18 @@ dependencies = [
666685 "autocfg",
667686]
668687
688+[[package]]
689+name = "nix"
690+version = "0.19.1"
691+source = "registry+https://github.com/rust-lang/crates.io-index"
692+checksum = "b2ccba0cfe4fdf15982d1674c69b1fd80bad427d293849982668dfe454bd61f2"
693+dependencies = [
694+ "bitflags",
695+ "cc",
696+ "cfg-if 1.0.0",
697+ "libc",
698+]
699+
669700[[package]]
670701name = "normalize-line-endings"
671702version = "0.3.0"
Cargo.toml+1
@@ -50,6 +50,7 @@ serde = { version = "1.0", features = ["derive"] }
5050serde_yaml = "0.8"
5151semver = "0.11"
5252path_abs = { version = "0.5", default-features = false }
53+clircle = "0.2.0"
5354
5455[dependencies.git2]
5556version = "0.13"
src/controller.rs+14
@@ -1,3 +1,4 @@
1+use std::convert::TryFrom;
12use std::io::{self, Write};
23
34use crate::assets::HighlightingAssets;
@@ -66,6 +67,14 @@ impl<'b> Controller<'b> {
6667 }
6768
6869 let attached_to_pager = output_type.is_pager();
70+ let rw_cycle_detected = !attached_to_pager && {
71+ let identifiers: Vec<_> = inputs
72+ .iter()
73+ .flat_map(clircle::Identifier::try_from)
74+ .collect();
75+ clircle::stdout_among_inputs(&identifiers)
76+ };
77+
6978 let writer = output_type.handle()?;
7079 let mut no_errors: bool = true;
7180
@@ -78,6 +87,11 @@ impl<'b> Controller<'b> {
7887 }
7988 };
8089
90+ if rw_cycle_detected {
91+ print_error(&"The output file is also an input!".into(), writer);
92+ return Ok(false);
93+ }
94+
8195 for (index, input) in inputs.into_iter().enumerate() {
8296 match input.open(io::stdin().lock()) {
8397 Err(error) => {
src/input.rs+14
@@ -1,6 +1,8 @@
1+use std::convert::TryFrom;
12use std::ffi::{OsStr, OsString};
23use std::fs::File;
34use std::io::{self, BufRead, BufReader, Read};
5+use std::path::Path;
46
57use content_inspector::{self, ContentType};
68
@@ -191,6 +193,18 @@ impl<'a> Input<'a> {
191193 }
192194}
193195
196+impl TryFrom<&'_ Input<'_>> for clircle::Identifier {
197+ type Error = ();
198+
199+ fn try_from(input: &Input) -> std::result::Result<clircle::Identifier, ()> {
200+ match input.kind {
201+ InputKind::OrdinaryFile(ref path) => Self::try_from(Path::new(path)).map_err(|_| ()),
202+ InputKind::StdIn => Self::try_from(clircle::Stdio::Stdin).map_err(|_| ()),
203+ InputKind::CustomReader(_) => Err(()),
204+ }
205+ }
206+}
207+
194208pub(crate) struct InputReader<'a> {
195209 inner: Box<dyn BufRead + 'a>,
196210 pub(crate) first_line: Vec<u8>,