diff --git a/.github/main.yml b/.github/workflows/main.yml similarity index 51% rename from .github/main.yml rename to .github/workflows/main.yml index 421a98d..5da2294 100644 --- a/.github/main.yml +++ b/.github/workflows/main.yml @@ -29,11 +29,6 @@ jobs: os: windows-latest rust: stable target: x86_64-pc-windows-msvc - - name: msrv - os: ubuntu-22.04 - # sync MSRV with docs: guide/src/guide/installation.md and Cargo.toml - rust: 1.88.0 - target: x86_64-unknown-linux-gnu name: ${{ matrix.name }} steps: - uses: actions/checkout@v5 @@ -42,4 +37,45 @@ jobs: - name: Build and run tests run: cargo test --workspace --locked --target ${{ matrix.target }} - name: Test no default - run: cargo test --workspace --no-default-features --target ${{ matrix.target }} \ No newline at end of file + run: cargo test --workspace --no-default-features --target ${{ matrix.target }} + + format: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - name: Install Rust + run: bash ci/install-rust.sh stable x86_64-unknown-linux-gnu + - run: rustup component add rustfmt + - run: cargo fmt --check + + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - name: Install Rust + run: bash ci/install-rust.sh stable x86_64-unknown-linux-gnu + - run: rustup component add clippy + - run: cargo clippy --workspace --all-targets --no-deps -- -D warnings + + build: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v5 + - name: Install Rust + run: bash ci/install-rust.sh stable aarch64-unknown-linux-musl + - name: Build + run: cargo build --locked --target aarch64-unknown-linux-musl + + success: + name: Success gate + if: always() + needs: + - test + - format + - lint + - build + runs-on: ubuntu-latest + steps: + - run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}' + - name: Done + run: exit 0 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 70c1858..83bc644 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Rust build artifacts /target/ -Cargo.lock # IDE .vscode/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c7a5149 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,551 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "assert_cmd" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c5bcfa8749ac45dd12cb11055aeeb6b27a3895560d60d71e3c23bf979e60514" +dependencies = [ + "anstyle", + "bstr", + "libc", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + +[[package]] +name = "bstr" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63044e1ae8e69f3b5a92c736ca6269b8d12fa7efe39bf34ddb06d102cf0e2cab" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "clap" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "float-cmp" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09cf3155332e944990140d967ff5eceb70df778b34f77d8075db46e4704e6d8" +dependencies = [ + "num-traits", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indexmap" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" + +[[package]] +name = "libc" +version = "0.2.180" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" + +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + +[[package]] +name = "lsmkd" +version = "1.0.0" +dependencies = [ + "assert_cmd", + "clap", + "predicates", + "regex", + "serde", + "serde_json", + "serde_yaml", + "tempfile", +] + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "predicates" +version = "3.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d19ee57562043d37e82899fade9a22ebab7be9cef5026b07fda9cdd4293573" +dependencies = [ + "anstyle", + "difflib", + "float-cmp", + "normalize-line-endings", + "predicates-core", + "regex", +] + +[[package]] +name = "predicates-core" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "727e462b119fe9c93fd0eb1429a5f7647394014cf3c04ab2c0350eeb09095ffa" + +[[package]] +name = "predicates-tree" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72dd2d6d381dfb73a193c7fca536518d7caee39fc8503f74e7dc0be0531b425c" +dependencies = [ + "predicates-core", + "termtree", +] + +[[package]] +name = "proc-macro2" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" + +[[package]] +name = "rustix" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "ryu" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a50f4cf475b65d88e057964e0e9bb1f0aa9bbb2036dc65c64596b42932536984" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "655da9c7eb6305c55742045d5a8d2037996d61d8de95806335c7c86ce0f82e9c" +dependencies = [ + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys", +] + +[[package]] +name = "termtree" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683" + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "wait-timeout" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" +dependencies = [ + "libc", +] + +[[package]] +name = "wasip2" +version = "1.0.1+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "wit-bindgen" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "zmij" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd8f3f50b848df28f887acb68e41201b5aea6bc8a8dacc00fb40635ff9a72fea" diff --git a/Makefile b/Makefile index cb3d14d..7d6c681 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,9 @@ clean: install: cargo install --path . +lint: + cargo clippy --workspace --all-targets --no-deps -- -D warnings + help: @echo "Available targets:" @echo " build - Build debug version" diff --git a/ci/install-rust.sh b/ci/install-rust.sh new file mode 100644 index 0000000..730a7ae --- /dev/null +++ b/ci/install-rust.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# Install/update rust. +# The first argument should be the toolchain to install. + +set -ex +if [ -z "$1" ] +then + echo "First parameter must be toolchain to install." + exit 1 +fi +TOOLCHAIN="$1" + +rustup set profile minimal +rustup component remove --toolchain=$TOOLCHAIN rust-docs || echo "already removed" +rustup update --no-self-update $TOOLCHAIN +if [ -n "$2" ] +then + TARGET="$2" + HOST=$(rustc -Vv | grep ^host: | sed -e "s/host: //g") + if [ "$HOST" != "$TARGET" ] + then + rustup component add llvm-tools-preview --toolchain=$TOOLCHAIN + rustup component add rust-std-$TARGET --toolchain=$TOOLCHAIN + fi + if [[ $TARGET == *"musl" ]] + then + # This is needed by libdbus-sys. + sudo apt update -y && sudo apt install musl-dev musl-tools -y + fi + if [[ $TARGET == "aarch64-unknown-linux-musl" ]] + then + echo CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=rust-lld >> $GITHUB_ENV + # This `CC` is some nonsense needed for libdbus-sys (via opener). + # I don't know if this is really the right thing to do, but it seems to work. + sudo apt install gcc-aarch64-linux-gnu -y + echo CC=aarch64-linux-gnu-gcc >> $GITHUB_ENV + fi +fi + +rustup default $TOOLCHAIN +rustup -V +rustc -Vv +cargo -V \ No newline at end of file diff --git a/src/lsmkd.rs b/src/lsmkd.rs index b8c1e2d..342a3e8 100644 --- a/src/lsmkd.rs +++ b/src/lsmkd.rs @@ -82,6 +82,8 @@ struct ScanStatistics { total_tokens: usize, } +type FileDataItem = (PathBuf, Vec, usize, Option); + fn main() { let args = Args::parse(); @@ -109,7 +111,7 @@ fn run(args: Args) -> Result<(), Box> { for path in &paths { let mut markdown_files = Vec::new(); collect_markdown_files( - &path, + path, &mut markdown_files, args.non_recursive, args.all, @@ -120,7 +122,12 @@ fn run(args: Args) -> Result<(), Box> { markdown_files.sort(); for md_file in markdown_files { - let headings = extract_headings(&md_file, args.min_toc_depth, args.max_toc_depth, args.tokens)?; + let headings = extract_headings( + &md_file, + args.min_toc_depth, + args.max_toc_depth, + args.tokens, + )?; let line_count = count_lines(&md_file)?; let size = fs::metadata(&md_file)?.len(); @@ -162,7 +169,14 @@ fn run(args: Args) -> Result<(), Box> { let file_data: Vec<_> = all_files .iter() .filter(|f| f.path.starts_with(&path_str)) - .map(|f| (PathBuf::from(&f.path), f.headings.clone(), f.lines, f.tokens)) + .map(|f| { + ( + PathBuf::from(&f.path), + f.headings.clone(), + f.lines, + f.tokens, + ) + }) .collect(); print_tree(path, &file_data, args.tokens); @@ -224,12 +238,19 @@ fn collect_markdown_files( files.push(entry_path); } else if entry_path.is_dir() && !non_recursive { // Check if we've exceeded the maximum depth before recursing - if let Some(max) = max_depth { - if current_depth >= max { - continue; - } + if let Some(max) = max_depth + && current_depth >= max + { + continue; } - collect_markdown_files(&entry_path, files, non_recursive, all, max_depth, current_depth + 1)?; + collect_markdown_files( + &entry_path, + files, + non_recursive, + all, + max_depth, + current_depth + 1, + )?; } } } @@ -366,7 +387,7 @@ fn extract_headings( Ok(headings) } -fn print_tree(root_path: &Path, file_data: &[(PathBuf, Vec, usize, Option)], show_tokens: bool) { +fn print_tree(root_path: &Path, file_data: &[FileDataItem], show_tokens: bool) { use std::collections::BTreeMap; if file_data.is_empty() { @@ -396,12 +417,28 @@ fn print_tree(root_path: &Path, file_data: &[(PathBuf, Vec, usize, Opti let file_size = get_file_size(path); if show_tokens { if let Some(tokens) = file_tokens { - println!("├── {} {{size: {}, lines: {}, tokens: {}}}", path.display(), file_size, line_count, tokens); + println!( + "├── {} {{size: {}, lines: {}, tokens: {}}}", + path.display(), + file_size, + line_count, + tokens + ); } else { - println!("├── {} {{size: {}, lines: {}}}", path.display(), file_size, line_count); + println!( + "├── {} {{size: {}, lines: {}}}", + path.display(), + file_size, + line_count + ); } } else { - println!("├── {} {{size: {}, lines: {}}}", path.display(), file_size, line_count); + println!( + "├── {} {{size: {}, lines: {}}}", + path.display(), + file_size, + line_count + ); } if headings.is_empty() { @@ -413,16 +450,19 @@ fn print_tree(root_path: &Path, file_data: &[(PathBuf, Vec, usize, Opti } // Group files by their directory relative to base - let mut dir_map: BTreeMap, usize, Option)>> = BTreeMap::new(); + let mut dir_map: BTreeMap> = BTreeMap::new(); for item in file_data { let file_dir = item.0.parent().unwrap_or_else(|| Path::new(".")); let rel_dir = if file_dir == base_dir { PathBuf::from(".") } else { - file_dir.strip_prefix(base_dir).unwrap_or(file_dir).to_path_buf() + file_dir + .strip_prefix(base_dir) + .unwrap_or(file_dir) + .to_path_buf() }; - dir_map.entry(rel_dir).or_insert_with(Vec::new).push(item); + dir_map.entry(rel_dir).or_default().push(item); } // Separate current dir files from subdirectories @@ -435,19 +475,28 @@ fn print_tree(root_path: &Path, file_data: &[(PathBuf, Vec, usize, Opti let mut item_idx = 0; // Print files in current directory first - for (_file_idx, (path, headings, line_count, file_tokens)) in current_files.iter().enumerate() { + for (path, headings, line_count, file_tokens) in current_files.iter() { item_idx += 1; let file_name = path.file_name().unwrap().to_string_lossy(); let file_size = get_file_size(path); if show_tokens { if let Some(tokens) = file_tokens { - println!("├── {} {{size: {}, lines: {}, tokens: {}}}", file_name, file_size, line_count, tokens); + println!( + "├── {} {{size: {}, lines: {}, tokens: {}}}", + file_name, file_size, line_count, tokens + ); } else { - println!("├── {} {{size: {}, lines: {}}}", file_name, file_size, line_count); + println!( + "├── {} {{size: {}, lines: {}}}", + file_name, file_size, line_count + ); } } else { - println!("├── {} {{size: {}, lines: {}}}", file_name, file_size, line_count); + println!( + "├── {} {{size: {}, lines: {}}}", + file_name, file_size, line_count + ); } // Always use │ continuation for files with headings in the root directory @@ -479,12 +528,21 @@ fn print_tree(root_path: &Path, file_data: &[(PathBuf, Vec, usize, Opti if show_tokens { if let Some(tokens) = file_tokens { - println!("{}{} {{size: {}, lines: {}, tokens: {}}}", file_prefix, file_name, file_size, line_count, tokens); + println!( + "{}{} {{size: {}, lines: {}, tokens: {}}}", + file_prefix, file_name, file_size, line_count, tokens + ); } else { - println!("{}{} {{size: {}, lines: {}}}", file_prefix, file_name, file_size, line_count); + println!( + "{}{} {{size: {}, lines: {}}}", + file_prefix, file_name, file_size, line_count + ); } } else { - println!("{}{} {{size: {}, lines: {}}}", file_prefix, file_name, file_size, line_count); + println!( + "{}{} {{size: {}, lines: {}}}", + file_prefix, file_name, file_size, line_count + ); } // Determine heading prefix based on context @@ -539,7 +597,10 @@ fn print_headings(headings: &[Heading], base_prefix: &str, show_tokens: bool) { let level_1_count = headings.iter().filter(|h| h.level == 1).count(); for (h_idx, heading) in headings.iter().enumerate() { - let is_last_at_this_level = !headings.iter().skip(h_idx + 1).any(|h| h.level <= heading.level); + let is_last_at_this_level = !headings + .iter() + .skip(h_idx + 1) + .any(|h| h.level <= heading.level); // Build indentation for heading level let mut heading_prefix = String::from(base_prefix); @@ -552,9 +613,7 @@ fn print_headings(headings: &[Heading], base_prefix: &str, show_tokens: bool) { } // For level-1 headings, use └── if it's the only one or the last one - let tree_char = if heading.level == 1 && level_1_count == 1 { - "└── " - } else if is_last_at_this_level { + let tree_char = if (heading.level == 1 && level_1_count == 1) || is_last_at_this_level { "└── " } else { "├── " @@ -562,15 +621,21 @@ fn print_headings(headings: &[Heading], base_prefix: &str, show_tokens: bool) { if show_tokens { if let Some(tokens) = heading.tokens { - println!("{}{}{} {{line: {}, tokens: {}}}", - heading_prefix, tree_char, heading.text, heading.line_number, tokens); + println!( + "{}{}{} {{line: {}, tokens: {}}}", + heading_prefix, tree_char, heading.text, heading.line_number, tokens + ); } else { - println!("{}{}{} {{line: {}}}", - heading_prefix, tree_char, heading.text, heading.line_number); + println!( + "{}{}{} {{line: {}}}", + heading_prefix, tree_char, heading.text, heading.line_number + ); } } else { - println!("{}{}{} {{line: {}}}", - heading_prefix, tree_char, heading.text, heading.line_number); + println!( + "{}{}{} {{line: {}}}", + heading_prefix, tree_char, heading.text, heading.line_number + ); } } } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 611825a..69a45c5 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -14,7 +14,9 @@ fn test_architecture_doc_sample() { cmd.assert() .success() .stdout(predicate::str::contains("tests/docs/architecture.md")) - .stdout(predicate::str::contains("Zephyr Platform Architecture {line: 1}")) + .stdout(predicate::str::contains( + "Zephyr Platform Architecture {line: 1}", + )) .stdout(predicate::str::contains("Overview {line: 3}")) .stdout(predicate::str::contains("Core Components {line: 7}")) .stdout(predicate::str::contains("Data Flow {line: 30}")) @@ -31,13 +33,19 @@ fn test_prd_doc_sample() { cmd.assert() .success() .stdout(predicate::str::contains("tests/docs/prd.md")) - .stdout(predicate::str::contains("Zephyr Platform - Product Requirements Document {line: 1}")) + .stdout(predicate::str::contains( + "Zephyr Platform - Product Requirements Document {line: 1}", + )) .stdout(predicate::str::contains("Executive Summary {line: 7}")) .stdout(predicate::str::contains("Business Objectives {line: 11}")) .stdout(predicate::str::contains("Target Users {line: 18}")) .stdout(predicate::str::contains("Core Features {line: 24}")) - .stdout(predicate::str::contains("Non-Functional Requirements {line: 66}")) - .stdout(predicate::str::contains("Deployment Constraints {line: 77}")) + .stdout(predicate::str::contains( + "Non-Functional Requirements {line: 66}", + )) + .stdout(predicate::str::contains( + "Deployment Constraints {line: 77}", + )) .stdout(predicate::str::contains("Success Metrics {line: 83}")) .stdout(predicate::str::contains("Timeline {line: 90}")) .stdout(predicate::str::contains("Assumptions {line: 97}")) @@ -241,7 +249,11 @@ fn test_markdown_extensions() { let temp_dir = TempDir::new().unwrap(); fs::write(temp_dir.path().join("test.md"), "# MD Extension\n").unwrap(); - fs::write(temp_dir.path().join("test.markdown"), "# Markdown Extension\n").unwrap(); + fs::write( + temp_dir.path().join("test.markdown"), + "# Markdown Extension\n", + ) + .unwrap(); fs::write(temp_dir.path().join("test.txt"), "# Not Markdown\n").unwrap(); let mut cmd = Command::new(cargo::cargo_bin!("lsmkd")); @@ -437,7 +449,11 @@ fn test_unlimited_depth_default() { #[test] fn test_json_output() { let temp_dir = TempDir::new().unwrap(); - fs::write(temp_dir.path().join("test.md"), "# Heading\n## Subheading\n").unwrap(); + fs::write( + temp_dir.path().join("test.md"), + "# Heading\n## Subheading\n", + ) + .unwrap(); let mut cmd = Command::new(cargo::cargo_bin!("lsmkd")); cmd.arg("-o").arg("json").arg(temp_dir.path()); @@ -459,7 +475,11 @@ fn test_json_output() { #[test] fn test_yaml_output() { let temp_dir = TempDir::new().unwrap(); - fs::write(temp_dir.path().join("test.md"), "# Heading\n## Subheading\n").unwrap(); + fs::write( + temp_dir.path().join("test.md"), + "# Heading\n## Subheading\n", + ) + .unwrap(); let mut cmd = Command::new(cargo::cargo_bin!("lsmkd")); cmd.arg("-o").arg("yaml").arg(temp_dir.path()); @@ -494,7 +514,11 @@ fn test_text_output_explicit() { #[test] fn test_tokens_flag_file() { let temp_dir = TempDir::new().unwrap(); - fs::write(temp_dir.path().join("test.md"), "# Heading\nSome content here.\n").unwrap(); + fs::write( + temp_dir.path().join("test.md"), + "# Heading\nSome content here.\n", + ) + .unwrap(); let mut cmd = Command::new(cargo::cargo_bin!("lsmkd")); cmd.arg("-t").arg(temp_dir.path()); @@ -531,7 +555,11 @@ fn test_tokens_flag_sections() { #[test] fn test_tokens_with_verbose() { let temp_dir = TempDir::new().unwrap(); - fs::write(temp_dir.path().join("test.md"), "# Heading\nSome content.\n").unwrap(); + fs::write( + temp_dir.path().join("test.md"), + "# Heading\nSome content.\n", + ) + .unwrap(); let mut cmd = Command::new(cargo::cargo_bin!("lsmkd")); cmd.arg("-t").arg("-v").arg(temp_dir.path()); @@ -545,7 +573,11 @@ fn test_tokens_with_verbose() { #[test] fn test_tokens_json_output() { let temp_dir = TempDir::new().unwrap(); - fs::write(temp_dir.path().join("test.md"), "# Heading\nContent here.\n").unwrap(); + fs::write( + temp_dir.path().join("test.md"), + "# Heading\nContent here.\n", + ) + .unwrap(); let mut cmd = Command::new(cargo::cargo_bin!("lsmkd")); cmd.arg("-t").arg("-o").arg("json").arg(temp_dir.path());