diff --git a/.github/actions/cached-toolchain/action.yml b/.github/actions/cached-toolchain/action.yml new file mode 100644 index 0000000..5ee6944 --- /dev/null +++ b/.github/actions/cached-toolchain/action.yml @@ -0,0 +1,49 @@ +name: cached-toolchain +description: Install and cache toolchain + +inputs: + toolchain: + description: Rust toolchain specification -- see https://rust-lang.github.io/rustup/concepts/toolchains.html#toolchain-specification + required: true + targets: + description: Comma-separated list of target triples to install for this toolchain + required: false + +outputs: + cachekey: + description: A short hash of the rustc version, appropriate for use as a cache key. "20220627a831" + value: ${{steps.toolchain.outputs.cachekey}} + +runs: + using: composite + steps: + - uses: actions/cache/restore@v3 + with: + path: | + ~/.cargo/bin/rustup + ~/.rustup + key: ${{ runner.os }}-toolchain-${{ inputs.toolchain }}- + restore-keys: | + ${{ runner.os }}-toolchain-${{ inputs.toolchain }}- + + - shell: bash + run: + mkdir -p "${CARGO_HOME:-$HOME/.cargo}/bin" + echo "${CARGO_HOME:-$HOME/.cargo}/bin" >> $GITHUB_PATH + if: runner.os != 'Windows' + + - uses: dtolnay/rust-toolchain@e12eda571dc9a5ee5d58eecf4738ec291c66f295 # latest for 2023.03.10 + id: toolchain + with: + toolchain: ${{ inputs.toolchain }} + # enable clippy only only on stable + components: ${{ inputs.toolchain == 'stable' && 'clippy' || '' }} + # additional targets + targets: wasm32-unknown-unknown${{ inputs.targets && ',' || '' }}${{ inputs.targets && ',' || '' }} + + - uses: actions/cache/save@v3 + with: + path: | + ~/.cargo/bin/rustup + ~/.rustup + key: ${{ runner.os }}-toolchain-${{ inputs.toolchain }}-${{ steps.toolchain.outputs.cachekey }} \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 790dab4..0f6b5b1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,31 +21,84 @@ jobs: steps: - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - uses: ./.github/actions/cached-toolchain + id: toolchain with: - toolchain: stable - - - name: Run cargo test - uses: actions-rs/cargo@v1 + toolchain: 'stable' + + - uses: actions/cache@v3 with: - command: test + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target + key: test-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-toolchain-${{ steps.toolchain.outputs.cachekey }} + restore-keys: | + test-${{ runner.os }}-cargo- + + - run: cargo test --all-features build: runs-on: ubuntu-latest strategy: + fail-fast: false matrix: rust: [stable, beta, nightly] steps: - uses: actions/checkout@v1 + + - uses: ./.github/actions/cached-toolchain + id: toolchain + with: + toolchain: ${{matrix.rust}} - - name: Set Rust version - env: - RUST_VERSION: ${{ matrix.rust }} - run: rustup default ${RUST_VERSION} + - uses: actions/cache/restore@v3 # fetched cargo cache + id: cache-cargo-install-restore + with: + path: | + ~/.cargo + key: build-tools-${{matrix.rust}}-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-toolchain-${{ steps.toolchain.outputs.cachekey }} + restore-keys: | + build-tools-${{matrix.rust}}-${{ runner.os }}-cargo- - - if: matrix.rust == 'stable' - run: rustup component add clippy + - name: Install wasm-pack + run: + cargo install --git https://github.com/mre/wasm-pack.git --branch first-class-bins + + - name: Print wasm-pack version + run: wasm-pack --version + + - name: Install trunk + run: + cargo install --locked trunk@0.17.2 # latest on 2023.07.24 + + - name: Print trunk version + run: trunk --version + + - uses: actions/cache/save@v3 # saves cargo cache + with: + path: | + ~/.cargo + key: ${{ steps.cache-cargo-install-restore.outputs.cache-primary-key }} + + - uses: actions/cache@v3 # fetched cargo cache + id: cache-cargo-restore + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target + key: build-${{matrix.rust}}-${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-toolchain-${{ steps.toolchain.outputs.cachekey }} + restore-keys: | + build-${{matrix.rust}}-${{ runner.os }}-cargo- + + - name: cargo fetch + run: cargo fetch --locked - if: matrix.rust == 'stable' uses: actions-rs/clippy-check@v1 @@ -53,18 +106,20 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} args: --all-features - - name: Install wasm-pack - run: - cargo install --force --git https://github.com/mre/wasm-pack.git --branch first-class-bins - - - name: Print wasm-pack version - run: wasm-pack --version - - name: Build and install tinysearch - run: cargo install --force --path bin + run: cargo install --force --path tinysearch --features=bin + continue-on-error: ${{ matrix.rust == 'nightly' }} - name: Build WASM package from test index - run: RUST_LOG=debug tinysearch fixtures/index.json + run: RUST_LOG=debug tinysearch -e 'path= "'$PWD'/tinysearch"' fixtures/index.json + continue-on-error: ${{ matrix.rust == 'nightly' }} + + - name: Build examples + run: + for example in `ls -1 examples`; do + (cd "examples/$example" && trunk build index.html) || exit 1; + done + continue-on-error: ${{ matrix.rust == 'nightly' }} - name: Audit for Security Vulnerabilities uses: actions-rs/audit-check@v1 @@ -77,33 +132,6 @@ jobs: command: doc args: --all-features --no-deps - publish-check: - if: startsWith(github.ref, 'refs/tags/') != true - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - - - name: cargo fetch - uses: actions-rs/cargo@v1 - with: - command: fetch - - # Note that we don't check `bin` here as it depends on `shared` - # to be published (which we don't do during dry-run). - # This could be fixed by allowing publish relative paths. - # https://github.com/rust-lang/cargo/issues/6738 - # https://github.com/rust-lang/cargo/issues/1565 - - name: Publish check for shared - uses: actions-rs/cargo@v1 - with: - command: publish - args: --dry-run --manifest-path shared/Cargo.toml - publish: if: startsWith(github.ref, 'refs/tags/') needs: @@ -117,24 +145,7 @@ jobs: with: command: fetch - - name: Publish shared - uses: actions-rs/cargo@v1 - env: - CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} - with: - command: publish - args: --manifest-path shared/Cargo.toml - - # The crates.io API might be delayed by a few seconds. - # As a result, the newly published `shared` might not be - # published when the binary that depends on it wants to pull it. - # Wait a few seconds to avoid that race-condition. - - name: Wait for cargo publication - uses: juliangruber/sleep-action@v1 - with: - time: 30s - - - name: Publish binary + - name: Publish uses: actions-rs/cargo@v1 env: CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9f85ced..6665e2c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,8 +23,8 @@ jobs: id: get_release uses: bruceadams/get-release@v1.2.2 env: - GITHUB_TOKEN: ${{ github.token }} - + GITHUB_TOKEN: ${{ github.token }} + linux: runs-on: ubuntu-latest needs: prepare @@ -58,12 +58,10 @@ jobs: sudo apt-get install -y binutils-aarch64-linux-gnu - uses: actions/checkout@v2 - - - uses: actions-rs/toolchain@v1 + - uses: ./.github/actions/cached-toolchain with: - profile: minimal toolchain: stable - default: true + targets: ${{ matrix.target }} - name: Build ${{ matrix.target }} uses: actions-rs/cargo@v1 diff --git a/.gitignore b/.gitignore index 76c9e2d..27a0c23 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,5 @@ tinysearch_engine_bg.wasm.d.ts package.json pkg/ wasm_output/ -demo.html .vscode +tinysearch-engine diff --git a/Cargo.lock b/Cargo.lock index 0e8e654..1ca5e0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,23 +2,17 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - [[package]] name = "anyhow" -version = "1.0.43" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28ae2b3dec75a406790005a200b1bd89785afc02517a00ca99ecfe093ee9e6cf" +checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854" [[package]] name = "argh" -version = "0.1.5" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e7317a549bc17c5278d9e72bb6e62c6aa801ac2567048e39ebc1c194249323e" +checksum = "ab257697eb9496bf75526f0217b5ed64636a9cfafa78b8365c71bd283fcef93e" dependencies = [ "argh_derive", "argh_shared", @@ -26,28 +20,21 @@ dependencies = [ [[package]] name = "argh_derive" -version = "0.1.5" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60949c42375351e9442e354434b0cba2ac402c1237edf673cac3a4bf983b8d3c" +checksum = "b382dbd3288e053331f03399e1db106c9fb0d8562ad62cb04859ae926f324fa6" dependencies = [ "argh_shared", - "heck", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] name = "argh_shared" -version = "0.1.5" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a61eb019cb8f415d162cb9f12130ee6bbe9168b7d953c17f4ad049e4051ca00" - -[[package]] -name = "autocfg" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "64cb94155d965e3d37ffbbe7cc5b82c3dd79dd33bd48e536f73d2cfb8d85506f" [[package]] name = "bincode" @@ -64,6 +51,18 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630be753d4e58660abd17930c71b647fe46c27ea6b63cc59e1e3851406972e42" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + [[package]] name = "cfg-if" version = "1.0.0" @@ -71,175 +70,124 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "crc32fast" -version = "1.2.1" +name = "equivalent" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" -dependencies = [ - "cfg-if", -] +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] -name = "flate2" -version = "1.0.20" +name = "errno" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd3aec53de10fe96d7d8c565eb17f2c687bb5518a2ec453b5b1252964526abe0" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" dependencies = [ - "cfg-if", - "crc32fast", + "errno-dragonfly", "libc", - "miniz_oxide", + "windows-sys", ] [[package]] -name = "getopts" -version = "0.2.21" +name = "errno-dragonfly" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" dependencies = [ - "unicode-width", + "cc", + "libc", ] [[package]] -name = "getrandom" -version = "0.1.16" +name = "fastrand" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" -dependencies = [ - "cfg-if", - "libc", - "wasi 0.9.0+wasi-snapshot-preview1", -] +checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" [[package]] -name = "getrandom" -version = "0.2.3" +name = "getopts" +version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" dependencies = [ - "cfg-if", - "libc", - "wasi 0.10.2+wasi-snapshot-preview1", + "unicode-width", ] [[package]] -name = "heck" -version = "0.3.3" +name = "hashbrown" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] +checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" [[package]] -name = "includedir" -version = "0.6.0" +name = "heck" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afd126bd778c00c43a9dc76d1609a0894bf4222088088b2217ccc0ce9e816db7" -dependencies = [ - "flate2", - "phf", -] +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] -name = "includedir_codegen" -version = "0.6.0" +name = "indexmap" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ac1500c9780957c9808c4ec3b94002f35aab01483833f5a8bce7dfb243e3148" +checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" dependencies = [ - "flate2", - "phf_codegen", - "walkdir", + "equivalent", + "hashbrown", ] [[package]] name = "itoa" -version = "0.4.7" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "libc" -version = "0.2.99" +version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] -name = "log" -version = "0.4.14" +name = "linux-raw-sys" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" -dependencies = [ - "cfg-if", -] +checksum = "09fc20d2ca12cb9f044c93e3bd6d32d523e6e2ec3db4f7b2939cd99026ecd3f0" [[package]] -name = "memchr" -version = "2.4.0" +name = "log" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] -name = "miniz_oxide" -version = "0.4.4" +name = "memchr" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" -dependencies = [ - "adler", - "autocfg", -] +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "phf" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12" -dependencies = [ - "phf_shared", -] - -[[package]] -name = "phf_codegen" -version = "0.8.0" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbffee61585b0411840d3ece935cce9cb6321f01c45477d30066498cd5e1a815" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" dependencies = [ - "phf_generator", "phf_shared", ] -[[package]] -name = "phf_generator" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526" -dependencies = [ - "phf_shared", - "rand 0.7.3", -] - [[package]] name = "phf_shared" -version = "0.8.0" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" dependencies = [ "siphasher", ] -[[package]] -name = "ppv-lite86" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" - [[package]] name = "proc-macro2" -version = "1.0.28" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] @@ -248,7 +196,7 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca36dea94d187597e104a5c8e4b07576a8a45aa5db48a65e12940d3eb7461f55" dependencies = [ - "bitflags", + "bitflags 1.3.2", "getopts", "memchr", "unicase", @@ -256,207 +204,149 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.9" +version = "1.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" dependencies = [ "proc-macro2", ] [[package]] -name = "rand" -version = "0.7.3" +name = "redox_syscall" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" dependencies = [ - "getrandom 0.1.16", - "libc", - "rand_chacha 0.2.2", - "rand_core 0.5.1", - "rand_hc 0.2.0", - "rand_pcg", + "bitflags 1.3.2", ] [[package]] -name = "rand" -version = "0.8.4" +name = "rustix" +version = "0.38.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +checksum = "0a962918ea88d644592894bc6dc55acc6c0956488adcebbfb6e273506b7fd6e5" dependencies = [ + "bitflags 2.3.3", + "errno", "libc", - "rand_chacha 0.3.1", - "rand_core 0.6.3", - "rand_hc 0.3.1", + "linux-raw-sys", + "windows-sys", ] [[package]] -name = "rand_chacha" -version = "0.2.2" +name = "rustversion" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" -dependencies = [ - "ppv-lite86", - "rand_core 0.5.1", -] +checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" [[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core 0.6.3", -] - -[[package]] -name = "rand_core" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" -dependencies = [ - "getrandom 0.1.16", -] - -[[package]] -name = "rand_core" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" -dependencies = [ - "getrandom 0.2.3", -] - -[[package]] -name = "rand_hc" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" -dependencies = [ - "rand_core 0.5.1", -] - -[[package]] -name = "rand_hc" -version = "0.3.1" +name = "ryu" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" -dependencies = [ - "rand_core 0.6.3", -] +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] -name = "rand_pcg" -version = "0.2.1" +name = "serde" +version = "1.0.175" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" +checksum = "5d25439cd7397d044e2748a6fe2432b5e85db703d6d097bd014b3c0ad1ebff0b" dependencies = [ - "rand_core 0.5.1", + "serde_derive", ] [[package]] -name = "redox_syscall" -version = "0.2.10" +name = "serde_derive" +version = "1.0.175" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +checksum = "b23f7ade6f110613c0d63858ddb8b94c1041f550eab58a16b371bdf2c9c80ab4" dependencies = [ - "bitflags", + "proc-macro2", + "quote", + "syn 2.0.27", ] [[package]] -name = "remove_dir_all" -version = "0.5.3" +name = "serde_json" +version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +checksum = "d03b412469450d4404fe8499a268edd7f8b79fecb074b0d812ad64ca21f4031b" dependencies = [ - "winapi", + "itoa", + "ryu", + "serde", ] [[package]] -name = "ryu" -version = "1.0.5" +name = "siphasher" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" [[package]] -name = "same-file" -version = "1.0.6" +name = "strip_markdown" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +checksum = "32c754386109f9adc8ca62513c51cf81cc2d8502588064103aa8e9f69b4276da" dependencies = [ - "winapi-util", + "log", + "pulldown-cmark", ] [[package]] -name = "serde" -version = "1.0.127" +name = "strum" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f03b9878abf6d14e6779d3f24f07b2cfa90352cfec4acc5aab8f1ac7f146fae8" +checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" dependencies = [ - "serde_derive", + "strum_macros", ] [[package]] -name = "serde_derive" -version = "1.0.127" +name = "strum_macros" +version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a024926d3432516606328597e0f224a51355a493b49fdd67e9209187cbe55ecc" +checksum = "6069ca09d878a33f883cc06aaa9718ede171841d3832450354410b718b097232" dependencies = [ + "heck", "proc-macro2", "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "336b10da19a12ad094b59d870ebde26a45402e5b470add4b5fd03c5048a32127" -dependencies = [ - "itoa", - "ryu", - "serde", + "rustversion", + "syn 2.0.27", ] [[package]] -name = "siphasher" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "729a25c17d72b06c68cb47955d44fda88ad2d3e7d77e025663fdd69b93dd71a1" - -[[package]] -name = "strip_markdown" -version = "0.2.0" +name = "syn" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32c754386109f9adc8ca62513c51cf81cc2d8502588064103aa8e9f69b4276da" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "log", - "pulldown-cmark", + "proc-macro2", + "quote", + "unicode-ident", ] [[package]] name = "syn" -version = "1.0.74" +version = "2.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" +checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] name = "tempfile" -version = "3.2.0" +version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +checksum = "5486094ee78b2e5038a6382ed7645bc084dc2ec433426ca4c3cb61e2007b8998" dependencies = [ "cfg-if", - "libc", - "rand 0.8.4", + "fastrand", "redox_syscall", - "remove_dir_all", - "winapi", + "rustix", + "windows-sys", ] [[package]] @@ -465,26 +355,34 @@ version = "0.7.0" dependencies = [ "anyhow", "argh", - "includedir", - "includedir_codegen", + "bincode", "log", "phf", "serde", "serde_derive", "serde_json", "strip_markdown", + "strum", "tempfile", - "tinysearch-shared", + "toml_edit", "xorf", ] [[package]] -name = "tinysearch-shared" -version = "0.7.0" +name = "toml_datetime" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" + +[[package]] +name = "toml_edit" +version = "0.19.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a" dependencies = [ - "bincode", - "serde", - "xorf", + "indexmap", + "toml_datetime", + "winnow", ] [[package]] @@ -497,89 +395,103 @@ dependencies = [ ] [[package]] -name = "unicode-segmentation" -version = "1.8.0" +name = "unicode-ident" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" [[package]] name = "unicode-width" -version = "0.1.8" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" [[package]] -name = "unicode-xid" -version = "0.2.2" +name = "version_check" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] -name = "version_check" -version = "0.9.3" +name = "windows-sys" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] [[package]] -name = "walkdir" -version = "2.3.2" +name = "windows-targets" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ - "same-file", - "winapi", - "winapi-util", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] -name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" +name = "windows_aarch64_gnullvm" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" [[package]] -name = "wasi" -version = "0.10.2+wasi-snapshot-preview1" +name = "windows_aarch64_msvc" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" [[package]] -name = "winapi" -version = "0.3.9" +name = "windows_i686_gnu" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "windows_i686_msvc" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" [[package]] -name = "winapi-util" -version = "0.1.5" +name = "windows_x86_64_gnu" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows_x86_64_gnullvm" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + +[[package]] +name = "winnow" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fac9742fd1ad1bd9643b991319f72dd031016d44b77039a26977eb667141e7" +dependencies = [ + "memchr", +] [[package]] name = "xorf" -version = "0.7.2" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0b223640dfbc22009679ce79f6777ae1db79add62a07345605777f69274a052" +checksum = "57901b00e3f8e14f4d20b8955bf8087ecb545cfe2ed8741c2a2dbc89847a1a29" dependencies = [ - "rand 0.8.4", "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 54d2cf3..e9e9a5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,4 @@ [workspace] members = [ - "bin", - "shared", + "tinysearch", ] \ No newline at end of file diff --git a/Makefile b/Makefile index 09fcb1f..585965e 100644 --- a/Makefile +++ b/Makefile @@ -19,11 +19,11 @@ build: ### Compile project .PHONY: install install: ## Install tinysearch - cargo install --force --path bin + cargo install --force --path tinysearch .PHONY: test test: ## Run unit tests - cargo test + cargo test --features=bin .PHONY: run run: ## Run tinysearch with sample input @@ -31,7 +31,7 @@ run: ## Run tinysearch with sample input .PHONY: pack pack: ## Pack tinysearch node module - wasm-pack build bin + wasm-pack build tinysearch wasm-pack pack .PHONY: publish diff --git a/bin/Cargo.toml b/bin/Cargo.toml deleted file mode 100644 index 672e709..0000000 --- a/bin/Cargo.toml +++ /dev/null @@ -1,28 +0,0 @@ -[package] -name = "tinysearch" -authors = ["Matthias Endler "] -version = "0.7.0" -edition = "2021" -description = "A tiny search engine for static websites" -license = "Apache-2.0/MIT" -documentation = "https://github.com/mre/tinysearch/blob/master/README.md" -homepage = "https://github.com/mre/tinysearch" -repository = "https://github.com/mre/tinysearch" - -[dependencies] -tinysearch-shared = { path = "../shared", version = "0.7.0" } - -argh = "0.1.5" -log = "0.4.14" -serde_json = "1.0.66" -anyhow = "1.0.43" -tempfile = "3.2.0" -serde = { version = "1.0.127", features = ["derive"] } -serde_derive = "1.0.127" -phf = "0.8.0" -includedir = "0.6.0" -strip_markdown = "0.2.0" -xorf = "0.7.2" - -[build-dependencies] -includedir_codegen = "0.6.0" diff --git a/bin/build.rs b/bin/build.rs deleted file mode 100644 index c0e3378..0000000 --- a/bin/build.rs +++ /dev/null @@ -1,11 +0,0 @@ -extern crate includedir_codegen; - -use includedir_codegen::Compression; - -fn main() { - includedir_codegen::start("FILES") - .dir("../engine", Compression::Gzip) - .dir("../shared", Compression::Gzip) - .build("engine.rs") - .unwrap(); -} diff --git a/bin/src/main.rs b/bin/src/main.rs deleted file mode 100644 index e7a196e..0000000 --- a/bin/src/main.rs +++ /dev/null @@ -1,160 +0,0 @@ -#[macro_use] -extern crate log; - -mod index; -mod storage; - -use anyhow::{bail, Context, Error, Result}; -use argh::FromArgs; -use std::io::Write; -use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; -use std::{env, fs}; -use tempfile::tempdir; - -use fs::File; -use index::Posts; - -// The search engine code gets statically included into the binary. -// During indexation (when running tinysearch), this will be compiled to WASM. -include!(concat!(env!("OUT_DIR"), "/engine.rs")); - -// Include a bare-bones HTML page that demonstrates how tinysearch is used -static DEMO_HTML: &str = include_str!("../assets/demo.html"); - -fn default_output_dir() -> PathBuf { - fs::create_dir_all("./wasm_output").unwrap(); - PathBuf::from("./wasm_output") -} - -#[derive(FromArgs)] -/// A tiny, static search engine for static websites -struct Opt { - /// show version and exit - #[argh(switch)] - version: bool, - - /// index JSON file to process - #[argh(positional)] - index: Option, - - /// output path for WASM module ("wasm_output" directory by default) - #[argh(option, short = 'p', long = "path", default = "default_output_dir()")] - out_path: PathBuf, - - /// optimize the output using binaryen - #[argh(switch, short = 'o', long = "optimize")] - optimize: bool, -} - -fn unpack_engine(temp_dir: &Path) -> Result<(), Error> { - println!("Starting unpack"); - for file in FILES.file_names() { - println!("Copying {:?}", file); - // This hack removes the "../" prefix that - // gets introduced by including the crates - // from the `bin` parent directory. - let filepath = file.trim_start_matches("../"); - let outpath = temp_dir.join(filepath); - if let Some(parent) = outpath.parent() { - debug!("Creating parent dir {:?}", &parent); - fs::create_dir_all(&parent)?; - } - let content = FILES.get(file)?; - let mut outfile = File::create(&outpath)?; - outfile.write_all(&content)?; - } - Ok(()) -} - -fn main() -> Result<(), Error> { - FILES.set_passthrough(env::var_os("PASSTHROUGH").is_some()); - - let opt: Opt = argh::from_env(); - - if opt.version { - println!("tinysearch {}", env!("CARGO_PKG_VERSION")); - std::process::exit(0); - } - - let out_path = opt.out_path.canonicalize()?; - - let index = opt.index.context("No index file specified")?; - let posts: Posts = index::read(fs::read_to_string(index)?)?; - trace!("Generating storage from posts: {:#?}", posts); - storage::write(posts)?; - - let temp_dir = tempdir()?; - println!( - "Unpacking tinysearch WASM engine into temporary directory {:?}", - temp_dir.path() - ); - unpack_engine(temp_dir.path())?; - debug!("Crate content extracted to {:?}/", &temp_dir); - - let engine_dir = temp_dir.path().join("engine"); - if !engine_dir.exists() { - fs::create_dir_all(&engine_dir)?; - } - if !engine_dir.exists() { - for path in fs::read_dir(out_path)? { - println!("Name: {}", path.unwrap().path().display()) - } - bail!( - "Engine directory could not be created at {}", - engine_dir.display() - ); - } - - println!("Copying index into crate"); - fs::copy("storage", engine_dir.join("storage"))?; - - println!("Compiling WASM module using wasm-pack"); - wasm_pack(&temp_dir.path().join("engine"), &out_path)?; - - if opt.optimize { - optimize(&out_path)?; - } - - fs::write(&out_path.join("demo.html"), DEMO_HTML)?; - - println!("All done! Open the output folder with a web server to try the demo."); - Ok(()) -} - -fn wasm_pack(in_dir: &Path, out_dir: &Path) -> Result { - run_output( - Command::new("wasm-pack") - .arg("build") - .arg(in_dir) - .arg("--target") - .arg("web") - .arg("--release") - .arg("--out-dir") - .arg(out_dir), - ) -} - -fn optimize(dir: &Path) -> Result { - run_output( - Command::new("wasm-opt") - .current_dir(dir) - .arg("-Oz") - .arg("-o") - .arg("tinysearch_engine_bg.wasm") - .arg("tinysearch_engine_bg.wasm"), - ) -} - -pub fn run_output(cmd: &mut Command) -> Result { - log::debug!("running {:?}", cmd); - let output = cmd - .stderr(Stdio::inherit()) - .output() - .with_context(|| format!("failed to run {:?}", cmd))?; - - if !output.status.success() { - anyhow::bail!("failed to execute {:?}\nstatus: {}", cmd, output.status) - } - Ok(String::from_utf8_lossy(&output.stdout).into_owned()) -} diff --git a/bin/assets/demo.html b/demo.html similarity index 100% rename from bin/assets/demo.html rename to demo.html diff --git a/engine/.gitignore b/engine/.gitignore deleted file mode 100644 index 2f88dba..0000000 --- a/engine/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/target -**/*.rs.bk -Cargo.lock \ No newline at end of file diff --git a/engine/Cargo.lock b/engine/Cargo.lock new file mode 100644 index 0000000..a2f3dd3 --- /dev/null +++ b/engine/Cargo.lock @@ -0,0 +1,260 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] +name = "bumpalo" +version = "3.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f1e260c3a9040a7c19a12468758f4c16f31a81a1fe087482be9570ec864bb6c" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + +[[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.112" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "memory_units" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" + +[[package]] +name = "once_cell" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" + +[[package]] +name = "proc-macro2" +version = "1.0.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f84e92c0f7c9d58328b85a78557813e4bd845130db68d7184635344399423b1" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "serde" +version = "1.0.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9875c23cf305cd1fd7eb77234cbb705f21ea6a72c637a5c6db5fe4b8e7f008" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc0db5cb2556c0e558887d9bbdcf6ac4471e83ff66cf696e5419024d1606276" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcbd0344bc6533bc7ec56df11d42fb70f1b912351c0825ccb7211b59d8af7cf5" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecb2e6da8ee5eb9a61068762a32fa9619cc591ceb055b3687f4cd4051ec2e06b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "tinysearch-engine" +version = "0.6.4" +dependencies = [ + "once_cell", + "tinysearch-shared", + "wasm-bindgen", + "wee_alloc", + "xorf", +] + +[[package]] +name = "tinysearch-shared" +version = "0.6.4" +dependencies = [ + "bincode", + "serde", + "xorf", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "wasm-bindgen" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "632f73e236b219150ea279196e54e610f5dbafa5d61786303d4da54f84e47fce" +dependencies = [ + "cfg-if 1.0.0", + "serde", + "serde_json", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a317bf8f9fba2476b4b2c85ef4c4af8ff39c3c7f0cdfeed4f82c34a880aa837b" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56146e7c495528bf6587663bea13a8eb588d39b36b679d83972e1a2dbbdacf9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7803e0eea25835f8abdc585cd3021b3deb11543c6fe226dcd30b228857c5c5ab" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0237232789cf037d5480773fe568aac745bfe2afbc11a863e97901780a6b47cc" + +[[package]] +name = "wee_alloc" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "memory_units", + "winapi", +] + +[[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" + +[[package]] +name = "xorf" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7afb3a52aef0211e557044386369919b033004fdcf4c56c5017710f97fa9aa3c" +dependencies = [ + "serde", +] diff --git a/engine/src/lib.rs b/engine/src/lib.rs deleted file mode 100644 index a0905eb..0000000 --- a/engine/src/lib.rs +++ /dev/null @@ -1,55 +0,0 @@ -use once_cell::sync::Lazy; -use wasm_bindgen::prelude::*; -use xorf::{HashProxy, Xor8}; - -use std::cmp::Reverse; -use std::collections::hash_map::DefaultHasher; - -use tinysearch_shared::{Filters, PostId, Score, Storage}; -pub type Filter = HashProxy; - -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - -const TITLE_WEIGHT: usize = 3; - -static FILTERS: Lazy = Lazy::new(|| { - let bytes = include_bytes!("../storage"); - Storage::from_bytes(bytes).unwrap().filters -}); - -// Wrapper around filter score, that also scores the post title -// Post title score has a higher weight than post body -fn score(title: &String, search_terms: &Vec, filter: &Filter) -> usize { - let title_terms: Vec = tokenize(&title); - let title_score: usize = search_terms - .iter() - .filter(|term| title_terms.contains(&term)) - .count(); - TITLE_WEIGHT * title_score + filter.score(search_terms) -} - -fn tokenize(s: &str) -> Vec { - s.to_lowercase() - .split_whitespace() - .filter(|&t| !t.trim().is_empty()) - .map(String::from) - .collect() -} - -#[wasm_bindgen] -pub fn search(query: String, num_results: usize) -> JsValue { - let search_terms: Vec = tokenize(&query); - - let mut matches: Vec<(&PostId, usize)> = FILTERS - .iter() - .map(|(post_id, filter)| (post_id, score(&post_id.0, &search_terms, &filter))) - .filter(|(_post_id, score)| *score > 0) - .collect(); - - matches.sort_by_key(|k| Reverse(k.1)); - - let results: Vec<&PostId> = matches.into_iter().take(num_results).map(|p| p.0).collect(); - - JsValue::from_serde(&results).unwrap() -} diff --git a/examples/yew-example-crate/.gitignore b/examples/yew-example-crate/.gitignore new file mode 100644 index 0000000..936c7e2 --- /dev/null +++ b/examples/yew-example-crate/.gitignore @@ -0,0 +1,3 @@ +example-search +target +dist diff --git a/examples/yew-example-crate/Cargo.lock b/examples/yew-example-crate/Cargo.lock new file mode 100644 index 0000000..f14f703 --- /dev/null +++ b/examples/yew-example-crate/Cargo.lock @@ -0,0 +1,672 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" + +[[package]] +name = "anymap" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33954243bd79057c2de7338850b85983a44588021f8a5fee574a8888c6de4344" + +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "boolinator" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfa8873f51c92e232f9bac4065cddef41b714152812bfc5f7672ba16d6ef8cd9" + +[[package]] +name = "bumpalo" +version = "3.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" + +[[package]] +name = "bytes" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cfg-match" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8100e46ff92eb85bf6dc2930c73f2a4f7176393c84a9446b3d501e1b354e7b34" + +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen", +] + +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn", +] + +[[package]] +name = "example_search" +version = "0.7.0" +dependencies = [ + "once_cell", + "serde-wasm-bindgen", + "tinysearch", + "wasm-bindgen", + "wee_alloc", + "xorf", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "gloo" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ce6f2dfa9f57f15b848efa2aade5e1850dc72986b87a2b0752d44ca08f4967" +dependencies = [ + "gloo-console-timer", + "gloo-events", + "gloo-file", + "gloo-timers", +] + +[[package]] +name = "gloo-console-timer" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b48675544b29ac03402c6dffc31a912f716e38d19f7e74b78b7e900ec3c941ea" +dependencies = [ + "web-sys", +] + +[[package]] +name = "gloo-events" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68b107f8abed8105e4182de63845afcc7b69c098b7852a813ea7462a320992fc" +dependencies = [ + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "gloo-file" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f9fecfe46b5dc3cc46f58e98ba580cc714f2c93860796d002eb3527a465ef49" +dependencies = [ + "gloo-events", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "gloo-timers" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "http" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + +[[package]] +name = "js-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "lexical-core" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe" +dependencies = [ + "arrayvec", + "bitflags", + "cfg-if 1.0.0", + "ryu", + "static_assertions", +] + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "memory_units" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" + +[[package]] +name = "nom" +version = "5.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" +dependencies = [ + "lexical-core", + "memchr", + "version_check", +] + +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + +[[package]] +name = "proc-macro2" +version = "1.0.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + +[[package]] +name = "semver" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a" + +[[package]] +name = "serde" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde-wasm-bindgen" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b4c031cd0d9014307d82b8abf653c0290fbdaeb4c02d00c63cf52f728628bf" +dependencies = [ + "js-sys", + "serde", + "wasm-bindgen", +] + +[[package]] +name = "serde_derive" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "slab" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d" +dependencies = [ + "autocfg", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinysearch" +version = "0.7.0" +dependencies = [ + "bincode", + "serde", + "xorf", +] + +[[package]] +name = "unicode-ident" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "775c11906edafc97bc378816b94585fbd9a054eabaf86fdd0ced94af449efab7" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasm-bindgen" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" +dependencies = [ + "cfg-if 1.0.0", + "serde", + "serde_json", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454" +dependencies = [ + "cfg-if 1.0.0", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" + +[[package]] +name = "web-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wee_alloc" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "memory_units", + "winapi", +] + +[[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" + +[[package]] +name = "xorf" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0b223640dfbc22009679ce79f6777ae1db79add62a07345605777f69274a052" +dependencies = [ + "serde", +] + +[[package]] +name = "ybc" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c409d21870c31cc3beb3b5ba8447306ecfac198876fa73bdce861b23299121" +dependencies = [ + "derive_more", + "web-sys", + "yew", + "yew-router", + "yewtil", +] + +[[package]] +name = "yew" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4d5154faef86dddd2eb333d4755ea5643787d20aca683e58759b0e53351409f" +dependencies = [ + "anyhow", + "anymap", + "bincode", + "cfg-if 1.0.0", + "cfg-match", + "console_error_panic_hook", + "gloo", + "http", + "indexmap", + "js-sys", + "log", + "ryu", + "serde", + "serde_json", + "slab", + "thiserror", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "yew-macro", +] + +[[package]] +name = "yew-example-crate" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "example_search", + "wasm-bindgen", + "ybc", + "yew", +] + +[[package]] +name = "yew-macro" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6e23bfe3dc3933fbe9592d149c9985f3047d08c637a884b9344c21e56e092ef" +dependencies = [ + "boolinator", + "lazy_static", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "yew-router" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27666236d9597eac9be560e841e415e20ba67020bc8cd081076be178e159c8bc" +dependencies = [ + "cfg-if 1.0.0", + "cfg-match", + "gloo", + "js-sys", + "log", + "nom", + "serde", + "serde_json", + "wasm-bindgen", + "web-sys", + "yew", + "yew-router-macro", + "yew-router-route-parser", +] + +[[package]] +name = "yew-router-macro" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c0ace2924b7a175e2d1c0e62ee7022a5ad840040dcd52414ce5f410ab322dba" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "yew-router-route-parser", +] + +[[package]] +name = "yew-router-route-parser" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de4a67208fb46b900af18a7397938b01f379dfc18da34799cfa8347eec715697" +dependencies = [ + "nom", +] + +[[package]] +name = "yewtil" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8543663ac49cd613df079282a1d8bdbdebdad6e02bac229f870fd4237b5d9aaa" +dependencies = [ + "log", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "yew", +] diff --git a/examples/yew-example-crate/Cargo.toml b/examples/yew-example-crate/Cargo.toml new file mode 100644 index 0000000..adfd90e --- /dev/null +++ b/examples/yew-example-crate/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "yew-example-crate" +version = "0.1.0" +edition = "2021" + + +[dependencies] +console_error_panic_hook = "0.1" +wasm-bindgen = "0.2" +ybc = "0.2" +yew = "0.18" +example_search = {path = "./example-search", features=[]} + + +[workspace] +members = [] \ No newline at end of file diff --git a/examples/yew-example-crate/Trunk.toml b/examples/yew-example-crate/Trunk.toml new file mode 100644 index 0000000..0613947 --- /dev/null +++ b/examples/yew-example-crate/Trunk.toml @@ -0,0 +1,13 @@ +[build] +target = "index.html" +dist = "dist" + +[[hooks]] +# This hook example shows all the current available fields. It will execute the equivalent of +# typing "echo Hello Trunk!" right at the start of the build process (even before the HTML file +# is read). By default, the command is spawned directly and no shell is used. +stage = "pre_build" +command = "sh" +command_arguments = [ + "-c", + "cd ../../ && cargo run --features=bin --release -- -m crate -p examples/yew-example-crate/example-search/ --crate-name example_search --engine-version 'path=\"../../../tinysearch\"' --non-top-level-crate fixtures/index.json"] diff --git a/examples/yew-example-crate/index.html b/examples/yew-example-crate/index.html new file mode 100644 index 0000000..f870492 --- /dev/null +++ b/examples/yew-example-crate/index.html @@ -0,0 +1,15 @@ + + + + + + Trunk | Yew | YBC + + + + + + + + + diff --git a/examples/yew-example-crate/src/index.scss b/examples/yew-example-crate/src/index.scss new file mode 100644 index 0000000..35a1942 --- /dev/null +++ b/examples/yew-example-crate/src/index.scss @@ -0,0 +1,3 @@ +@charset "utf-8"; + +html {} diff --git a/examples/yew-example-crate/src/main.rs b/examples/yew-example-crate/src/main.rs new file mode 100644 index 0000000..748f419 --- /dev/null +++ b/examples/yew-example-crate/src/main.rs @@ -0,0 +1,110 @@ +#![recursion_limit = "1024"] + +use console_error_panic_hook::set_once as set_panic_hook; +use ybc::TileCtx::{Child, Parent}; +use ybc::InputType::Text; +use yew::prelude::*; + +use example_search::search_local; + +struct App{ + value: String, + posts: Vec, + link: ComponentLink +} + +enum Msg { + SearchChanged(String) +} + +impl App{ + fn render_post(s: &String) -> yew::Html{ + html!{ + + + {s.clone()} + + + } + } +} + +impl Component for App { + type Message = Msg; + type Properties = (); + + fn create(_: Self::Properties, link: ComponentLink) -> Self { + Self{ + value: String::default(), + posts: Vec::new(), + link: link + } + } + + fn update(&mut self, msg: Self::Message) -> bool { + match msg { + Msg::SearchChanged(s) => { + if s != self.value{ + self.value = s; + let posts = search_local(self.value.clone(), 5); + self.posts = posts.iter().map(|x|x.0.clone()).collect(); + true + }else{ + false + } + } + } + } + + fn change(&mut self, _: Self::Properties) -> bool { + false + } + + fn view(&self) -> Html { + html! { + <> + + {"Tinysearch | Trunk | Yew | YBC | demo"} + + } + navstart=html!{} + navend=html!{ + <> + + + + + } + /> + + + + + { + for self.posts.iter().map(App::render_post) + } + + + }> + + + } + } +} + +fn main() { + set_panic_hook(); + + yew::start_app::(); +} \ No newline at end of file diff --git a/examples/yew-example-storage/.gitignore b/examples/yew-example-storage/.gitignore new file mode 100644 index 0000000..936c7e2 --- /dev/null +++ b/examples/yew-example-storage/.gitignore @@ -0,0 +1,3 @@ +example-search +target +dist diff --git a/examples/yew-example-storage/Cargo.lock b/examples/yew-example-storage/Cargo.lock new file mode 100644 index 0000000..8d62f2c --- /dev/null +++ b/examples/yew-example-storage/Cargo.lock @@ -0,0 +1,598 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" + +[[package]] +name = "anymap" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33954243bd79057c2de7338850b85983a44588021f8a5fee574a8888c6de4344" + +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "boolinator" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfa8873f51c92e232f9bac4065cddef41b714152812bfc5f7672ba16d6ef8cd9" + +[[package]] +name = "bumpalo" +version = "3.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" + +[[package]] +name = "bytes" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cfg-match" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8100e46ff92eb85bf6dc2930c73f2a4f7176393c84a9446b3d501e1b354e7b34" + +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "gloo" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ce6f2dfa9f57f15b848efa2aade5e1850dc72986b87a2b0752d44ca08f4967" +dependencies = [ + "gloo-console-timer", + "gloo-events", + "gloo-file", + "gloo-timers", +] + +[[package]] +name = "gloo-console-timer" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b48675544b29ac03402c6dffc31a912f716e38d19f7e74b78b7e900ec3c941ea" +dependencies = [ + "web-sys", +] + +[[package]] +name = "gloo-events" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68b107f8abed8105e4182de63845afcc7b69c098b7852a813ea7462a320992fc" +dependencies = [ + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "gloo-file" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f9fecfe46b5dc3cc46f58e98ba580cc714f2c93860796d002eb3527a465ef49" +dependencies = [ + "gloo-events", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "gloo-timers" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "http" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + +[[package]] +name = "js-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "lexical-core" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe" +dependencies = [ + "arrayvec", + "bitflags", + "cfg-if", + "ryu", + "static_assertions", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "nom" +version = "5.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" +dependencies = [ + "lexical-core", + "memchr", + "version_check", +] + +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + +[[package]] +name = "proc-macro2" +version = "1.0.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + +[[package]] +name = "semver" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a" + +[[package]] +name = "serde" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "slab" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d" +dependencies = [ + "autocfg", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinysearch" +version = "0.7.0" +dependencies = [ + "bincode", + "serde", + "xorf", +] + +[[package]] +name = "unicode-ident" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "775c11906edafc97bc378816b94585fbd9a054eabaf86fdd0ced94af449efab7" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasm-bindgen" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" +dependencies = [ + "cfg-if", + "serde", + "serde_json", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" + +[[package]] +name = "web-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "xorf" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0b223640dfbc22009679ce79f6777ae1db79add62a07345605777f69274a052" +dependencies = [ + "serde", +] + +[[package]] +name = "ybc" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c409d21870c31cc3beb3b5ba8447306ecfac198876fa73bdce861b23299121" +dependencies = [ + "derive_more", + "web-sys", + "yew", + "yew-router", + "yewtil", +] + +[[package]] +name = "yew" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4d5154faef86dddd2eb333d4755ea5643787d20aca683e58759b0e53351409f" +dependencies = [ + "anyhow", + "anymap", + "bincode", + "cfg-if", + "cfg-match", + "console_error_panic_hook", + "gloo", + "http", + "indexmap", + "js-sys", + "log", + "ryu", + "serde", + "serde_json", + "slab", + "thiserror", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "yew-macro", +] + +[[package]] +name = "yew-example-storage" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "once_cell", + "tinysearch", + "wasm-bindgen", + "ybc", + "yew", +] + +[[package]] +name = "yew-macro" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6e23bfe3dc3933fbe9592d149c9985f3047d08c637a884b9344c21e56e092ef" +dependencies = [ + "boolinator", + "lazy_static", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "yew-router" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27666236d9597eac9be560e841e415e20ba67020bc8cd081076be178e159c8bc" +dependencies = [ + "cfg-if", + "cfg-match", + "gloo", + "js-sys", + "log", + "nom", + "serde", + "serde_json", + "wasm-bindgen", + "web-sys", + "yew", + "yew-router-macro", + "yew-router-route-parser", +] + +[[package]] +name = "yew-router-macro" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c0ace2924b7a175e2d1c0e62ee7022a5ad840040dcd52414ce5f410ab322dba" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "yew-router-route-parser", +] + +[[package]] +name = "yew-router-route-parser" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de4a67208fb46b900af18a7397938b01f379dfc18da34799cfa8347eec715697" +dependencies = [ + "nom", +] + +[[package]] +name = "yewtil" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8543663ac49cd613df079282a1d8bdbdebdad6e02bac229f870fd4237b5d9aaa" +dependencies = [ + "log", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "yew", +] diff --git a/examples/yew-example-storage/Cargo.toml b/examples/yew-example-storage/Cargo.toml new file mode 100644 index 0000000..b33550a --- /dev/null +++ b/examples/yew-example-storage/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "yew-example-storage" +version = "0.1.0" +edition = "2021" + + +[dependencies] +console_error_panic_hook = "0.1" +wasm-bindgen = "0.2" +ybc = "0.2" +yew = "0.18" +once_cell = "*" +tinysearch = {path="../../tinysearch"} + +[workspace] +members = [] \ No newline at end of file diff --git a/examples/yew-example-storage/Trunk.toml b/examples/yew-example-storage/Trunk.toml new file mode 100644 index 0000000..62d4bb9 --- /dev/null +++ b/examples/yew-example-storage/Trunk.toml @@ -0,0 +1,10 @@ +[build] +target = "index.html" +dist = "dist" + +[[hooks]] +stage = "pre_build" +command = "sh" +command_arguments = [ + "-c", + "cd ../../ && cargo run --features=bin --release -- -m storage -p examples/yew-example-storage/src/ fixtures/index.json"] diff --git a/examples/yew-example-storage/index.html b/examples/yew-example-storage/index.html new file mode 100644 index 0000000..be37e29 --- /dev/null +++ b/examples/yew-example-storage/index.html @@ -0,0 +1,15 @@ + + + + + + Trunk | Yew | YBC + + + + + + + + + diff --git a/examples/yew-example-storage/src/index.scss b/examples/yew-example-storage/src/index.scss new file mode 100644 index 0000000..35a1942 --- /dev/null +++ b/examples/yew-example-storage/src/index.scss @@ -0,0 +1,3 @@ +@charset "utf-8"; + +html {} diff --git a/examples/yew-example-storage/src/main.rs b/examples/yew-example-storage/src/main.rs new file mode 100644 index 0000000..d3e5ac7 --- /dev/null +++ b/examples/yew-example-storage/src/main.rs @@ -0,0 +1,115 @@ +#![recursion_limit = "1024"] + +use console_error_panic_hook::set_once as set_panic_hook; +use once_cell::sync::Lazy; +use ybc::TileCtx::{Child, Parent}; +use ybc::InputType::Text; +use yew::prelude::*; +use tinysearch::{Filters, Storage,search}; + +struct App{ + value: String, + posts: Vec, + link: ComponentLink +} + +enum Msg { + SearchChanged(String) +} + +static FILTERS: Lazy = Lazy::new(|| { + let bytes = include_bytes!("storage"); + Storage::from_bytes(bytes).unwrap().filters +}); + +impl App{ + fn render_post(s: &String) -> yew::Html{ + html!{ + + + {s.clone()} + + + } + } +} + +impl Component for App { + type Message = Msg; + type Properties = (); + + fn create(_: Self::Properties, link: ComponentLink) -> Self { + Self{ + value: String::default(), + posts: Vec::new(), + link: link + } + } + + fn update(&mut self, msg: Self::Message) -> bool { + match msg { + Msg::SearchChanged(s) => { + if s != self.value{ + self.value = s; + let posts = search(&FILTERS, self.value.clone(), 5); + self.posts = posts.iter().map(|x|x.0.clone()).collect(); + true + }else{ + false + } + } + } + } + + fn change(&mut self, _: Self::Properties) -> bool { + false + } + + fn view(&self) -> Html { + html! { + <> + + {"Tinysearch | Trunk | Yew | YBC | demo"} + + } + navstart=html!{} + navend=html!{ + <> + + + + + } + /> + + + + + { + for self.posts.iter().map(App::render_post) + } + + + }> + + + } + } +} + +fn main() { + set_panic_hook(); + + yew::start_app::(); +} \ No newline at end of file diff --git a/report.json b/report.json new file mode 100644 index 0000000..940d8ff --- /dev/null +++ b/report.json @@ -0,0 +1,26 @@ +{ + "detailed_stats": false, + "total": 40, + "successful": 39, + "unknown": 0, + "unsupported": 0, + "timeouts": 0, + "redirects": 0, + "excludes": 0, + "errors": 1, + "cached": 0, + "success_map": {}, + "fail_map": { + "./README.md": [ + { + "url": "http://0.0.0.0:8000/demo.html", + "status": { + "text": "Failed: Network error", + "code": 404 + } + } + ] + }, + "suggestion_map": {}, + "excluded_map": {} +} \ No newline at end of file diff --git a/shared/.gitignore b/shared/.gitignore deleted file mode 100644 index 2f88dba..0000000 --- a/shared/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/target -**/*.rs.bk -Cargo.lock \ No newline at end of file diff --git a/shared/Cargo.toml b/shared/Cargo.toml deleted file mode 100644 index 42cc843..0000000 --- a/shared/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "tinysearch-shared" -version = "0.7.0" -authors = ["Matthias Endler "] -edition = "2021" -description = "Shared libraries for tinysearch - a tiny search engine for static websites" -license = "Apache-2.0/MIT" -documentation = "https://github.com/mre/tinysearch/blob/master/README.md" -homepage = "https://github.com/mre/tinysearch" -repository = "https://github.com/mre/tinysearch" - -[dependencies] -bincode = "1.3.3" - -[dependencies.serde] -version = "1.0.127" -features = ["derive"] - -[dependencies.xorf] -version = "0.7.2" -default-features = false -features = ["serde"] diff --git a/shared/src/lib.rs b/shared/src/lib.rs deleted file mode 100644 index 86afffd..0000000 --- a/shared/src/lib.rs +++ /dev/null @@ -1,48 +0,0 @@ -use bincode::Error as BincodeError; -use serde::{Deserialize, Serialize}; -use std::convert::From; -use xorf::{Filter, HashProxy, Xor8}; - -use std::collections::hash_map::DefaultHasher; - -type Title = String; -type Url = String; -type Meta = Option; -pub type PostId = (Title, Url, Meta); -pub type PostFilter = (PostId, HashProxy); -pub type Filters = Vec; - -#[derive(Serialize, Deserialize)] -pub struct Storage { - pub filters: Filters, -} - -impl From for Storage { - fn from(filters: Filters) -> Self { - Storage { filters } - } -} - -pub trait Score { - fn score(&self, terms: &[String]) -> usize; -} - -// the score denotes the number of terms from the query that are contained in the -// current filter -impl Score for HashProxy { - fn score(&self, terms: &[String]) -> usize { - terms.iter().filter(|term| self.contains(term)).count() - } -} - -impl Storage { - pub fn to_bytes(&self) -> Result, BincodeError> { - let encoded: Vec = bincode::serialize(&self)?; - Ok(encoded) - } - - pub fn from_bytes(bytes: &[u8]) -> Result { - let decoded: Filters = bincode::deserialize(bytes)?; - Ok(Storage { filters: decoded }) - } -} diff --git a/testscript.sh b/testscript.sh new file mode 100755 index 0000000..976e2f1 --- /dev/null +++ b/testscript.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +# + +for example in `ls -1 examples`; do + (cd "examples/$example" && trunk build index.html) || exit 1; +done diff --git a/bin/.gitignore b/tinysearch/.gitignore similarity index 100% rename from bin/.gitignore rename to tinysearch/.gitignore diff --git a/tinysearch/Cargo.toml b/tinysearch/Cargo.toml new file mode 100644 index 0000000..f2b254d --- /dev/null +++ b/tinysearch/Cargo.toml @@ -0,0 +1,58 @@ +[package] +name = "tinysearch" +authors = ["Matthias Endler "] +version = "0.7.0" +edition = "2021" +description = "A tiny search engine for static websites" +license = "Apache-2.0/MIT" +documentation = "https://github.com/mre/tinysearch/blob/master/README.md" +homepage = "https://github.com/mre/tinysearch" +repository = "https://github.com/mre/tinysearch" +include = ["/src/assets"] +[lib] + +[[bin]] +name = "tinysearch" +required-features = ["bin"] + +[dependencies] +bincode = "1.3.3" + + +argh = { version = "0.1.10", optional = true } +log = { version = "0.4.19", optional = true } +serde_json = { version = "1.0.103", optional = true } +anyhow = { version = "1.0.72", optional = true } +tempfile = { version = "3.7.0", optional = true } + +serde_derive = { version = "1.0.175", optional = true } +phf = { version = "0.11.2", optional = true } +strip_markdown = { version = "0.2.0", optional = true } +strum = { version = "0.25.0", features = ["derive"], optional = true } +toml_edit = { version = "0.19.14", optional = true } + + +[dependencies.serde] +version = "1.0.175" +features = ["derive"] + +[dependencies.xorf] +version = "0.8.1" +default-features = false +features = ["serde"] + + +[features] +default = [] +bin = [ + "argh", + "log", + "serde_json", + "anyhow", + "tempfile", + "serde_derive", + "phf", + "strip_markdown", + "strum", + "toml_edit", +] diff --git a/engine/Cargo.toml b/tinysearch/assets/crate/Cargo.toml similarity index 59% rename from engine/Cargo.toml rename to tinysearch/assets/crate/Cargo.toml index 54ae348..b610f75 100644 --- a/engine/Cargo.toml +++ b/tinysearch/assets/crate/Cargo.toml @@ -1,5 +1,7 @@ +# WARNING: this file is autogenerated! + [package] -name = "tinysearch-engine" +name = "THIS_VALUE_SHOULD_BE_FILLED" authors = ["Matthias Endler "] version = "0.7.0" edition = "2021" @@ -7,26 +9,28 @@ description = "A tiny search engine for static websites" license = "Apache-2.0/MIT" documentation = "https://github.com/mre/tinysearch/blob/master/README.md" homepage = "https://github.com/mre/tinysearch" +wasm-opt = false [lib] crate-type = ["cdylib"] +[features] +default=["bind"] +bind=["wee_alloc", "wasm-bindgen", "serde-wasm-bindgen"] + [dependencies] -wee_alloc = "0.4.5" once_cell = "1.8.0" +tinysearch = "THIS_VALUE_SHOULD_BE_FILLED" -[dependencies.tinysearch-shared] -path = "../shared" -version = "0.7.0" + +wee_alloc = {version = "0.4.5", optional = true} +wasm-bindgen= {version = "0.2.75", optional = true} +serde-wasm-bindgen = {version = "0.4", optional = true} [dependencies.xorf] version = "0.7.2" default-features = false -[dependencies.wasm-bindgen] -version = "0.2.75" -features = ["serde-serialize"] - [workspace] members = [] @@ -34,3 +38,4 @@ members = [] opt-level = 's' # Optimize for size. lto = true codegen-units = 1 + diff --git a/tinysearch/assets/crate/src/lib.rs b/tinysearch/assets/crate/src/lib.rs new file mode 100644 index 0000000..3556be5 --- /dev/null +++ b/tinysearch/assets/crate/src/lib.rs @@ -0,0 +1,27 @@ +use once_cell::sync::Lazy; + +#[cfg(feature = "bind")] +use serde_wasm_bindgen; +#[cfg(feature = "bind")] +use wasm_bindgen::prelude::*; + +use tinysearch::{search as base_search, Filters, PostId, Storage}; + +#[cfg(feature = "bind")] +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +static FILTERS: Lazy = Lazy::new(|| { + let bytes = include_bytes!("storage"); + Storage::from_bytes(bytes).unwrap().filters +}); + +pub fn search_local(query: String, num_results: usize) -> Vec<&'static PostId> { + base_search(&FILTERS, query, num_results) +} + +#[cfg(feature = "bind")] +#[wasm_bindgen] +pub fn search(query: String, num_results: usize) -> JsValue { + serde_wasm_bindgen::to_value(&search_local(query, num_results)).unwrap() +} diff --git a/tinysearch/assets/demo.html b/tinysearch/assets/demo.html new file mode 100644 index 0000000..5fae875 --- /dev/null +++ b/tinysearch/assets/demo.html @@ -0,0 +1,72 @@ + + + + + + + + + + + + +

Search

+ +

Results

+
    +
+ + + \ No newline at end of file diff --git a/bin/assets/stopwords b/tinysearch/assets/stopwords similarity index 100% rename from bin/assets/stopwords rename to tinysearch/assets/stopwords diff --git a/tinysearch/src/bin/tinysearch.rs b/tinysearch/src/bin/tinysearch.rs new file mode 100644 index 0000000..b489067 --- /dev/null +++ b/tinysearch/src/bin/tinysearch.rs @@ -0,0 +1,431 @@ +#![cfg(feature = "bin")] +#[macro_use] +extern crate log; + +mod utils; +use utils::assets; +use utils::index; +use utils::storage; + +pub use anyhow::{Error, Result}; +use anyhow::{bail, Context}; +use argh::FromArgs; +use std::path::PathBuf; +use std::process::{Command, Stdio}; +use std::str::FromStr; +use std::{env, fs}; +use tempfile::TempDir; +use toml_edit::{value, Document}; + +use index::Posts; +use strum::{EnumString, IntoStaticStr}; + +fn ensure_exists(path: PathBuf) -> Result { + if !path.exists() { + fs::create_dir_all(&path)?; + } + let path = path.canonicalize()?; + if !path.exists() { + for path in fs::read_dir(&path)? { + println!("Name: {}", path.unwrap().path().display()) + } + bail!("Directory could not be created at {}", &path.display()); + } + Ok(path) +} + +#[derive(Debug)] +enum DirOrTemp { + Path(PathBuf), + Temp(TempDir), +} + +impl DirOrTemp { + pub fn path(&self) -> PathBuf { + match self { + DirOrTemp::Path(p) => p.clone(), + DirOrTemp::Temp(p) => p.path().to_path_buf(), + } + } +} + +impl Default for DirOrTemp { + fn default() -> Self { + Self::Temp(TempDir::new().expect("Failed to create a temporary directory")) + } +} + +impl FromStr for DirOrTemp { + type Err = ::Err; + + fn from_str(s: &str) -> std::result::Result { + Ok(DirOrTemp::Path(PathBuf::from_str(s)?)) + } +} + +#[derive(IntoStaticStr, EnumString, Clone)] +#[strum(serialize_all = "snake_case")] +enum OutputMode { + Search, + Storage, + Crate, + Wasm, +} + +fn parse_engine_version(str: &str) -> Result { + let doc = str.parse::().map_err(|e| e.to_string())?; + Ok(doc.as_table().clone()) +} + +#[derive(FromArgs, Clone)] +/// A tiny, static search engine for static websites +/// +/// +/// It can run in several modes (-m/--mode argument). +/// Valid modes are: +/// **search** - runs search engine on generated storage data, +/// **storage** - generates storage data for posts, +/// **crate** - creates a Rust crate with storage data, +/// **wasm** - creates a crate and generates a loadable js/wasm script. +/// +struct Opt { + /// show version and exit + #[argh(switch)] + version: bool, + + /// output mode + #[argh(option, short = 'm', long = "mode", default = "OutputMode::Wasm")] + output_mode: OutputMode, + + /// term to search in posts (only for search mode) + #[argh( + option, + short = 'S', + long = "search-term", + default = "String::default()" + )] + search_term: String, + + /// number of posts to show in search results (only for search mode) + #[argh(option, short = 'N', long = "num-searches", default = "5")] + num_searches: usize, + + /// input file to process (either JSON with posts for code generation or storage for inference) + #[argh(positional)] + input_file: Option, + + /// output path for WASM module ("wasm_output" directory by default) + #[argh( + option, + short = 'p', + long = "path", + default = "\"./wasm_output\".into()" + )] + out_path: PathBuf, + + /// where to put generated crate + /// * In wasm mode crate is generated: + /// * If this option is specified: in this path. + /// * If this option is omitted: in a temp directory removed after run. + /// * In crate mode this is ignored in favor of -p/--path. + #[argh(option, long = "crate-path")] + crate_path: Option, + + /// this version will be used in Cargo.toml for the generated crate + /// (only used in wasm, crate modes). This should be a valid TOML table definition. + /// Default is 'version="env!("CARGO_PKG_VERSION")"'. If you have a local version of + /// tinysearch, you can specify 'path="/path/to/tinysearch"' + #[argh( + option, + short = 'e', + long = "engine-version", + from_str_fn(parse_engine_version), + default = "format!(\"version=\\\"{}\\\"\", env!(\"CARGO_PKG_VERSION\")).parse::().unwrap().as_table().clone()" + )] + engine_version: toml_edit::Table, + + /// this name will be used in Cargo.toml for the generated crate (only used in wasm and crate modes) + #[argh( + option, + long = "crate-name", + default = "\"tinysearch-engine\".into()" + )] + crate_name: String, + + /// removes all top-level configs from Cargo.toml of generated crate and makes it locally importable (only makes sense in crate mode) + #[argh( + switch, + long = "non-top-level-crate" + )] + non_top_level_crate: bool, + + /// optimize the output using binaryen (only valid in wasm mode) + #[argh(switch, short = 'o', long = "optimize")] + optimize: bool, +} + +trait Stage: Sized { + fn from_opt(opt: &Opt) -> Result; + + fn build(&self) -> Result<(), Error>; +} + +#[derive(Default)] +struct Search { + storage_file: PathBuf, + term: String, + num_searches: usize, +} + +impl Stage for Search { + fn from_opt(opt: &Opt) -> Result { + let input = opt.input_file.clone().context("Missing input file")?; + let term = opt.search_term.clone(); + Ok(Self { + storage_file: input + .canonicalize() + .with_context(|| format!("Failed to find file: {}", input.display()))?, + term, + num_searches: opt.num_searches, + }) + } + + fn build(&self) -> Result<(), Error> { + use tinysearch::{search as base_search, Storage}; + let bytes = fs::read(&self.storage_file).with_context(|| { + format!("Failed to read input file: {}", self.storage_file.display()) + })?; + let filters = Storage::from_bytes(&bytes)?.filters; + let results = base_search(&filters, self.term.clone(), self.num_searches); + for result in results { + println!( + "Title: {}, Url: {}, Meta: {:?}", + result.0, result.1, result.2 + ); + } + Ok(()) + } +} + +#[derive(Default)] +struct Storage { + posts_index: PathBuf, + out_path: PathBuf, +} + +impl Stage for Storage { + fn from_opt(opt: &Opt) -> Result { + Ok(Self { + posts_index: opt.input_file.clone().context("No input file")?, + out_path: ensure_exists(opt.out_path.clone())?, + }) + } + + fn build(&self) -> Result<(), Error> { + let storage_file = self.out_path.join("storage"); + println!( + "Creating storage file for posts {} in file {}", + self.posts_index.display(), + storage_file.display() + ); + let posts: Posts = index::read( + fs::read_to_string(&self.posts_index) + .with_context(||format!("Failed to read file {}", self.posts_index.display()))? + ).with_context(||format!("Failed to decode {}", self.posts_index.display()))?; + trace!("Generating storage from posts: {:#?}", posts); + storage::write(posts, &storage_file)?; + println!("Storage ready in file {}", storage_file.display()); + Ok(()) + } +} + +#[derive(Default)] +struct Crate { + s: Storage, + out_path: PathBuf, + crate_name: String, + engine_version: toml_edit::Table, + non_top_level: bool +} + +impl Stage for Crate { + fn from_opt(opt: &Opt) -> Result { + if opt.crate_path.is_some(){ + bail!("Don't use --crate-path to specify crate output dir!"); + } + let out_path = ensure_exists(opt.out_path.clone())?; + let storage_opt = { + let mut ret: Opt = opt.clone(); + ret.out_path = ensure_exists(out_path.join("src"))?; + ret + }; + + Ok(Self { + s: Storage::from_opt(&storage_opt)?, + out_path, + crate_name: opt.crate_name.clone(), + engine_version: opt.engine_version.clone(), + non_top_level: opt.non_top_level_crate + }) + } + + fn build(&self) -> Result<(), Error> { + println!( + "Creating tinysearch implementation crate {} in directory {}", + self.crate_name, + self.out_path.display() + ); + let cargo_toml = self.out_path.join("Cargo.toml"); + let mut cargo_toml_contents = assets::CRATE_CARGO_TOML.parse::()?; + cargo_toml_contents["package"]["name"] = value(self.crate_name.clone()); + cargo_toml_contents["dependencies"]["tinysearch"] = + toml_edit::Item::Table(self.engine_version.clone()); + if self.non_top_level{ + cargo_toml_contents.as_table_mut().remove("workspace"); + cargo_toml_contents.as_table_mut().remove("profile"); + cargo_toml_contents.as_table_mut().remove("lib"); + cargo_toml_contents["lib"] = toml_edit::table(); + } + fs::write(&cargo_toml, cargo_toml_contents.to_string())?; + + // let mut file = fs::OpenOptions::new().write(true).truncate(true).open(&cargo_toml)?; + // file.write(new.as_bytes())?; + + self.s.build().context("Failed building storage")?; + fs::write( + &self.out_path.join("src").join("lib.rs"), + assets::CRATE_LIB_RS, + )?; + println!("Crate content generated in {}/", &self.out_path.display()); + Ok(()) + } +} + +#[derive(Default)] +struct Wasm { + c: Crate, + out_path: PathBuf, + crate_path: DirOrTemp, + optimize: bool, +} + +impl Wasm { + fn ensure_crate_path(crate_path: &Option) -> Result { + Ok(match crate_path { + Some(p) => DirOrTemp::Path(ensure_exists(p.clone())?), + None => DirOrTemp::default(), + }) + } +} + +impl Stage for Wasm { + fn from_opt(opt: &Opt) -> Result { + let crate_path = Wasm::ensure_crate_path(&opt.crate_path)?; + let crate_opt = { + let mut ret: Opt = opt.clone(); + ret.out_path = crate_path.path(); + ret.crate_path = None; + ret + }; + Ok(Self { + c: Crate::from_opt(&crate_opt)?, + out_path: ensure_exists(opt.out_path.clone())?, + crate_path, + optimize: opt.optimize, + }) + } + + fn build(self: &Wasm) -> Result<(), Error> { + self.c.build().context("Failed generating crate")?; + println!("Compiling WASM module using wasm-pack"); + let crate_path = self.crate_path.path(); + run_output( + Command::new("wasm-pack") + .arg("build") + .arg(&crate_path) + .arg("--target") + .arg("web") + .arg("--release") + .arg("--out-dir") + .arg(&self.out_path), + )?; + let wasm_name = self.c.crate_name.replace('-', "_"); + + if self.optimize { + let wasm_file = format!("{}_bg.wasm", &wasm_name); + run_output( + Command::new("wasm-opt") + .current_dir(&self.out_path) + .arg("-Oz") + .arg("-o") + .arg(&wasm_file) + .arg(&wasm_file), + )?; + } + let html_path = self.out_path.join("demo.html"); + fs::write( + &html_path, + assets::DEMO_HTML.replace("{WASM_NAME}", &wasm_name), + ) + .with_context(|| format!("Failed writing demo.html to {}", &html_path.display()))?; + println!("All done! Open the output folder with a web server to try the demo."); + Ok(()) + } +} + +pub fn main() -> Result<(), Error> { + let opt: Opt = argh::from_env(); + + if opt.version { + println!("tinysearch {}", env!("CARGO_PKG_VERSION")); + std::process::exit(0); + } + + let parse_ctx = || { + format!( + "Failed to parse options for {} mode", + Into::<&'static str>::into(&opt.output_mode) + ) + }; + + match opt.output_mode { + OutputMode::Search => Search::from_opt(&opt).with_context(parse_ctx)?.build(), + OutputMode::Storage => Storage::from_opt(&opt).with_context(parse_ctx)?.build(), + OutputMode::Crate => Crate::from_opt(&opt).with_context(parse_ctx)?.build(), + OutputMode::Wasm => Wasm::from_opt(&opt).with_context(parse_ctx)?.build(), + } + .with_context(|| { + format!( + "Failed to build {} mode", + Into::<&'static str>::into(&opt.output_mode) + ) + }) +} + +pub fn run_output(cmd: &mut Command) -> Result { + println!("running {:?}", cmd); + let output = cmd + .stderr(Stdio::inherit()) + .output() + .with_context(|| format!("failed to run {:?}", cmd))?; + + if !output.status.success() { + anyhow::bail!("failed to execute {:?}\nstatus: {}", cmd, output.status) + } + Ok(String::from_utf8_lossy(&output.stdout).into_owned()) +} + +// #[cfg(test)] +// mod tests { +// use super::*; + +// #[test] +// fn test_compile_example(){ +// run_output( +// Command::new("/home/delphi/.cargo/bin/trunk") +// .current_dir("../examples/yew-example-storage") +// .arg("build") +// .arg("--release") +// ).unwrap(); +// } +// } \ No newline at end of file diff --git a/tinysearch/src/bin/utils/assets.rs b/tinysearch/src/bin/utils/assets.rs new file mode 100644 index 0000000..132d688 --- /dev/null +++ b/tinysearch/src/bin/utils/assets.rs @@ -0,0 +1,7 @@ +pub static CRATE_CARGO_TOML: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/crate/Cargo.toml")); +pub static CRATE_LIB_RS: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/crate/src/lib.rs")); + +// Include a bare-bones HTML page template that demonstrates how tinysearch is used +pub static DEMO_HTML: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/demo.html")); + +pub static STOP_WORDS: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/stopwords")); \ No newline at end of file diff --git a/bin/src/index.rs b/tinysearch/src/bin/utils/index.rs similarity index 100% rename from bin/src/index.rs rename to tinysearch/src/bin/utils/index.rs diff --git a/tinysearch/src/bin/utils/mod.rs b/tinysearch/src/bin/utils/mod.rs new file mode 100644 index 0000000..1c3b10c --- /dev/null +++ b/tinysearch/src/bin/utils/mod.rs @@ -0,0 +1,3 @@ +pub mod assets; +pub mod index; +pub mod storage; diff --git a/bin/src/storage.rs b/tinysearch/src/bin/utils/storage.rs similarity index 87% rename from bin/src/storage.rs rename to tinysearch/src/bin/utils/storage.rs index b0c9979..33249cd 100644 --- a/bin/src/storage.rs +++ b/tinysearch/src/bin/utils/storage.rs @@ -1,18 +1,20 @@ use anyhow::Error; use std::collections::{HashMap, HashSet}; use std::fs; +use std::path; -use crate::index::Posts; +use super::assets::STOP_WORDS; +use super::index::Posts; use strip_markdown::strip_markdown; -use tinysearch_shared::{Filters, PostId, Storage}; +use tinysearch::{Filters, PostId, Storage}; use xorf::HashProxy; -pub fn write(posts: Posts) -> Result<(), Error> { +pub fn write(posts: Posts, path: &path::PathBuf) -> Result<(), Error> { let filters = build(posts)?; trace!("Storage::from"); let storage = Storage::from(filters); trace!("Write"); - fs::write("storage", storage.to_bytes()?)?; + fs::write(path, storage.to_bytes()?)?; trace!("ok"); Ok(()) } @@ -45,8 +47,7 @@ pub fn generate_filters(posts: HashMap>) -> Result = stopwords.split_whitespace().map(String::from).collect(); + let stopwords: HashSet = STOP_WORDS.split_whitespace().map(String::from).collect(); let split_posts: HashMap>> = posts .into_iter() @@ -97,11 +98,11 @@ mod tests { let mut posts = HashMap::new(); posts.insert( ( - "Maybe You Don't Need Kubernetes, Or Excel - You Know".to_string(),//title - "".to_string(),//url - None,//meta + "Maybe You Don't Need Kubernetes, Or Excel - You Know".to_string(), //title + "".to_string(), //url + None, //meta ), - None,//body + None, //body ); let filters = generate_filters(posts).unwrap(); assert_eq!(filters.len(), 1); diff --git a/tinysearch/src/lib.rs b/tinysearch/src/lib.rs new file mode 100644 index 0000000..8d85e7d --- /dev/null +++ b/tinysearch/src/lib.rs @@ -0,0 +1,83 @@ +use bincode::Error as BincodeError; +use serde::{Deserialize, Serialize}; +use std::cmp::Reverse; +use std::collections::hash_map::DefaultHasher; +use std::convert::From; +use xorf::{Filter as XorfFilter, HashProxy, Xor8}; + +type Title = String; +type Url = String; +type Meta = Option; +pub type PostId = (Title, Url, Meta); +pub type PostFilter = (PostId, HashProxy); +pub type Filters = Vec; + +#[derive(Serialize, Deserialize)] +pub struct Storage { + pub filters: Filters, +} + +impl From for Storage { + fn from(filters: Filters) -> Self { + Storage { filters } + } +} + +pub trait Score { + fn score(&self, terms: &[String]) -> usize; +} + +// the score denotes the number of terms from the query that are contained in the +// current filter +impl Score for HashProxy { + fn score(&self, terms: &[String]) -> usize { + terms.iter().filter(|term| self.contains(term)).count() + } +} + +impl Storage { + pub fn to_bytes(&self) -> Result, BincodeError> { + let encoded: Vec = bincode::serialize(&self)?; + Ok(encoded) + } + + pub fn from_bytes(bytes: &[u8]) -> Result { + let decoded: Filters = bincode::deserialize(bytes)?; + Ok(Storage { filters: decoded }) + } +} + +pub type Filter = HashProxy; + +const TITLE_WEIGHT: usize = 3; + +// Wrapper around filter score, that also scores the post title +// Post title score has a higher weight than post body +fn score(title: &str, search_terms: &[String], filter: &Filter) -> usize { + let title_terms: Vec = tokenize(title); + let title_score: usize = search_terms + .iter() + .filter(|term| title_terms.contains(term)) + .count(); + TITLE_WEIGHT * title_score + filter.score(search_terms) +} + +fn tokenize(s: &str) -> Vec { + s.to_lowercase() + .split_whitespace() + .filter(|&t| !t.trim().is_empty()) + .map(String::from) + .collect() +} +pub fn search(filters: &'_ Filters, query: String, num_results: usize) -> Vec<&'_ PostId> { + let search_terms: Vec = tokenize(&query); + let mut matches: Vec<(&PostId, usize)> = filters + .iter() + .map(|(post_id, filter)| (post_id, score(&post_id.0, &search_terms, filter))) + .filter(|(_post_id, score)| *score > 0) + .collect(); + + matches.sort_by_key(|k| Reverse(k.1)); + + matches.into_iter().take(num_results).map(|p| p.0).collect() +}