From 95995be5ff76f28c37799e76a0d9dca156725b10 Mon Sep 17 00:00:00 2001 From: Suyashtnt Date: Wed, 24 Feb 2021 18:14:06 +0200 Subject: [PATCH] initial commit --- .github/actions-rs/grcov.yml | 2 + .github/chglog/CHANGELOG.tpl.md | 49 ++++++ .github/chglog/RELEASE.tpl.md | 29 ++++ .github/chglog/changelog.yml | 23 +++ .github/chglog/release.yml | 22 +++ .github/dependabot.yml | 8 + .github/workflows/audit.yml | 14 ++ .github/workflows/codecov.yml | 38 ++++ .github/workflows/deploy.yml | 127 ++++++++++++++ .github/workflows/rust.yml | 34 ++++ .gitignore | 1 + .vscode/launch.json | 16 ++ Cargo.lock | 295 ++++++++++++++++++++++++++++++++ Cargo.toml | 12 ++ src/cli.rs | 29 ++++ src/generator.rs | 44 +++++ src/main.rs | 34 ++++ 17 files changed, 777 insertions(+) create mode 100644 .github/actions-rs/grcov.yml create mode 100644 .github/chglog/CHANGELOG.tpl.md create mode 100644 .github/chglog/RELEASE.tpl.md create mode 100644 .github/chglog/changelog.yml create mode 100644 .github/chglog/release.yml create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/audit.yml create mode 100644 .github/workflows/codecov.yml create mode 100644 .github/workflows/deploy.yml create mode 100644 .github/workflows/rust.yml create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/cli.rs create mode 100644 src/generator.rs create mode 100644 src/main.rs diff --git a/.github/actions-rs/grcov.yml b/.github/actions-rs/grcov.yml new file mode 100644 index 0000000..3779aa9 --- /dev/null +++ b/.github/actions-rs/grcov.yml @@ -0,0 +1,2 @@ +output-type: lcov +output-path: ./lcov.info diff --git a/.github/chglog/CHANGELOG.tpl.md b/.github/chglog/CHANGELOG.tpl.md new file mode 100644 index 0000000..389e978 --- /dev/null +++ b/.github/chglog/CHANGELOG.tpl.md @@ -0,0 +1,49 @@ +{{ if .Versions -}} + +## [Unreleased] + +{{ if .Unreleased.CommitGroups -}} +{{ range .Unreleased.CommitGroups -}} +### {{ .Title }} +{{ range .Commits -}} +- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} +{{ end -}} +{{ end -}} + +{{ range .Versions }} + +## {{ if .Tag.Previous }}[{{ .Tag.Name }}]{{ else }}{{ .Tag.Name }}{{ end }} - {{ datetime "2006-01-02" .Tag.Date }} +{{ range .CommitGroups -}} +### {{ .Title }} +{{ range .Commits -}} +- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} + +{{- if .RevertCommits -}} +### Reverts +{{ range .RevertCommits -}} +- {{ .Revert.Header }} +{{ end }} +{{ end -}} + +{{- if .NoteGroups -}} +{{ range .NoteGroups -}} +### {{ .Title }} +{{ range .Notes }} +{{ .Body }} +{{ end }} +{{ end -}} +{{ end -}} +{{ end -}} + +{{- if .Versions }} +[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD +{{ range .Versions -}} +{{ if .Tag.Previous -}} +[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }} +{{ end -}} +{{ end -}} +{{ end -}} \ No newline at end of file diff --git a/.github/chglog/RELEASE.tpl.md b/.github/chglog/RELEASE.tpl.md new file mode 100644 index 0000000..40e704c --- /dev/null +++ b/.github/chglog/RELEASE.tpl.md @@ -0,0 +1,29 @@ +{{ range .Versions }} +## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }}) + +{{ range .CommitGroups -}} +### {{ .Title }} + +{{ range .Commits -}} +* {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} + +{{- if .RevertCommits -}} +### Reverts + +{{ range .RevertCommits -}} +* {{ .Revert.Header }} +{{ end }} +{{ end -}} + +{{- if .NoteGroups -}} +{{ range .NoteGroups -}} +### {{ .Title }} + +{{ range .Notes }} +{{ .Body }} +{{ end }} +{{ end -}} +{{ end -}} +{{ end -}} \ No newline at end of file diff --git a/.github/chglog/changelog.yml b/.github/chglog/changelog.yml new file mode 100644 index 0000000..c2ce068 --- /dev/null +++ b/.github/chglog/changelog.yml @@ -0,0 +1,23 @@ +style: github +template: CHANGELOG.tpl.md +info: + title: CHANGELOG + repository_url: https://github.com/suyashtnt/fizzbuzz +options: + commits: + commit_groups: + title_maps: + feat: Features + fix: Bug Fixes + perf: Performance Improvements + refactor: Code Refactor + ci: Continuous Integration + header: + pattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$" + pattern_maps: + - Type + - Scope + - Subject + notes: + keywords: + - BREAKING CHANGE \ No newline at end of file diff --git a/.github/chglog/release.yml b/.github/chglog/release.yml new file mode 100644 index 0000000..9b5e922 --- /dev/null +++ b/.github/chglog/release.yml @@ -0,0 +1,22 @@ +style: github +template: RELEASE.tpl.md +info: + repository_url: https://github.com/Milo123459/glitter +options: + commits: + commit_groups: + title_maps: + feat: Features + fix: Bug Fixes + perf: Performance Improvements + refactor: Code Refactoring + ci: Continuous Integration + header: + pattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$" + pattern_maps: + - Type + - Scope + - Subject + notes: + keywords: + - BREAKING CHANGE \ No newline at end of file diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..57263b9 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: +- package-ecosystem: cargo + directory: "/" + schedule: + interval: daily + time: "10:00" + open-pull-requests-limit: 10 \ No newline at end of file diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml new file mode 100644 index 0000000..e46bb3c --- /dev/null +++ b/.github/workflows/audit.yml @@ -0,0 +1,14 @@ +name: Security audit +on: + push: + paths: + - '**/Cargo.toml' + - '**/Cargo.lock' +jobs: + security_audit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/audit-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml new file mode 100644 index 0000000..a77b8aa --- /dev/null +++ b/.github/workflows/codecov.yml @@ -0,0 +1,38 @@ +name: CodeCov + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + codecov: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - run: sudo apt-get update && sudo apt-get install gcc clang g++ -y + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - uses: actions-rs/toolchain@v1.0.6 + with: + toolchain: nightly + override: true + - uses: actions-rs/cargo@v1 + with: + command: test + args: --all-features --no-fail-fast + env: + CARGO_INCREMENTAL: '0' + RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests' + RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests' + - uses: actions-rs/grcov@v0.1 + - uses: codecov/codecov-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..fd2fbbd --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,127 @@ +name: Deploy +on: + workflow_dispatch: + push: + tags: + - "v*" +# check out starship/starship - inspired by their workflow +jobs: + github_build: + name: Build release binaries + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-unknown-linux-gnu + os: ubuntu-latest + name: glitter-x86_64-unknown-linux-gnu.tar.gz + + - target: x86_64-unknown-linux-musl + os: ubuntu-latest + name: glitter-x86_64-unknown-linux-musl.tar.gz + + - target: i686-unknown-linux-musl + os: ubuntu-latest + name: glitter-i686-unknown-linux-musl.tar.gz + + - target: x86_64-apple-darwin + os: macOS-latest + name: glitter-x86_64-apple-darwin.tar.gz + + - target: aarch64-apple-darwin + os: macOS-latest + name: glitter-aarch64-apple-darwin.tar.gz + + - target: x86_64-pc-windows-msvc + os: windows-latest + name: glitter-x86_64-pc-windows-msvc.zip + + - target: i686-pc-windows-msvc + os: windows-latest + name: glitter-i686-pc-windows-msvc.zip + + - target: aarch64-pc-windows-msvc + os: windows-latest + name: glitter-aarch64-pc-windows-msvc.zip + + runs-on: ${{ matrix.os }} + continue-on-error: true + steps: + - name: Setup | Checkout + uses: actions/checkout@v2 + - name: Setup | Install llvm and build-essential, cmake, cmake-doc, snapd and ninja-build + if: ${{ matrix.os == 'ubuntu-latest' }} + run: sudo apt install llvm build-essential cmake cmake-doc ninja-build snapd + - name: Setup | Instal CC-Tool + if: ${{ matrix.os == 'ubuntu-latest' }} + run: sudo snap install cc-tool --edge --devmode + - name: Setup | Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + profile: minimal + target: ${{ matrix.target }} + - name: Build | Build + uses: actions-rs/cargo@v1 + # TODO: Remove this once it's the default + env: + SDKROOT: /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk + with: + command: build + args: --release --target ${{ matrix.target }} + + - name: Post Build | Prepare artifacts [Windows] + if: matrix.os == 'windows-latest' + run: | + cd target/${{ matrix.target }}/release + strip glitter.exe + 7z a ../../../${{ matrix.name }} glitter.exe + cd - + - name: Post Build | Prepare artifacts [-nix] + if: matrix.os != 'windows-latest' + run: | + cd target/${{ matrix.target }}/release + # TODO: investigate better cross platform stripping + strip glitter || true + tar czvf ../../../${{ matrix.name }} glitter + cd - + - name: Deploy | Upload artifacts + uses: actions/upload-artifact@v2 + with: + name: ${{ matrix.name }} + path: ${{ matrix.name }} + + # Create GitHub release with Rust build targets and release notes + github_release: + name: Create GitHub Release + needs: github_build + runs-on: ubuntu-latest + steps: + - name: Setup | Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Setup | Go + uses: actions/setup-go@v2 + with: + go-version: "^1.15.7" + + - name: Setup | Artifacts + uses: actions/download-artifact@v2 + + - name: Setup | Checksums + run: for file in glitter-*/glitter-*; do openssl dgst -sha256 -r "$file" | awk '{print $1}' > "${file}.sha256"; done + + - name: Setup | Release notes + run: | + GO111MODULE=on go get github.com/git-chglog/git-chglog/cmd/git-chglog@0.9.1 + git-chglog -c .github/chglog/release.yml $(git describe --tags) > RELEASE.md + - name: Build | Publish + uses: softprops/action-gh-release@v1 + with: + files: glitter-*/glitter-* + body_path: RELEASE.md + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..4810071 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,34 @@ +name: Rust + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] +env: + CARGO_TERM_COLOR: always +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/cache@v2 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - run: sudo apt-get update && sudo apt-get install gcc clang g++ -y + - uses: actions-rs/toolchain@v1.0.6 + with: + toolchain: nightly + override: true + - uses: actions-rs/cargo@v1 + with: + command: build + args: --verbose + - uses: actions-rs/cargo@v1 + with: + command: test + args: --verbose diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..224af64 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c96fce9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,295 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "console" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cc80946b3480f421c2f17ed1cb841753a371c7c5104f51d507e13f532c856aa" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "regex", + "terminal_size", + "unicode-width", + "winapi", +] + +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + +[[package]] +name = "fizzbuzz" +version = "0.1.0" +dependencies = [ + "indicatif", + "structopt", + "strum", +] + +[[package]] +name = "heck" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +dependencies = [ + "libc", +] + +[[package]] +name = "indicatif" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7baab56125e25686df467fe470785512329883aab42696d661247aca2a2896e4" +dependencies = [ + "console", + "lazy_static", + "number_prefix", + "regex", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" + +[[package]] +name = "number_prefix" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" +dependencies = [ + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "structopt" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5277acd7ee46e63e5168a80734c9f6ee81b1367a7d8772a2d765df2a3705d28c" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ba9cdfda491b814720b6b06e0cac513d922fc407582032e8706e9f137976f90" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strum" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c" + +[[package]] +name = "syn" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "terminal_size" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86ca8ced750734db02076f44132d802af0b33b09942331f4459dde8636fd2406" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + +[[package]] +name = "unicode-segmentation" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..776abd5 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "fizzbuzz" +version = "0.1.0" +authors = ["Suyashtnt "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +indicatif = "0.15.0" +structopt = "0.3.21" +strum = "0.20.0" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..9af7138 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,29 @@ +use std::error::Error; +use structopt::StructOpt; +#[derive(Debug, StructOpt)] +pub struct Cli { + /// minimum value as an integar + pub min: i128, + /// minimum value as an integar + pub max: i128, + #[structopt(short, long, parse(try_from_str = parse_key_val))] + /// create custom matchers + /// + /// syntax: replace=number + /// + /// example: fizzbuzz 0 10 -d fizz=3 buzz=5 bazz=7 + pub defines: Option>, +} + +fn parse_key_val(s: &str) -> Result<(T, U), Box> +where + T: std::str::FromStr, + T::Err: Error + 'static, + U: std::str::FromStr, + U::Err: Error + 'static, +{ + let pos = s + .find('=') + .ok_or_else(|| format!("invalid KEY=value: no `=` found in `{}`", s))?; + Ok((s[..pos].parse()?, s[pos + 1..].parse()?)) +} diff --git a/src/generator.rs b/src/generator.rs new file mode 100644 index 0000000..6c99a4e --- /dev/null +++ b/src/generator.rs @@ -0,0 +1,44 @@ +use std::{convert::TryInto, i128}; + +pub struct Generator { + pub current: i128, + pub max: i128, + pub match_against: Vec<(String, i128)>, +} + +impl Iterator for Generator { + type Item = String; + + fn next(&mut self) -> Option { + if self.max != 0 && self.current == self.max { + return None; + }; + + self.current += 1; + + let mut output = String::new(); + + for (value, num_to_match) in &self.match_against { + if self.current % num_to_match == 0 { + output += value; + } + } + + if output == "" { + output = self.current.to_string(); + } + + Some(output) + } + + fn count(self) -> usize { + return self.max.try_into().unwrap(); + } + + fn size_hint(&self) -> (usize, Option) { + ( + self.max.try_into().unwrap(), + Some(self.max.try_into().unwrap()), + ) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a1a12d8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,34 @@ +mod cli; +mod generator; + +use std::convert::TryInto; + +use cli::Cli; +use generator::Generator; +use indicatif::{ProgressBar, ProgressIterator, ProgressStyle}; +use structopt::StructOpt; + +fn main() { + let cli: Cli = Cli::from_args(); + + let gen = Generator { + match_against: cli + .defines + .unwrap_or(vec![("Buzz".to_string(), 5), ("Fizz".to_string(), 3)]), + max: cli.max, + current: cli.min, + }; + + let mut value = String::new(); + let bar = ProgressBar::new(cli.max.try_into().unwrap()).with_style( + ProgressStyle::default_bar() + .template("[{elapsed_precise}] {bar:50.cyan/blue} {pos:>7}/{len:7} {msg}") + .progress_chars("##-"), + ); + + for val in gen.progress_with(bar.clone()) { + bar.set_message(&*val); + value += &format!("{}\n", val); + } + println!("{}", value); +}