diff --git a/Cargo.lock b/Cargo.lock index d0bbed3..93be9b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,6 +33,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + [[package]] name = "android_system_properties" version = "0.1.5" @@ -128,9 +134,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bit-set" @@ -195,13 +201,13 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.24" +version = "0.4.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" +checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" dependencies = [ + "android-tzdata", "iana-time-zone", "js-sys", - "num-integer", "num-traits", "serde", "time", @@ -292,7 +298,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.18", ] [[package]] @@ -360,9 +366,9 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.1" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" dependencies = [ "cfg-if", "crossbeam-utils", @@ -370,9 +376,9 @@ dependencies = [ [[package]] name = "crossbeam-deque" -version = "0.8.1" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" dependencies = [ "cfg-if", "crossbeam-epoch", @@ -381,13 +387,13 @@ dependencies = [ [[package]] name = "crossbeam-epoch" -version = "0.9.5" +version = "0.9.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd" +checksum = "46bd5f3f85273295a9d14aedfb86f6aadbff6d8f5295c4a9edb08e819dcf5695" dependencies = [ + "autocfg", "cfg-if", "crossbeam-utils", - "lazy_static", "memoffset", "scopeguard", ] @@ -433,9 +439,9 @@ dependencies = [ [[package]] name = "either" -version = "1.6.1" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" [[package]] name = "env_logger" @@ -509,14 +515,14 @@ checksum = "e77ac7b51b8e6313251737fcef4b1c01a2ea102bde68415b62c0ee9268fec357" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.18", ] [[package]] name = "half" -version = "1.7.1" +version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" [[package]] name = "hashbrown" @@ -551,6 +557,15 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.1" @@ -607,9 +622,9 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ "hermit-abi 0.3.1", "libc", @@ -636,19 +651,13 @@ checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb" [[package]] name = "itertools" -version = "0.10.1" +version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" dependencies = [ "either", ] -[[package]] -name = "itoa" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" - [[package]] name = "itoa" version = "1.0.6" @@ -657,9 +666,9 @@ checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" [[package]] name = "js-sys" -version = "0.3.55" +version = "0.3.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cc9ffccd38c451a86bf13657df244e9c3f37493cce8e5e21e940963777acc84" +checksum = "2f37a4a5928311ac501dee68b3c7613a1037d0edb30c8e5427bd832d55d1b790" dependencies = [ "wasm-bindgen", ] @@ -684,12 +693,9 @@ checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" [[package]] name = "log" -version = "0.4.17" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] +checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de" [[package]] name = "lru" @@ -717,9 +723,9 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "memoffset" -version = "0.6.4" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" +checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" dependencies = [ "autocfg", ] @@ -751,7 +757,7 @@ checksum = "4901771e1d44ddb37964565c654a3223ba41a594d02b8da471cc4464912b5cfa" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.18", ] [[package]] @@ -880,35 +886,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a652d9771a63711fd3c3deb670acfbe5c30a4072e664d7a3bf5a9e1056ac72c3" dependencies = [ "arrayvec 0.7.2", - "itoa 1.0.6", -] - -[[package]] -name = "num-integer" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" -dependencies = [ - "autocfg", - "num-traits", + "itoa", ] [[package]] name = "num-traits" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" dependencies = [ "autocfg", ] [[package]] name = "num_cpus" -version = "1.13.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" dependencies = [ - "hermit-abi 0.1.19", + "hermit-abi 0.2.6", "libc", ] @@ -920,9 +916,9 @@ checksum = "80adb31078122c880307e9cdfd4e3361e6545c319f9b9dcafcb03acd3b51a575" [[package]] name = "once_cell" -version = "1.17.1" +version = "1.17.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +checksum = "9670a07f94779e00908f3e686eab508878ebb390ba6e604d3a284c00e8d0487b" [[package]] name = "oorandom" @@ -944,9 +940,9 @@ checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" [[package]] name = "plotters" -version = "0.3.1" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a3fd9ec30b9749ce28cd91f255d569591cdf937fe280c312143e3c4bad6f2a" +checksum = "2538b639e642295546c50fcd545198c9d64ee2a38620a628724a3b266d5fbf97" dependencies = [ "num-traits", "plotters-backend", @@ -957,24 +953,24 @@ dependencies = [ [[package]] name = "plotters-backend" -version = "0.3.2" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d88417318da0eaf0fdcdb51a0ee6c3bed624333bff8f946733049380be67ac1c" +checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142" [[package]] name = "plotters-svg" -version = "0.3.1" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "521fa9638fa597e1dc53e9412a4f9cefb01187ee1f7413076f9e6749e2885ba9" +checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f" dependencies = [ "plotters-backend", ] [[package]] name = "proc-macro2" -version = "1.0.58" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8" +checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b" dependencies = [ "unicode-ident", ] @@ -991,35 +987,32 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.27" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" +checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" dependencies = [ "proc-macro2", ] [[package]] name = "rayon" -version = "1.5.1" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" dependencies = [ - "autocfg", - "crossbeam-deque", "either", "rayon-core", ] [[package]] name = "rayon-core" -version = "1.9.1" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" dependencies = [ "crossbeam-channel", "crossbeam-deque", "crossbeam-utils", - "lazy_static", "num_cpus", ] @@ -1082,9 +1075,9 @@ checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06" [[package]] name = "ryu" -version = "1.0.5" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" [[package]] name = "same-file" @@ -1118,16 +1111,16 @@ checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.18", ] [[package]] name = "serde_json" -version = "1.0.68" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8" +checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" dependencies = [ - "itoa 0.4.8", + "itoa", "ryu", "serde", ] @@ -1169,7 +1162,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 1.0.76", + "syn 1.0.109", ] [[package]] @@ -1202,20 +1195,20 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.76" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] name = "syn" -version = "2.0.16" +version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01" +checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" dependencies = [ "proc-macro2", "quote", @@ -1300,7 +1293,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.18", ] [[package]] @@ -1345,7 +1338,7 @@ checksum = "2c3e1c30cedd24fc597f7d37a721efdbdc2b1acae012c1ef1218f4c7c2c0f3e7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.16", + "syn 2.0.18", ] [[package]] @@ -1359,9 +1352,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.8" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" [[package]] name = "unicode-linebreak" @@ -1375,15 +1368,9 @@ dependencies = [ [[package]] name = "unicode-width" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" - -[[package]] -name = "unicode-xid" -version = "0.2.2" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" [[package]] name = "utf8-width" @@ -1426,12 +1413,11 @@ dependencies = [ [[package]] name = "walkdir" -version = "2.3.2" +version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" dependencies = [ "same-file", - "winapi", "winapi-util", ] @@ -1449,9 +1435,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.78" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "632f73e236b219150ea279196e54e610f5dbafa5d61786303d4da54f84e47fce" +checksum = "5bba0e8cb82ba49ff4e229459ff22a191bbe9a1cb3a341610c9c33efc27ddf73" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -1459,24 +1445,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.78" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a317bf8f9fba2476b4b2c85ef4c4af8ff39c3c7f0cdfeed4f82c34a880aa837b" +checksum = "19b04bc93f9d6bdee709f6bd2118f57dd6679cf1176a1af464fca3ab0d66d8fb" dependencies = [ "bumpalo", - "lazy_static", "log", + "once_cell", "proc-macro2", "quote", - "syn 1.0.76", + "syn 2.0.18", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.78" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d56146e7c495528bf6587663bea13a8eb588d39b36b679d83972e1a2dbbdacf9" +checksum = "14d6b024f1a526bb0234f52840389927257beb670610081360e5a03c5df9c258" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1484,28 +1470,28 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.78" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7803e0eea25835f8abdc585cd3021b3deb11543c6fe226dcd30b228857c5c5ab" +checksum = "e128beba882dd1eb6200e1dc92ae6c5dbaa4311aa7bb211ca035779e5efc39f8" dependencies = [ "proc-macro2", "quote", - "syn 1.0.76", + "syn 2.0.18", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.78" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0237232789cf037d5480773fe568aac745bfe2afbc11a863e97901780a6b47cc" +checksum = "ed9d5b4305409d1fc9482fee2d7f9bcbf24b3972bf59817ef757e23982242a93" [[package]] name = "web-sys" -version = "0.3.55" +version = "0.3.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38eb105f1c59d9eaa6b5cdc92b859d85b926e82cb2e0945cd0c9259faa6fe9fb" +checksum = "3bdd9ef4e984da1187bf8110c5cf5b845fbc87a23602cdf912386a76fcd3a7c2" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/src/formatting.rs b/src/formatting.rs index 766e8d4..5923bb4 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -1,39 +1,18 @@ -use crate::{config::Config, Input, Session}; -use log::{debug, trace}; +use crate::config::Config; +use log::trace; use nu_parser::{flatten_block, parse, FlatShape}; use nu_protocol::engine::{self, StateWorkingSet}; -use std::fs::File; -use std::io::Write; - -impl<'b, T: Write + 'b> Session<'b, T> { - pub fn format_input_inner(&mut self, input: Input) { - println!("formatting ...💭"); - format_project(input, &self.config); - } -} // Format an entire crate (or subset of the module tree) -// -// TODO: It is possible that this fucntion return some value. -// For example a Vec[u8], or a String to pass to another function -// which writes the file, or add the indentation. -fn format_project(input: Input, config: &Config) { - debug!("using config:{:?}", config); +pub fn format_inner(contents: &[u8], _config: &Config) -> Vec { // nice place to measure parsing and formatting time // let mut timer = Timer::start(); // parsing starts - let main_file = input.file_name(); - let main_file_as_str = main_file.unwrap().as_path().to_str().unwrap(); - // TODO: if input is stdin, format the string - // let input_is_stdin = main_file == Input::Text; - - let contents = input.contents(); - let engine_state = engine::EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let parsed_block = parse(&mut working_set, Some(main_file_as_str), &contents, false); + let parsed_block = parse(&mut working_set, None, contents, false); trace!("parsed block:\n{:?}\n", &parsed_block); // flat is a list of (Span , Flatshape) // @@ -44,7 +23,6 @@ fn format_project(input: Input, config: &Config) { // timer = timer.done_parsing() // formatting starts - let mut writer = File::create(main_file.unwrap()).unwrap(); let mut out: Vec = vec![]; for (span, shape) in flat { @@ -93,24 +71,14 @@ fn format_project(input: Input, config: &Config) { out = insert_newline(out); } - _ => (), + _ => out.extend(c_bites), } } - // writing - // just before writing, append a new line to the file. out = insert_newline(out); - // TODO: check if the last byte is already b'\n' - - // now write the file - let file_bites = out.as_slice(); - trace!("writing {:?}", out); - writer - .write_all(file_bites) - .expect("something went wrong writing"); - trace!("written") // timer = timer.done_formatting() + out } fn insert_newline(mut bytes: Vec) -> Vec { diff --git a/src/lib.rs b/src/lib.rs index 020904b..25baaaa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,71 +3,45 @@ //! //! It does not do anything more than that, which makes it so fast. -use log::trace; +use config::Config; +use formatting::format_inner; +use log::{debug, trace}; use std::error::Error; +use std::fs::File; use std::io::{BufReader, BufWriter, Read, Write}; use std::path::PathBuf; pub mod config; pub mod formatting; -pub struct Session<'b, T: Write> { - pub config: config::Config, - pub out: Option<&'b mut T>, - pub has_operational_errors: bool, -} +/// Reads the file and format it. After that, writes the file inplace +pub fn format_single_file(file: &PathBuf, config: &Config) { + // read the contents of the file + let contents = std::fs::read(file) + .unwrap_or_else(|_| panic!("something went wrong reading the file {}", file.display())); -impl<'b, T: Write + 'b> Session<'b, T> { - pub fn new(config: config::Config, out: Option<&'b mut T>) -> Self { - Self { - config, - out, - has_operational_errors: false, - } - } - - pub fn has_operational_errors(self) -> bool { - self.has_operational_errors - } + // obtain the formatted file + let formatted_bytes = format_inner(&contents, config); - pub fn add_operational_error(&mut self) { - self.has_operational_errors = true; + // compare the contents + if formatted_bytes == contents { + debug!("File is formatted correctly.") } - pub fn format(&mut self, input: Input) { - self.format_input_inner(input) - } -} - -// This is getting deprecated. I think -#[derive(Debug, PartialEq)] -pub enum FileName { - Real(PathBuf), - Stdin, + // write down the file to path + let mut writer = File::create(file).unwrap(); + let file_bites = formatted_bytes.as_slice(); + trace!("writing {:?}", formatted_bytes); + writer + .write_all(file_bites) + .expect("something went wrong writing"); + trace!("written") } -#[derive(Debug)] -pub enum Input { - File(PathBuf), - Text(String), -} - -impl Input { - fn file_name(&self) -> Option<&PathBuf> { - match *self { - Input::File(ref file) => Some(file), - Input::Text(..) => None, - } - } - - fn contents(&self) -> Vec { - match self { - Input::File(path) => std::fs::read(path).unwrap_or_else(|_| { - panic!("something went wrong reading the file {}", path.display()) - }), - Input::Text(string) => string.as_bytes().to_vec(), - } - } +pub fn format_string(input_string: &String, config: &Config) -> String { + let contents = input_string.as_bytes(); + let formatted_bytes = format_inner(contents, config); + String::from_utf8(formatted_bytes).unwrap() } /// @@ -239,24 +213,11 @@ where mod test { use super::*; - #[test] - fn already_formatted() { - let expected = "[ - { - \"a\": 0 - }, - {}, - { - \"a\": null - } -]"; - assert_eq!(expected, format_nu(expected, Indentation::Default)); - } - #[test] fn array_of_object() { - let nu = "[{\"a\": 0}, {}, {\"a\": null}]"; - let expected = "[ + let expected = String::from("[{\"a\":0},{},{\"a\":null}]\n"); + let nu = String::from( + "[ { \"a\": 0 }, @@ -264,8 +225,9 @@ mod test { { \"a\": null } -]"; - assert_eq!(expected, format_nu(nu, Indentation::Default)); +]", + ); + assert_eq!(expected, format_string(&nu, &Config::default())); } #[test] @@ -300,23 +262,4 @@ mod test { let expected = "0"; assert_eq!(expected, format_nu(nu, Indentation::Default)); } - - #[test] - fn simple_array() { - let nu = "[1,2,null]"; - let expected = "[ - 1, - 2, - null -]"; - assert_eq!(expected, format_nu(nu, Indentation::Default)); - } - #[test] - fn simple_object() { - let nu = "{\"a\":0}"; - let expected = "{ - \"a\": 0 -}"; - assert_eq!(expected, format_nu(nu, Indentation::Default)); - } } diff --git a/src/main.rs b/src/main.rs index 5035ef9..b69eda7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,42 +2,72 @@ //! //! # Usage //! -//! `nufmt` is inteded to be used as this: -//! -//! To format a single file -//! ```shell -//! nufmt file1.nu +//! ```text +//! nufmt [OPTIONS] [FILES] ... //! ``` +//! ## Files //! -//! `TODO!` +//! `Files` are a list of files. It cannot be used combined with `--stdin`. +//! You can format many files with one command!. For example: //! -//! Set options file -//! ```shell -//! nufmt --config nufmt.nuon +//! ```text +//! nufmt my-file1.nu my-file2.nu my-file3.nu //! ``` +//! +//! ## Options +//! +//! - `-s` or `--stdin` formats from `stdin`, returns to `stdout` as a String. It cannot be used combined with `files`. +//! +//! - `-c` or `--config` pass the config file path. +//! +//! Sample: +//! +//! ```text +//! nufmt --config my-config.json +//! ``` +//! +//! or +//! +//! ```text +//! nufmt --stdin --config my-stdin-config.json +//! ``` +//! +//! - `-h` or `--help` show help and exit +//! +//! - `-v` or `--version` prints the version and exit -// throw error if are docs missing -// or finds a broken link doc +// throw error if finds a broken link in doc #![deny(rustdoc::broken_intra_doc_links)] +// or docs are missing for public members #![warn(missing_docs)] -// for debug purposes, allow unused imports and variables -#[allow(unused)] -#[allow(unused_imports)] -#[allow(unused_import_braces)] -use anyhow::Result; +use anyhow::{Ok, Result}; use clap::Parser; +use log::trace; use nufmt::config::Config; -use nufmt::{Input, Session}; +use nufmt::{format_single_file, format_string}; use std::error::Error; -use std::io::{stdout, Write}; +use std::io::Write; use std::path::PathBuf; +const SUCCESSFUL_EXIT: i32 = 0; +const FAILED_EXIT: i32 = 1; + #[derive(Parser)] #[command(author, version, about)] struct Cli { - #[arg(help = "The file or files you want to format in nu")] + #[arg( + required_unless_present("stdin"), + help = "The file or files you want to format in nu" + )] files: Vec, + #[arg( + short, + long, + conflicts_with = "files", + help = "Format the code passed in stdin as a string." + )] + stdin: Option, #[arg(short, long, help = "The configuration file")] config: Option, } @@ -47,17 +77,33 @@ fn main() -> Result<(), Box> { env_logger::init(); let cli = Cli::parse(); + trace!("recieved cli.files: {:?}", cli.files); + trace!("recieved cli.stdin: {:?}", cli.stdin); + trace!("recieved cli.config: {:?}", cli.config); - let exit_code = match execute(cli.files, Config::default()) { - Ok(code) => code, - Err(e) => { - eprintln!("{:#}", e); - 1 + let cli_config = match cli.config { + None => Config::default(), + Some(input_cli) => { + todo!( + "cannot read from {:?} Reading a config from file not implemented!", + input_cli + ) } }; + + // Note the deref and reborrow here to obtain a slice + // so rust doesnt complain for the [] arm + let exit_code = match &*cli.files { + // if cli.files is an empty list, + // it means the flag --stdin was passed + [] => execute_string(cli.stdin, &cli_config)?, + _ => execute_files(cli.files, &cli_config)?, + }; + // Make sure standard output is flushed before we exit. std::io::stdout().flush().unwrap(); + trace!("exit code: {exit_code}"); // Exit with given exit code. // // NOTE: this immediately terminates the process without doing any cleanup, @@ -65,49 +111,36 @@ fn main() -> Result<(), Box> { std::process::exit(exit_code); } +/// returns the string formatted to `stdout` +fn execute_string(string: Option, options: &Config) -> Result { + // format the string + let output = format_string(&string.unwrap(), options); + println!("output: \n{output}"); + + Ok(SUCCESSFUL_EXIT) +} + /// Sends the files to format in lib.rs -fn execute(files: Vec, options: Config) -> Result { - // open a session - let out = &mut stdout(); - let mut session = Session::new(options, Some(out)); - - for file in files { - // TODO: this would be a great place to create an enum like - // enum - // enum File { - // stdin, - // single_file, - // folder, - // _mod, - // } +fn execute_files(files: Vec, options: &Config) -> Result { + // walk the files in the vec of files + for file in files.iter() { if !file.exists() { eprintln!("Error: {} not found!", file.to_str().unwrap()); - session.add_operational_error() + return Ok(FAILED_EXIT); } else if file.is_dir() { - // TODO: recursive search eprintln!( "Error: {} is a directory. Please pass files only.", file.to_str().unwrap() ); - session.add_operational_error() + return Ok(FAILED_EXIT); } else { // send the file to lib.rs println!("formatting file: {:?}", file); - format_and_emit_report(&mut session, Input::File(file)); + format_single_file(file, options); } } - let exit_code = if session.has_operational_errors() { - 1 - } else { - 0 - }; - - Ok(exit_code) -} - -fn format_and_emit_report(session: &mut Session<'_, T>, input: Input) { - session.format(input); + Ok(SUCCESSFUL_EXIT) } #[cfg(test)]