diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0a9ca30..18b4ae7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [main, master] + branches: [main] pull_request: - branches: [main, master] + branches: [main] env: CARGO_TERM_COLOR: always @@ -17,45 +17,36 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - rust: [stable, 1.70.0] + rust: [stable, "1.85.0"] steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v6 - name: Install Rust - uses: dtolnay/rust-action@stable + uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} - - name: Cache cargo registry - uses: actions/cache@v4 + - uses: Swatinem/rust-cache@v2 with: - path: | - ~/.cargo/registry - ~/.cargo/git - target - key: ${{ runner.os }}-cargo-${{ matrix.rust }}-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-cargo-${{ matrix.rust }}- - - - name: Build - run: cargo build --verbose + key: ${{ matrix.rust }} - name: Run tests - run: cargo test --verbose + run: cargo test --locked --verbose lint: name: Lint runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v6 - name: Install Rust - uses: dtolnay/rust-action@stable + uses: dtolnay/rust-toolchain@stable with: - toolchain: stable components: rustfmt, clippy + - uses: Swatinem/rust-cache@v2 + - name: Check formatting run: cargo fmt --all -- --check @@ -66,14 +57,14 @@ jobs: name: Documentation runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v6 - name: Install Rust - uses: dtolnay/rust-action@stable - with: - toolchain: stable + uses: dtolnay/rust-toolchain@stable + + - uses: Swatinem/rust-cache@v2 - name: Check documentation run: cargo doc --no-deps --document-private-items env: - RUSTDOCFLAGS: -D warnings + RUSTDOCFLAGS: "-Dwarnings" diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 42ee8ed..ae4fa88 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -13,27 +13,23 @@ jobs: name: Test before publish runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v6 - name: Install Rust - uses: dtolnay/rust-action@stable - with: - toolchain: stable + uses: dtolnay/rust-toolchain@stable - name: Run tests - run: cargo test --verbose + run: cargo test --locked --verbose publish: name: Publish to crates.io needs: test runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v6 - name: Install Rust - uses: dtolnay/rust-action@stable - with: - toolchain: stable + uses: dtolnay/rust-toolchain@stable - name: Verify version matches tag run: | diff --git a/Cargo.lock b/Cargo.lock index 8e45df1..81901fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,6 +25,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + [[package]] name = "atomic-waker" version = "1.1.2" @@ -39,9 +45,9 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "bitflags" -version = "2.10.0" +version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" +checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" [[package]] name = "block-buffer" @@ -54,21 +60,21 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.19.1" +version = "3.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" +checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" [[package]] name = "bytes" -version = "1.11.0" +version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" [[package]] name = "cc" -version = "1.2.54" +version = "1.2.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6354c81bbfd62d9cfa9cb3c773c2b7b2a3a482d569de977fd0e961f6e7c00583" +checksum = "7a0dd1ca384932ff3641c8718a02769f1698e7563dc6974ffd03346116310423" dependencies = [ "find-msvc-tools", "shlex", @@ -90,6 +96,16 @@ dependencies = [ "libc", ] +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -160,9 +176,9 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "find-msvc-tools" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" [[package]] name = "fnv" @@ -170,6 +186,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "foreign-types" version = "0.3.2" @@ -196,41 +218,40 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.31" +version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" dependencies = [ "futures-core", ] [[package]] name = "futures-core" -version = "0.3.31" +version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" [[package]] name = "futures-sink" -version = "0.3.31" +version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" +checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893" [[package]] name = "futures-task" -version = "0.3.31" +version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" [[package]] name = "futures-util" -version = "0.3.31" +version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" dependencies = [ "futures-core", "futures-task", "pin-project-lite", - "pin-utils", "slab", ] @@ -257,14 +278,15 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.3.4" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555" dependencies = [ "cfg-if", "libc", "r-efi", "wasip2", + "wasip3", ] [[package]] @@ -286,12 +308,27 @@ dependencies = [ "tracing", ] +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + [[package]] name = "hashbrown" version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "http" version = "1.4.0" @@ -387,14 +424,13 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.19" +version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "727805d60e7938b76b826a6ef209eb70eaa1812794f9424d4a4e2d740662df5f" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" dependencies = [ "base64", "bytes", "futures-channel", - "futures-core", "futures-util", "http", "http-body", @@ -492,6 +528,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + [[package]] name = "idna" version = "1.1.0" @@ -520,14 +562,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.16.1", + "serde", + "serde_core", ] [[package]] name = "ipnet" -version = "2.11.0" +version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" +checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2" [[package]] name = "iri-string" @@ -547,25 +591,31 @@ checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" [[package]] name = "js-sys" -version = "0.3.85" +version = "0.3.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3" +checksum = "b49715b7073f385ba4bc528e5747d02e66cb39c6146efb66b781f131f0fb399c" dependencies = [ "once_cell", "wasm-bindgen", ] +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + [[package]] name = "libc" -version = "0.2.180" +version = "0.2.183" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" +checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d" [[package]] name = "linux-raw-sys" -version = "0.11.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" [[package]] name = "litemap" @@ -600,9 +650,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.6" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" [[package]] name = "mime" @@ -623,9 +673,9 @@ dependencies = [ [[package]] name = "native-tls" -version = "0.2.14" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87de3442987e9dbec73158d5c715e7ad9072fda936bb03d19d7fa10e00520f0e" +checksum = "465500e14ea162429d264d44189adc38b199b62b1c21eea9f69e4b73cb03bbf2" dependencies = [ "libc", "log", @@ -640,15 +690,15 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.21.3" +version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" [[package]] name = "openssl" -version = "0.10.75" +version = "0.10.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08838db121398ad17ab8531ce9de97b244589089e290a384c900cb9ff7434328" +checksum = "951c002c75e16ea2c65b8c7e4d3d51d5530d8dfa7d060b4776828c88cfb18ecf" dependencies = [ "bitflags", "cfg-if", @@ -672,15 +722,15 @@ dependencies = [ [[package]] name = "openssl-probe" -version = "0.1.6" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" +checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" [[package]] name = "openssl-sys" -version = "0.9.111" +version = "0.9.112" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82cab2d520aa75e3c58898289429321eb788c3106963d0dc886ec7a5f4adc321" +checksum = "57d55af3b3e226502be1526dfdba67ab0e9c96fc293004e79576b2b9edb0dbdb" dependencies = [ "cc", "libc", @@ -719,9 +769,9 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" [[package]] name = "pin-project-lite" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" [[package]] name = "pin-utils" @@ -744,6 +794,16 @@ dependencies = [ "zerovec", ] +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.106" @@ -755,18 +815,18 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.44" +version = "1.0.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" dependencies = [ "proc-macro2", ] [[package]] name = "r-efi" -version = "5.3.0" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" [[package]] name = "redox_syscall" @@ -779,9 +839,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.12.2" +version = "1.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" dependencies = [ "aho-corasick", "memchr", @@ -791,9 +851,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" dependencies = [ "aho-corasick", "memchr", @@ -802,9 +862,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.8" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" [[package]] name = "reqwest" @@ -862,9 +922,9 @@ dependencies = [ [[package]] name = "rustix" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" dependencies = [ "bitflags", "errno", @@ -875,9 +935,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.36" +version = "0.23.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c665f33d38cea657d9614f766881e4d510e0eda4239891eea56b4cadcf01801b" +checksum = "758025cb5fccfd3bc2fd74708fd4682be41d99e5dff73c377c0646c6012c73a4" dependencies = [ "once_cell", "rustls-pki-types", @@ -914,15 +974,15 @@ checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] name = "ryu" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a50f4cf475b65d88e057964e0e9bb1f0aa9bbb2036dc65c64596b42932536984" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" [[package]] name = "schannel" -version = "0.1.28" +version = "0.1.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891d81b926048e76efe18581bf793546b4c0eaf8448d72be8de2bbee5fd166e1" +checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939" dependencies = [ "windows-sys 0.61.2", ] @@ -935,12 +995,12 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "security-framework" -version = "2.11.1" +version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" dependencies = [ "bitflags", - "core-foundation", + "core-foundation 0.10.1", "core-foundation-sys", "libc", "security-framework-sys", @@ -948,14 +1008,20 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.15.0" +version = "2.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc1f0cbffaac4852523ce30d8bd3c5cdc873501d96ff467ca09b6767bb8cd5c0" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" dependencies = [ "core-foundation-sys", "libc", ] +[[package]] +name = "semver" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" + [[package]] name = "serde" version = "1.0.228" @@ -1029,9 +1095,9 @@ dependencies = [ [[package]] name = "slab" -version = "0.4.11" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] name = "smallvec" @@ -1041,12 +1107,12 @@ checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" [[package]] name = "socket2" -version = "0.6.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f4aa3ad99f2088c990dfa82d367e19cb29268ed67c574d10d0a4bfe71f07e0" +checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -1063,9 +1129,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.114" +version = "2.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" dependencies = [ "proc-macro2", "quote", @@ -1094,12 +1160,12 @@ dependencies = [ [[package]] name = "system-configuration" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +checksum = "a13f3d0daba03132c0aa9767f98351b3488edc2c100cda2d2ec2b04f3d8d3c8b" dependencies = [ "bitflags", - "core-foundation", + "core-foundation 0.9.4", "system-configuration-sys", ] @@ -1115,12 +1181,12 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.24.0" +version = "3.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "655da9c7eb6305c55742045d5a8d2037996d61d8de95806335c7c86ce0f82e9c" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.2", "once_cell", "rustix", "windows-sys 0.61.2", @@ -1158,9 +1224,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.49.0" +version = "1.50.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" +checksum = "27ad5e34374e03cfffefc301becb44e9dc3c17584f414349ebe29ed26661822d" dependencies = [ "bytes", "libc", @@ -1175,9 +1241,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.6.0" +version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" +checksum = "5c55a2eff8b69ce66c84f85e1da1c233edc36ceb85a2058d11b0d6a3c7e7569c" dependencies = [ "proc-macro2", "quote", @@ -1295,9 +1361,15 @@ checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" [[package]] name = "unicode-ident" -version = "1.0.22" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" [[package]] name = "untrusted" @@ -1359,11 +1431,20 @@ dependencies = [ "wit-bindgen", ] +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen", +] + [[package]] name = "wasm-bindgen" -version = "0.2.108" +version = "0.2.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" +checksum = "6532f9a5c1ece3798cb1c2cfdba640b9b3ba884f5db45973a6f442510a87d38e" dependencies = [ "cfg-if", "once_cell", @@ -1374,9 +1455,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.58" +version = "0.4.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70a6e77fd0ae8029c9ea0063f87c46fde723e7d887703d74ad2616d792e51e6f" +checksum = "e9c5522b3a28661442748e09d40924dfb9ca614b21c00d3fd135720e48b67db8" dependencies = [ "cfg-if", "futures-util", @@ -1388,9 +1469,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.108" +version = "0.2.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" +checksum = "18a2d50fcf105fb33bb15f00e7a77b772945a2ee45dcf454961fd843e74c18e6" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1398,9 +1479,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.108" +version = "0.2.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" +checksum = "03ce4caeaac547cdf713d280eda22a730824dd11e6b8c3ca9e42247b25c631e3" dependencies = [ "bumpalo", "proc-macro2", @@ -1411,18 +1492,52 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.108" +version = "0.2.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" +checksum = "75a326b8c223ee17883a4251907455a2431acc2791c98c26279376490c378c16" dependencies = [ "unicode-ident", ] +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + [[package]] name = "web-sys" -version = "0.3.85" +version = "0.3.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "312e32e551d92129218ea9a2452120f4aabc03529ef03e4d0d82fb2780608598" +checksum = "854ba17bb104abfb26ba36da9729addc7ce7f06f5c0f90f3c391f8461cca21f9" dependencies = [ "js-sys", "wasm-bindgen", @@ -1469,16 +1584,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" -dependencies = [ - "windows-targets 0.53.5", + "windows-targets", ] [[package]] @@ -1496,31 +1602,14 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm 0.52.6", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.53.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" -dependencies = [ - "windows-link", - "windows_aarch64_gnullvm 0.53.1", - "windows_aarch64_msvc 0.53.1", - "windows_i686_gnu 0.53.1", - "windows_i686_gnullvm 0.53.1", - "windows_i686_msvc 0.53.1", - "windows_x86_64_gnu 0.53.1", - "windows_x86_64_gnullvm 0.53.1", - "windows_x86_64_msvc 0.53.1", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] @@ -1529,60 +1618,30 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" - [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" - [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" -[[package]] -name = "windows_i686_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" - [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" - [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_i686_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" - [[package]] name = "windows_x86_64_gnu" version = "0.52.6" @@ -1590,40 +1649,104 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] -name = "windows_x86_64_gnu" -version = "0.53.1" +name = "windows_x86_64_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] -name = "windows_x86_64_gnullvm" +name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.1" +name = "wit-bindgen" +version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] [[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" +name = "wit-bindgen-core" +version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck", + "wit-parser", +] [[package]] -name = "windows_x86_64_msvc" -version = "0.53.1" +name = "wit-bindgen-rust" +version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck", + "indexmap", + "prettyplease", + "syn", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] [[package]] -name = "wit-bindgen" +name = "wit-bindgen-rust-macro" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] [[package]] name = "writeable" @@ -1716,6 +1839,6 @@ dependencies = [ [[package]] name = "zmij" -version = "1.0.17" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02aae0f83f69aafc94776e879363e9771d7ecbffe2c7fbb6c14c5e00dfe88439" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/Cargo.toml b/Cargo.toml index 4350624..5dff6fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,51 @@ readme = "README.md" keywords = ["absmartly", "ab-testing", "feature-flags", "experimentation", "sdk"] categories = ["development-tools", "web-programming"] exclude = [".github/", "target/", ".claude/"] +rust-version = "1.85" + +[lints.rust] +unsafe_code = "forbid" +missing_docs = "warn" +missing_debug_implementations = "warn" +unreachable_pub = "warn" +unused_qualifications = "warn" +unused_results = "warn" + +[lints.clippy] +correctness = { level = "deny", priority = -1 } +suspicious = { level = "deny", priority = -1 } + +style = { level = "warn", priority = -1 } +complexity = { level = "warn", priority = -1 } +perf = { level = "warn", priority = -1 } + +pedantic = { level = "warn", priority = -1 } + +module_name_repetitions = "allow" +must_use_candidate = "allow" + +unwrap_used = "warn" +expect_used = "warn" +panic = "warn" +todo = "warn" +unimplemented = "warn" +dbg_macro = "warn" +print_stdout = "warn" +print_stderr = "warn" +wildcard_imports = "warn" +enum_glob_use = "warn" +rc_buffer = "warn" +str_to_string = "warn" +inefficient_to_string = "warn" +implicit_clone = "warn" +needless_pass_by_value = "warn" +redundant_closure_for_method_calls = "warn" +semicolon_if_nothing_returned = "warn" +doc_markdown = "warn" +manual_let_else = "warn" +match_wildcard_for_single_variants = "warn" +missing_errors_doc = "warn" +missing_panics_doc = "warn" [dependencies] serde = { version = "1.0", features = ["derive"] } diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..b1248b8 --- /dev/null +++ b/clippy.toml @@ -0,0 +1,2 @@ +msrv = "1.85" +doc-valid-idents = ["ABsmartly", "GitHub", "JavaScript", "TypeScript"] diff --git a/src/assigner.rs b/src/assigner.rs index cae3df7..c3b3043 100644 --- a/src/assigner.rs +++ b/src/assigner.rs @@ -1,16 +1,22 @@ +//! Deterministic variant assignment based on unit hashing. + use crate::murmur3::murmur3_32; use crate::utils::choose_variant; +/// Assigns experiment variants deterministically based on a hashed unit identifier. +#[derive(Debug)] pub struct VariantAssigner { unit_hash: u32, } impl VariantAssigner { + /// Creates a new assigner from a unit identifier string. pub fn new(unit: &str) -> Self { let unit_hash = murmur3_32(unit.as_bytes(), 0); Self { unit_hash } } + /// Assigns a variant index based on the split weights and experiment seeds. pub fn assign(&self, split: &[f64], seed_hi: u32, seed_lo: u32) -> usize { let prob = self.probability(seed_hi, seed_lo); choose_variant(split, prob) @@ -23,7 +29,7 @@ impl VariantAssigner { buffer[8..12].copy_from_slice(&self.unit_hash.to_le_bytes()); let hash = murmur3_32(&buffer, 0); - (hash as f64) / (0xFFFFFFFFu32 as f64) + f64::from(hash) / f64::from(0xFFFF_FFFFu32) } } @@ -36,7 +42,7 @@ mod tests { fn test_assigner_bleh_email() { let hashed_unit = hash_unit("bleh@absmartly.com"); let assigner = VariantAssigner::new(&hashed_unit); - let variant = assigner.assign(&[0.5, 0.5], 0x00000000, 0x00000000); + let variant = assigner.assign(&[0.5, 0.5], 0x0000_0000, 0x0000_0000); assert_eq!(variant, 0); } @@ -44,7 +50,7 @@ mod tests { fn test_assigner_bleh_email_different_seed() { let hashed_unit = hash_unit("bleh@absmartly.com"); let assigner = VariantAssigner::new(&hashed_unit); - let variant = assigner.assign(&[0.5, 0.5], 0x00000000, 0x00000001); + let variant = assigner.assign(&[0.5, 0.5], 0x0000_0000, 0x0000_0001); assert_eq!(variant, 1); } @@ -52,15 +58,15 @@ mod tests { fn test_assigner_123456789() { let hashed_unit = hash_unit("123456789"); let assigner = VariantAssigner::new(&hashed_unit); - assert_eq!(assigner.assign(&[0.5, 0.5], 0x00000000, 0x00000000), 1); - assert_eq!(assigner.assign(&[0.5, 0.5], 0x00000000, 0x00000001), 0); + assert_eq!(assigner.assign(&[0.5, 0.5], 0x0000_0000, 0x0000_0000), 1); + assert_eq!(assigner.assign(&[0.5, 0.5], 0x0000_0000, 0x0000_0001), 0); } #[test] fn test_assigner_three_way_split() { let hashed_unit = hash_unit("bleh@absmartly.com"); let assigner = VariantAssigner::new(&hashed_unit); - let variant = assigner.assign(&[0.33, 0.33, 0.34], 0x00000000, 0x00000001); + let variant = assigner.assign(&[0.33, 0.33, 0.34], 0x0000_0000, 0x0000_0001); assert_eq!(variant, 2); } } diff --git a/src/context.rs b/src/context.rs index a7abce8..237156f 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,12 +1,18 @@ +//! Experiment context for managing assignments, exposures, and goal tracking. + use serde_json::Value; use std::collections::HashMap; use std::time::{SystemTime, UNIX_EPOCH}; use crate::assigner::VariantAssigner; use crate::matcher::AudienceMatcher; -use crate::models::*; +use crate::models::{ + Assignment, Attribute, ContextData, ContextState, ExperimentData, Exposure, Goal, + PublishParams, Unit, +}; use crate::utils::{array_equals_shallow, hash_unit}; +/// Callback type for logging context events. pub type EventLogger = Box) + Send + Sync>; struct Experiment { @@ -14,6 +20,23 @@ struct Experiment { variables: Vec>, } +impl std::fmt::Debug for Experiment { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Experiment") + .field("data", &self.data) + .field("variables", &self.variables) + .finish() + } +} + +/// The main context for interacting with ABsmartly experiments. +/// +/// A context holds unit assignments, tracks exposures and goals, and resolves +/// experiment variables. Create one via [`SDK::create_context`] or +/// [`SDK::create_context_with`]. +/// +/// [`SDK::create_context`]: crate::sdk::SDK::create_context +/// [`SDK::create_context_with`]: crate::sdk::SDK::create_context_with pub struct Context { units: HashMap, attrs: Vec, @@ -34,7 +57,19 @@ pub struct Context { event_logger: Option, } +impl std::fmt::Debug for Context { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Context") + .field("units", &self.units) + .field("state", &self.state) + .field("pending", &self.pending) + .field("data", &self.data) + .finish_non_exhaustive() + } +} + impl Context { + /// Creates a new context initialized with the given experiment data. pub fn new(data: ContextData) -> Self { let mut ctx = Self { units: HashMap::new(), @@ -60,6 +95,7 @@ impl Context { ctx } + /// Sets an event logger callback for observing context events. pub fn set_event_logger(&mut self, logger: EventLogger) { self.event_logger = Some(logger); } @@ -95,7 +131,7 @@ impl Context { variables.push(parsed); } - self.index.insert( + let _ = self.index.insert( experiment.name.clone(), Experiment { data: experiment.clone(), @@ -105,53 +141,75 @@ impl Context { } } + /// Returns true if the context is ready for use. pub fn is_ready(&self) -> bool { self.state == ContextState::Ready } + /// Returns true if the context failed to initialize. pub fn is_failed(&self) -> bool { self.state == ContextState::Failed } + /// Returns true if the context has been finalized. pub fn is_finalized(&self) -> bool { self.state == ContextState::Finalized } + /// Returns true if the context is currently finalizing. pub fn is_finalizing(&self) -> bool { self.state == ContextState::Finalizing } + /// Returns the number of pending events waiting to be published. pub fn pending(&self) -> usize { self.pending } + fn check_not_finalized(&self) -> Result<(), String> { + if self.is_finalized() { + return Err("ABSmartly Context is finalized.".to_owned()); + } + if self.is_finalizing() { + return Err("ABSmartly Context is finalizing.".to_owned()); + } + Ok(()) + } + + /// Returns a reference to the context's experiment data. pub fn data(&self) -> &ContextData { &self.data } + /// Sets a unit identifier for the given unit type. + /// + /// # Errors + /// + /// Returns an error if the context is finalized/finalizing, the UID is blank, + /// or a different UID is already set for this unit type. pub fn set_unit(&mut self, unit_type: &str, uid: &str) -> Result<(), String> { - if self.is_finalized() { - return Err("ABSmartly Context is finalized.".to_string()); - } - if self.is_finalizing() { - return Err("ABSmartly Context is finalizing.".to_string()); - } + self.check_not_finalized()?; let uid = uid.trim(); if uid.is_empty() { - return Err(format!("Unit '{}' UID must not be blank.", unit_type)); + return Err(format!("Unit '{unit_type}' UID must not be blank.")); } if let Some(existing) = self.units.get(unit_type) { if existing != uid { - return Err(format!("Unit '{}' UID already set.", unit_type)); + return Err(format!("Unit '{unit_type}' UID already set.")); } } - self.units.insert(unit_type.to_string(), uid.to_string()); + let _ = self.units.insert(unit_type.to_owned(), uid.to_owned()); Ok(()) } + /// Sets multiple unit identifiers at once. + /// + /// # Errors + /// + /// Returns an error if any individual `set_unit` call fails. pub fn set_units(&mut self, units: I) -> Result<(), String> where I: IntoIterator, @@ -164,24 +222,26 @@ impl Context { Ok(()) } + /// Returns the UID for the given unit type, if set. pub fn get_unit(&self, unit_type: &str) -> Option<&String> { self.units.get(unit_type) } + /// Returns all unit type to UID mappings. pub fn get_units(&self) -> &HashMap { &self.units } + /// Sets a context attribute. + /// + /// # Errors + /// + /// Returns an error if the context is finalized or finalizing. pub fn set_attribute(&mut self, name: &str, value: impl Into) -> Result<(), String> { - if self.is_finalized() { - return Err("ABSmartly Context is finalized.".to_string()); - } - if self.is_finalizing() { - return Err("ABSmartly Context is finalizing.".to_string()); - } + self.check_not_finalized()?; self.attrs.push(Attribute { - name: name.to_string(), + name: name.to_owned(), value: value.into(), set_at: now_millis(), }); @@ -189,18 +249,18 @@ impl Context { Ok(()) } + /// Sets multiple context attributes at once. + /// + /// # Errors + /// + /// Returns an error if the context is finalized or finalizing. pub fn set_attributes(&mut self, attrs: I) -> Result<(), String> where I: IntoIterator, K: Into, V: Into, { - if self.is_finalized() { - return Err("ABSmartly Context is finalized.".to_string()); - } - if self.is_finalizing() { - return Err("ABSmartly Context is finalizing.".to_string()); - } + self.check_not_finalized()?; let set_at = now_millis(); for (name, value) in attrs { @@ -214,6 +274,7 @@ impl Context { Ok(()) } + /// Returns the most recently set value for the given attribute name. pub fn get_attribute(&self, name: &str) -> Option<&Value> { self.attrs .iter() @@ -222,40 +283,53 @@ impl Context { .map(|a| &a.value) } + /// Returns all attributes as a map (last-write-wins for duplicate names). pub fn get_attributes(&self) -> HashMap { let mut attrs = HashMap::new(); for attr in &self.attrs { - attrs.insert(attr.name.clone(), attr.value.clone()); + let _ = attrs.insert(attr.name.clone(), attr.value.clone()); } attrs } + /// Sets an override for the given experiment, forcing a specific variant. pub fn set_override(&mut self, experiment_name: &str, variant: i32) { - self.overrides.insert(experiment_name.to_string(), variant); + let _ = self.overrides.insert(experiment_name.to_owned(), variant); } + /// Sets multiple experiment overrides at once. pub fn set_overrides(&mut self, overrides: I) where I: IntoIterator, K: Into, { for (experiment_name, variant) in overrides { - self.overrides.insert(experiment_name.into(), variant); + let _ = self.overrides.insert(experiment_name.into(), variant); } } - pub fn set_custom_assignment(&mut self, experiment_name: &str, variant: i32) -> Result<(), String> { - if self.is_finalized() { - return Err("ABSmartly Context is finalized.".to_string()); - } - if self.is_finalizing() { - return Err("ABSmartly Context is finalizing.".to_string()); - } - self.cassignments - .insert(experiment_name.to_string(), variant); + /// Sets a custom assignment for the given experiment. + /// + /// # Errors + /// + /// Returns an error if the context is finalized or finalizing. + pub fn set_custom_assignment( + &mut self, + experiment_name: &str, + variant: i32, + ) -> Result<(), String> { + self.check_not_finalized()?; + let _ = self + .cassignments + .insert(experiment_name.to_owned(), variant); Ok(()) } + /// Sets multiple custom assignments at once. + /// + /// # Errors + /// + /// Returns an error if any individual assignment fails. pub fn set_custom_assignments(&mut self, assignments: I) -> Result<(), String> where I: IntoIterator, @@ -267,10 +341,12 @@ impl Context { Ok(()) } + /// Returns the assigned variant for an experiment without recording an exposure. pub fn peek(&mut self, experiment_name: &str) -> i32 { self.assign(experiment_name).variant } + /// Returns the assigned variant for an experiment and records an exposure. pub fn treatment(&mut self, experiment_name: &str) -> i32 { let assignment = self.assign(experiment_name); let variant = assignment.variant; @@ -285,33 +361,37 @@ impl Context { variant } + /// Records a goal achievement event. + /// + /// # Errors + /// + /// Returns an error if the context is finalized or finalizing. pub fn track(&mut self, goal_name: &str, properties: impl Into) -> Result<(), String> { - if self.is_finalized() { - return Err("ABSmartly Context is finalized.".to_string()); - } - if self.is_finalizing() { - return Err("ABSmartly Context is finalizing.".to_string()); - } + self.check_not_finalized()?; let properties_map: Option> = match properties.into() { Value::Object(map) => Some(map.into_iter().collect()), - Value::Null => None, _ => None, }; let goal = Goal { - name: goal_name.to_string(), + name: goal_name.to_owned(), properties: properties_map, achieved_at: now_millis(), }; - self.log_event("goal", Some(serde_json::to_value(&goal).unwrap_or_default())); + self.log_event( + "goal", + Some(serde_json::to_value(&goal).unwrap_or_default()), + ); self.goals.push(goal); self.pending += 1; Ok(()) } + /// Returns the variable value for the given key, falling back to the default. + /// Records an exposure for the experiment that provides the variable. pub fn variable_value(&mut self, key: &str, default_value: impl Into) -> Value { if let Some(experiment_names) = self.index_variables.get(key).cloned() { for exp_name in experiment_names { @@ -335,6 +415,7 @@ impl Context { default_value.into() } + /// Returns the variable value for the given key without recording an exposure. pub fn peek_variable_value(&mut self, key: &str, default_value: impl Into) -> Value { if let Some(experiment_names) = self.index_variables.get(key).cloned() { for exp_name in experiment_names { @@ -351,89 +432,100 @@ impl Context { default_value.into() } + /// Returns a map of variable keys to the experiment names that provide them. pub fn variable_keys(&self) -> HashMap> { - let mut result = HashMap::new(); - for (key, exp_names) in &self.index_variables { - result.insert(key.clone(), exp_names.clone()); - } - result + self.index_variables.clone() } + /// Returns a custom field value for the given experiment and field name. pub fn custom_field_value(&self, experiment_name: &str, field_name: &str) -> Option { - if let Some(exp) = self.index.get(experiment_name) { - if let Some(ref custom_fields) = exp.data.custom_field_values { - if let Some(field) = custom_fields.iter().find(|f| f.name == field_name) { - return match field.field_type.as_str() { - "text" | "string" => Some(Value::String(field.value.clone())), - "number" => field.value.parse::().ok().map(|n| { - serde_json::Number::from_f64(n) - .map(Value::Number) - .unwrap_or(Value::Null) - }), - "json" => { - if field.value == "null" { - Some(Value::Null) - } else if field.value.is_empty() { - Some(Value::String(String::new())) - } else { - serde_json::from_str(&field.value).ok() - } - } - "boolean" => Some(Value::Bool(field.value == "true")), - _ => None, - }; + let exp = self.index.get(experiment_name)?; + let custom_fields = exp.data.custom_field_values.as_ref()?; + let field = custom_fields.iter().find(|f| f.name == field_name)?; + + match field.field_type.as_str() { + "text" | "string" => Some(Value::String(field.value.clone())), + "number" => field + .value + .parse::() + .ok() + .map(|n| serde_json::Number::from_f64(n).map_or(Value::Null, Value::Number)), + "json" => { + if field.value == "null" { + Some(Value::Null) + } else if field.value.is_empty() { + Some(Value::String(String::new())) + } else { + serde_json::from_str(&field.value).ok() } } + "boolean" => Some(Value::Bool(field.value == "true")), + _ => None, } - None } - pub fn custom_field_value_type(&self, experiment_name: &str, field_name: &str) -> Option { - if let Some(exp) = self.index.get(experiment_name) { - if let Some(ref custom_fields) = exp.data.custom_field_values { - if let Some(field) = custom_fields.iter().find(|f| f.name == field_name) { - return Some(field.field_type.clone()); - } - } - } - None + /// Returns the type of a custom field for the given experiment and field name. + pub fn custom_field_value_type( + &self, + experiment_name: &str, + field_name: &str, + ) -> Option { + let exp = self.index.get(experiment_name)?; + let custom_fields = exp.data.custom_field_values.as_ref()?; + let field = custom_fields.iter().find(|f| f.name == field_name)?; + Some(field.field_type.clone()) } + /// Returns all unique custom field names across all experiments. pub fn custom_field_keys(&self) -> Vec { let mut keys = std::collections::HashSet::new(); for exp in &self.data.experiments { if let Some(ref custom_fields) = exp.custom_field_values { for field in custom_fields { - keys.insert(field.name.clone()); + let _ = keys.insert(field.name.clone()); } } } keys.into_iter().collect() } + /// Returns the names of all experiments in the context. pub fn experiments(&self) -> Vec { - self.data.experiments.iter().map(|e| e.name.clone()).collect() + self.data + .experiments + .iter() + .map(|e| e.name.clone()) + .collect() } + /// Refreshes the context with new experiment data, clearing all cached assignments. pub fn refresh(&mut self, new_data: ContextData) { self.assignments.clear(); self.init(new_data); - self.log_event("refresh", Some(serde_json::to_value(&self.data).unwrap_or_default())); + self.log_event( + "refresh", + Some(serde_json::to_value(&self.data).unwrap_or_default()), + ); } + /// Publishes all pending events (exposures, goals, attributes). pub fn publish(&mut self) { if self.pending == 0 { return; } let params = self.build_publish_params(); - self.log_event("publish", Some(serde_json::to_value(¶ms).unwrap_or_default())); + self.log_event( + "publish", + Some(serde_json::to_value(¶ms).unwrap_or_default()), + ); self.pending = 0; self.exposures.clear(); self.goals.clear(); } + /// Finalizes the context, publishing any pending events and preventing further mutations. pub fn finalize(&mut self) { if self.is_finalized() { return; @@ -449,6 +541,7 @@ impl Context { self.log_event("finalize", None); } + #[allow(clippy::too_many_lines)] fn assign(&mut self, experiment_name: &str) -> Assignment { let has_custom = self.cassignments.contains_key(experiment_name); let has_override = self.overrides.contains_key(experiment_name); @@ -465,7 +558,7 @@ impl Context { } } else if !has_custom || self.cassignments[experiment_name] == cached.variant { if let Some(exp) = self.index.get(experiment_name) { - if self.experiment_matches(&exp.data, cached) + if Self::experiment_matches(&exp.data, cached) && self.audience_matches(&exp.data, cached) { return cached.clone(); @@ -484,7 +577,7 @@ impl Context { if has_override { if let Some(ref exp_data) = exp_data_opt { assignment.id = exp_data.id; - assignment.unit_type = exp_data.unit_type.clone(); + assignment.unit_type.clone_from(&exp_data.unit_type); } assignment.overridden = true; @@ -525,11 +618,16 @@ impl Context { assignment.variant = self.cassignments[experiment_name]; assignment.custom = true; } else { - assignment.variant = assigner.assign( + #[allow( + clippy::cast_possible_truncation, + clippy::cast_possible_wrap + )] + let v = assigner.assign( &exp_data.split, exp_data.seed_hi, exp_data.seed_lo, ) as i32; + assignment.variant = v; } } else { assignment.variant = 0; @@ -540,11 +638,13 @@ impl Context { } else { assignment.assigned = true; assignment.eligible = true; - assignment.variant = exp_data.full_on_variant as i32; + #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] + let v = exp_data.full_on_variant as i32; + assignment.variant = v; assignment.full_on = true; } - assignment.unit_type = exp_data.unit_type.clone(); + assignment.unit_type.clone_from(&exp_data.unit_type); assignment.id = exp_data.id; assignment.iteration = exp_data.iteration; assignment.traffic_split = Some(exp_data.traffic_split.clone()); @@ -553,17 +653,20 @@ impl Context { } if let Some(exp) = self.index.get(experiment_name) { - if (assignment.variant as usize) < exp.variables.len() { - assignment.variables = Some(exp.variables[assignment.variant as usize].clone()); + #[allow(clippy::cast_sign_loss)] + let variant_idx = assignment.variant as usize; + if variant_idx < exp.variables.len() { + assignment.variables = Some(exp.variables[variant_idx].clone()); } } - self.assignments - .insert(experiment_name.to_string(), assignment.clone()); + let _ = self + .assignments + .insert(experiment_name.to_owned(), assignment.clone()); assignment } - fn experiment_matches(&self, experiment: &ExperimentData, assignment: &Assignment) -> bool { + fn experiment_matches(experiment: &ExperimentData, assignment: &Assignment) -> bool { experiment.id == assignment.id && experiment.unit_type == assignment.unit_type && experiment.iteration == assignment.iteration @@ -571,7 +674,7 @@ impl Context { && assignment .traffic_split .as_ref() - .map_or(false, |ts| array_equals_shallow(&experiment.traffic_split, ts)) + .is_some_and(|ts| array_equals_shallow(&experiment.traffic_split, ts)) } fn audience_matches(&self, experiment: &ExperimentData, assignment: &Assignment) -> bool { @@ -579,7 +682,7 @@ impl Context { let attrs = self.get_attributes(); let result = self.audience_matcher.evaluate(&experiment.audience, &attrs); if let Some(matched) = result { - return matched == !assignment.audience_mismatch; + return matched != assignment.audience_mismatch; } } true @@ -589,7 +692,7 @@ impl Context { if let Some(assignment) = self.assignments.get(experiment_name) { let exposure = Exposure { id: assignment.id, - name: experiment_name.to_string(), + name: experiment_name.to_owned(), exposed_at: now_millis(), unit: assignment.unit_type.clone(), variant: assignment.variant, @@ -601,7 +704,10 @@ impl Context { audience_mismatch: assignment.audience_mismatch, }; - self.log_event("exposure", Some(serde_json::to_value(&exposure).unwrap_or_default())); + self.log_event( + "exposure", + Some(serde_json::to_value(&exposure).unwrap_or_default()), + ); self.exposures.push(exposure); self.pending += 1; } @@ -614,7 +720,7 @@ impl Context { if let Some(unit) = self.units.get(unit_type) { let hash = hash_unit(unit); - self.hashes.insert(unit_type.to_string(), hash.clone()); + let _ = self.hashes.insert(unit_type.to_owned(), hash.clone()); return Some(hash); } @@ -624,8 +730,8 @@ impl Context { fn build_publish_params(&self) -> PublishParams { let units: Vec = self .units - .iter() - .map(|(unit_type, _)| Unit { + .keys() + .map(|unit_type| Unit { unit_type: unit_type.clone(), uid: self.hashes.get(unit_type).cloned(), }) @@ -660,6 +766,7 @@ impl Context { } } +#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] fn now_millis() -> i64 { SystemTime::now() .duration_since(UNIX_EPOCH) @@ -670,6 +777,7 @@ fn now_millis() -> i64 { #[cfg(test)] mod tests { use super::*; + use crate::models::Variant; use serde_json::json; fn make_experiment(name: &str, variants: Vec<&str>, split: Vec) -> ExperimentData { @@ -690,7 +798,11 @@ mod tests { variants: variants .iter() .map(|c| Variant { - config: if c.is_empty() { None } else { Some(c.to_string()) }, + config: if c.is_empty() { + None + } else { + Some((*c).to_string()) + }, }) .collect(), variables: HashMap::new(), @@ -774,7 +886,11 @@ mod tests { #[test] fn test_context_treatment_with_experiment() { - let exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); + let exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.5, 0.5], + ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); @@ -785,7 +901,11 @@ mod tests { #[test] fn test_context_peek_does_not_queue_exposure() { - let exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); + let exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.5, 0.5], + ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); @@ -796,7 +916,11 @@ mod tests { #[test] fn test_context_treatment_queues_exposure() { - let exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); + let exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.5, 0.5], + ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); @@ -807,7 +931,11 @@ mod tests { #[test] fn test_context_treatment_only_queues_once() { - let exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); + let exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.5, 0.5], + ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); @@ -820,7 +948,11 @@ mod tests { #[test] fn test_context_set_override() { - let exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); + let exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.5, 0.5], + ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); @@ -832,7 +964,11 @@ mod tests { #[test] fn test_context_set_custom_assignment() { - let exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); + let exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.5, 0.5], + ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); @@ -864,7 +1000,11 @@ mod tests { #[test] fn test_context_publish_clears_pending() { - let exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); + let exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.5, 0.5], + ); let data = make_context_data(vec![exp]); let mut context = Context::new(data); @@ -945,7 +1085,11 @@ mod tests { #[test] fn test_context_full_on_variant() { - let mut exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.5, 0.5]); + let mut exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.5, 0.5], + ); exp.full_on_variant = 1; let data = make_context_data(vec![exp]); let mut context = Context::new(data); @@ -956,7 +1100,11 @@ mod tests { #[test] fn test_context_audience_mismatch_strict() { - let mut exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0]); + let mut exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.0, 1.0], + ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = true; let data = make_context_data(vec![exp]); @@ -970,7 +1118,11 @@ mod tests { #[test] fn test_context_audience_match() { - let mut exp = make_experiment("test_exp", vec!["{}", r#"{"button":"red"}"#], vec![0.0, 1.0]); + let mut exp = make_experiment( + "test_exp", + vec!["{}", r#"{"button":"red"}"#], + vec![0.0, 1.0], + ); exp.audience = r#"{"filter":[{"eq":[{"var":"country"},{"value":"US"}]}]}"#.to_string(); exp.audience_strict = true; let data = make_context_data(vec![exp]); @@ -1046,7 +1198,7 @@ mod tests { let data = make_context_data(vec![]); let mut context = Context::new(data); - let attrs = std::collections::HashMap::from([ + let attrs = HashMap::from([ ("country".to_string(), json!("UK")), ("tier".to_string(), json!("gold")), ]); @@ -1087,10 +1239,15 @@ mod tests { let data = make_context_data(vec![]); let mut context = Context::new(data); - assert!(context.track("purchase", json!({ - "item_count": 1, - "total_amount": 99.99 - })).is_ok()); + assert!(context + .track( + "purchase", + json!({ + "item_count": 1, + "total_amount": 99.99 + }) + ) + .is_ok()); assert_eq!(context.pending(), 1); } diff --git a/src/jsonexpr/evaluator.rs b/src/jsonexpr/evaluator.rs index 4fb822a..82d7b3d 100644 --- a/src/jsonexpr/evaluator.rs +++ b/src/jsonexpr/evaluator.rs @@ -1,46 +1,53 @@ +//! Core expression evaluator with type coercion and comparison logic. + use serde_json::Value; use std::collections::HashMap; use super::operators; +/// Evaluates JSON expressions against a set of variables. +#[derive(Debug)] pub struct Evaluator { vars: HashMap, } impl Evaluator { + /// Creates a new evaluator with the given variable bindings. pub fn new(vars: HashMap) -> Self { Self { vars } } + /// Evaluates a JSON expression, dispatching to the appropriate operator. pub fn evaluate(&self, expr: &Value) -> Value { match expr { Value::Array(arr) => operators::and_op(self, &Value::Array(arr.clone())), Value::Object(map) => { - for (key, value) in map.iter() { + if let Some((key, value)) = map.into_iter().next() { match key.as_str() { - "and" => return operators::and_op(self, value), - "or" => return operators::or_op(self, value), - "value" => return operators::value_op(self, value), - "var" => return operators::var_op(self, value), - "null" => return operators::null_op(self, value), - "not" => return operators::not_op(self, value), - "in" => return operators::in_op(self, value), - "match" => return operators::match_op(self, value), - "eq" => return operators::eq_op(self, value), - "gt" => return operators::gt_op(self, value), - "gte" => return operators::gte_op(self, value), - "lt" => return operators::lt_op(self, value), - "lte" => return operators::lte_op(self, value), - _ => {} + "and" => operators::and_op(self, value), + "or" => operators::or_op(self, value), + "value" => operators::value_op(self, value), + "var" => operators::var_op(self, value), + "null" => operators::null_op(self, value), + "not" => operators::not_op(self, value), + "in" => operators::in_op(self, value), + "match" => operators::match_op(self, value), + "eq" => operators::eq_op(self, value), + "gt" => operators::gt_op(self, value), + "gte" => operators::gte_op(self, value), + "lt" => operators::lt_op(self, value), + "lte" => operators::lte_op(self, value), + _ => Value::Null, } - break; + } else { + Value::Null } - Value::Null } _ => Value::Null, } } + /// Converts a JSON value to a boolean using ABsmartly's coercion rules. pub fn boolean_convert(&self, x: &Value) -> bool { match x { Value::Bool(b) => *b, @@ -55,11 +62,11 @@ impl Evaluator { } Value::String(s) => s != "false" && s != "0" && !s.is_empty(), Value::Null => false, - Value::Array(_) => true, - Value::Object(_) => true, + Value::Array(_) | Value::Object(_) => true, } } + /// Converts a JSON value to a number, returning `None` if conversion is not possible. pub fn number_convert(&self, x: &Value) -> Option { match x { Value::Number(n) => n.as_f64(), @@ -69,15 +76,16 @@ impl Evaluator { } } + /// Converts a JSON value to a string, returning `None` if conversion is not possible. pub fn string_convert(&self, x: &Value) -> Option { match x { Value::String(s) => Some(s.clone()), Value::Bool(b) => Some(b.to_string()), Value::Number(n) => { if let Some(f) = n.as_f64() { - let formatted = format!("{:.15}", f); + let formatted = format!("{f:.15}"); let trimmed = formatted.trim_end_matches('0').trim_end_matches('.'); - Some(trimmed.to_string()) + Some(trimmed.to_owned()) } else { None } @@ -86,6 +94,7 @@ impl Evaluator { } } + /// Extracts a variable value from the evaluator's variable map using a slash-separated path. pub fn extract_var(&self, path: &str) -> Value { let frags: Vec<&str> = path.split('/').collect(); let mut target: &Value = &Value::Object( @@ -122,6 +131,7 @@ impl Evaluator { target.clone() } + /// Compares two JSON values, returning -1, 0, or 1 for ordering, or `None` if incomparable. pub fn compare(&self, lhs: &Value, rhs: &Value) -> Option { if lhs.is_null() { return if rhs.is_null() { Some(0) } else { None }; @@ -170,12 +180,13 @@ impl Evaluator { } } +/// Performs a deep equality comparison of two JSON values. pub fn values_equal_deep(a: &Value, b: &Value) -> bool { match (a, b) { (Value::Null, Value::Null) => true, (Value::Bool(ab), Value::Bool(bb)) => ab == bb, (Value::Number(an), Value::Number(bn)) => { - an.as_f64().zip(bn.as_f64()).map_or(false, |(a, b)| { + an.as_f64().zip(bn.as_f64()).is_some_and(|(a, b)| { if a.is_nan() && b.is_nan() { true } else { @@ -183,13 +194,19 @@ pub fn values_equal_deep(a: &Value, b: &Value) -> bool { } }) } - (Value::String(as_), Value::String(bs)) => as_ == bs, + (Value::String(a_str), Value::String(b_str)) => a_str == b_str, (Value::Array(aa), Value::Array(ba)) => { - aa.len() == ba.len() && aa.iter().zip(ba.iter()).all(|(x, y)| values_equal_deep(x, y)) + aa.len() == ba.len() + && aa + .iter() + .zip(ba.iter()) + .all(|(x, y)| values_equal_deep(x, y)) } (Value::Object(ao), Value::Object(bo)) => { ao.len() == bo.len() - && ao.iter().all(|(k, v)| bo.get(k).map_or(false, |bv| values_equal_deep(v, bv))) + && ao + .iter() + .all(|(k, v)| bo.get(k).is_some_and(|bv| values_equal_deep(v, bv))) } _ => false, } @@ -265,8 +282,14 @@ mod tests { assert_eq!(evaluator.number_convert(&json!(1.5)), Some(1.5)); assert_eq!(evaluator.number_convert(&json!(2.0)), Some(2.0)); assert_eq!(evaluator.number_convert(&json!(3.0)), Some(3.0)); - assert_eq!(evaluator.number_convert(&json!(2147483647)), Some(2147483647.0)); - assert_eq!(evaluator.number_convert(&json!(-2147483647)), Some(-2147483647.0)); + assert_eq!( + evaluator.number_convert(&json!(2_147_483_647)), + Some(2_147_483_647.0) + ); + assert_eq!( + evaluator.number_convert(&json!(-2_147_483_647)), + Some(-2_147_483_647.0) + ); } #[test] @@ -291,27 +314,45 @@ mod tests { #[test] fn test_string_convert_booleans() { let evaluator = make_evaluator(); - assert_eq!(evaluator.string_convert(&json!(true)), Some("true".to_string())); - assert_eq!(evaluator.string_convert(&json!(false)), Some("false".to_string())); + assert_eq!( + evaluator.string_convert(&json!(true)), + Some("true".to_string()) + ); + assert_eq!( + evaluator.string_convert(&json!(false)), + Some("false".to_string()) + ); } #[test] fn test_string_convert_strings() { let evaluator = make_evaluator(); - assert_eq!(evaluator.string_convert(&json!("")), Some("".to_string())); - assert_eq!(evaluator.string_convert(&json!("abc")), Some("abc".to_string())); + assert_eq!(evaluator.string_convert(&json!("")), Some(String::new())); + assert_eq!( + evaluator.string_convert(&json!("abc")), + Some("abc".to_string()) + ); } #[test] fn test_string_convert_numbers() { let evaluator = make_evaluator(); - assert_eq!(evaluator.string_convert(&json!(-1.0)), Some("-1".to_string())); + assert_eq!( + evaluator.string_convert(&json!(-1.0)), + Some("-1".to_string()) + ); assert_eq!(evaluator.string_convert(&json!(0.0)), Some("0".to_string())); assert_eq!(evaluator.string_convert(&json!(1.0)), Some("1".to_string())); assert_eq!(evaluator.string_convert(&json!(2.0)), Some("2".to_string())); assert_eq!(evaluator.string_convert(&json!(3.0)), Some("3".to_string())); - assert_eq!(evaluator.string_convert(&json!(2147483647.0)), Some("2147483647".to_string())); - assert_eq!(evaluator.string_convert(&json!(-2147483647.0)), Some("-2147483647".to_string())); + assert_eq!( + evaluator.string_convert(&json!(2_147_483_647.0)), + Some("2147483647".to_string()) + ); + assert_eq!( + evaluator.string_convert(&json!(-2_147_483_647.0)), + Some("-2147483647".to_string()) + ); } #[test] @@ -395,7 +436,10 @@ mod tests { fn test_compare_objects() { let evaluator = make_evaluator(); assert_eq!(evaluator.compare(&json!({}), &json!({})), Some(0)); - assert_eq!(evaluator.compare(&json!({"a": 1}), &json!({"a": 1})), Some(0)); + assert_eq!( + evaluator.compare(&json!({"a": 1}), &json!({"a": 1})), + Some(0) + ); assert_eq!(evaluator.compare(&json!({"a": 1}), &json!({"b": 2})), None); assert_eq!(evaluator.compare(&json!({}), &json!([])), None); } @@ -501,8 +545,14 @@ mod tests { #[test] fn test_values_equal_deep_objects() { - assert!(values_equal_deep(&json!({"a": 1, "b": 2}), &json!({"a": 1, "b": 2}))); - assert!(values_equal_deep(&json!({"a": 1, "b": 2}), &json!({"b": 2, "a": 1}))); + assert!(values_equal_deep( + &json!({"a": 1, "b": 2}), + &json!({"a": 1, "b": 2}) + )); + assert!(values_equal_deep( + &json!({"a": 1, "b": 2}), + &json!({"b": 2, "a": 1}) + )); assert!(!values_equal_deep(&json!({"a": 1}), &json!({"b": 2}))); assert!(!values_equal_deep(&json!({}), &json!({"a": 1}))); assert!(!values_equal_deep(&json!({"a": 1}), &json!({}))); diff --git a/src/jsonexpr/mod.rs b/src/jsonexpr/mod.rs index e7948da..4953c60 100644 --- a/src/jsonexpr/mod.rs +++ b/src/jsonexpr/mod.rs @@ -1,3 +1,5 @@ +//! JSON expression evaluator for audience filter matching. + pub mod evaluator; pub mod operators; @@ -5,19 +7,28 @@ use evaluator::Evaluator; use serde_json::Value; use std::collections::HashMap; +/// Entry point for evaluating JSON-based filter expressions. +#[derive(Debug)] pub struct JsonExpr; impl JsonExpr { + /// Creates a new JSON expression evaluator. pub fn new() -> Self { Self } - pub fn evaluate_boolean_expr(&self, expr: &Value, vars: &HashMap) -> Option { + /// Evaluates a JSON expression as a boolean in the context of the given variables. + pub fn evaluate_boolean_expr( + &self, + expr: &Value, + vars: &HashMap, + ) -> Option { let evaluator = Evaluator::new(vars.clone()); let result = evaluator.evaluate(expr); Some(evaluator.boolean_convert(&result)) } + /// Evaluates a JSON expression and returns the resulting value. pub fn evaluate_expr(&self, expr: &Value, vars: &HashMap) -> Value { let evaluator = Evaluator::new(vars.clone()); evaluator.evaluate(expr) diff --git a/src/jsonexpr/operators/mod.rs b/src/jsonexpr/operators/mod.rs index e38a67f..6bde250 100644 --- a/src/jsonexpr/operators/mod.rs +++ b/src/jsonexpr/operators/mod.rs @@ -1,8 +1,11 @@ +//! JSON expression operator implementations. + use regex::Regex; use serde_json::Value; use super::evaluator::{values_equal_deep, Evaluator}; +/// Logical AND: returns true if all sub-expressions are truthy. pub fn and_op(evaluator: &Evaluator, args: &Value) -> Value { if let Value::Array(exprs) = args { for expr in exprs { @@ -17,6 +20,7 @@ pub fn and_op(evaluator: &Evaluator, args: &Value) -> Value { } } +/// Logical OR: returns true if any sub-expression is truthy. pub fn or_op(evaluator: &Evaluator, args: &Value) -> Value { if let Value::Array(exprs) = args { for expr in exprs { @@ -31,10 +35,12 @@ pub fn or_op(evaluator: &Evaluator, args: &Value) -> Value { } } +/// Returns the argument value as-is. pub fn value_op(_evaluator: &Evaluator, args: &Value) -> Value { args.clone() } +/// Resolves a variable reference by path. pub fn var_op(evaluator: &Evaluator, args: &Value) -> Value { if let Value::String(path) = args { evaluator.extract_var(path) @@ -43,16 +49,19 @@ pub fn var_op(evaluator: &Evaluator, args: &Value) -> Value { } } +/// Returns true if the evaluated argument is null. pub fn null_op(evaluator: &Evaluator, args: &Value) -> Value { let result = evaluator.evaluate(args); Value::Bool(result.is_null()) } +/// Logical NOT: returns the boolean negation of the evaluated argument. pub fn not_op(evaluator: &Evaluator, args: &Value) -> Value { let result = evaluator.evaluate(args); Value::Bool(!evaluator.boolean_convert(&result)) } +/// Membership test: checks if a value is contained in a string, array, or object. pub fn in_op(evaluator: &Evaluator, args: &Value) -> Value { if let Value::Array(arr) = args { if arr.len() != 2 { @@ -67,7 +76,7 @@ pub fn in_op(evaluator: &Evaluator, args: &Value) -> Value { (_, Value::Array(h_arr)) => { Value::Bool(h_arr.iter().any(|item| values_equal_deep(&needle, item))) } - (Value::String(n), Value::Object(h_map)) => Value::Bool(h_map.contains_key(n)), + (Value::String(n), Value::Object(h_map)) => Value::Bool(h_map.contains_key(n.as_str())), _ => Value::Bool(false), } } else { @@ -75,6 +84,7 @@ pub fn in_op(evaluator: &Evaluator, args: &Value) -> Value { } } +/// Regex match: checks if a string matches a regular expression pattern. pub fn match_op(evaluator: &Evaluator, args: &Value) -> Value { if let Value::Array(arr) = args { if arr.len() != 2 { @@ -98,71 +108,32 @@ pub fn match_op(evaluator: &Evaluator, args: &Value) -> Value { } } +/// Equality comparison. pub fn eq_op(evaluator: &Evaluator, args: &Value) -> Value { - if let Value::Array(arr) = args { - if arr.len() != 2 { - return Value::Bool(false); - } - - let lhs = evaluator.evaluate(&arr[0]); - let rhs = evaluator.evaluate(&arr[1]); - let result = evaluator.compare(&lhs, &rhs); - - Value::Bool(result == Some(0)) - } else { - Value::Bool(false) - } + compare_op(evaluator, args, |r| r == 0) } +/// Greater-than comparison. pub fn gt_op(evaluator: &Evaluator, args: &Value) -> Value { - if let Value::Array(arr) = args { - if arr.len() != 2 { - return Value::Bool(false); - } - - let lhs = evaluator.evaluate(&arr[0]); - let rhs = evaluator.evaluate(&arr[1]); - let result = evaluator.compare(&lhs, &rhs); - - Value::Bool(result.map_or(false, |r| r > 0)) - } else { - Value::Bool(false) - } + compare_op(evaluator, args, |r| r > 0) } +/// Greater-than-or-equal comparison. pub fn gte_op(evaluator: &Evaluator, args: &Value) -> Value { - if let Value::Array(arr) = args { - if arr.len() != 2 { - return Value::Bool(false); - } - - let lhs = evaluator.evaluate(&arr[0]); - let rhs = evaluator.evaluate(&arr[1]); - let result = evaluator.compare(&lhs, &rhs); - - Value::Bool(result.map_or(false, |r| r >= 0)) - } else { - Value::Bool(false) - } + compare_op(evaluator, args, |r| r >= 0) } +/// Less-than comparison. pub fn lt_op(evaluator: &Evaluator, args: &Value) -> Value { - if let Value::Array(arr) = args { - if arr.len() != 2 { - return Value::Bool(false); - } - - let lhs = evaluator.evaluate(&arr[0]); - let rhs = evaluator.evaluate(&arr[1]); - let result = evaluator.compare(&lhs, &rhs); - - Value::Bool(result.map_or(false, |r| r < 0)) - } else { - Value::Bool(false) - } + compare_op(evaluator, args, |r| r < 0) } +/// Less-than-or-equal comparison. pub fn lte_op(evaluator: &Evaluator, args: &Value) -> Value { + compare_op(evaluator, args, |r| r <= 0) +} + +fn compare_op(evaluator: &Evaluator, args: &Value, predicate: fn(i32) -> bool) -> Value { if let Value::Array(arr) = args { if arr.len() != 2 { return Value::Bool(false); @@ -172,7 +143,7 @@ pub fn lte_op(evaluator: &Evaluator, args: &Value) -> Value { let rhs = evaluator.evaluate(&arr[1]); let result = evaluator.compare(&lhs, &rhs); - Value::Bool(result.map_or(false, |r| r <= 0)) + Value::Bool(result.is_some_and(predicate)) } else { Value::Bool(false) } @@ -197,17 +168,35 @@ mod tests { fn test_and_all_true() { let evaluator = make_evaluator(); assert_eq!(and_op(&evaluator, &json!([{"value": true}])), json!(true)); - assert_eq!(and_op(&evaluator, &json!([{"value": true}, {"value": true}])), json!(true)); - assert_eq!(and_op(&evaluator, &json!([{"value": true}, {"value": true}, {"value": true}])), json!(true)); + assert_eq!( + and_op(&evaluator, &json!([{"value": true}, {"value": true}])), + json!(true) + ); + assert_eq!( + and_op( + &evaluator, + &json!([{"value": true}, {"value": true}, {"value": true}]) + ), + json!(true) + ); } #[test] fn test_and_any_false() { let evaluator = make_evaluator(); assert_eq!(and_op(&evaluator, &json!([{"value": false}])), json!(false)); - assert_eq!(and_op(&evaluator, &json!([{"value": true}, {"value": false}])), json!(false)); - assert_eq!(and_op(&evaluator, &json!([{"value": false}, {"value": true}])), json!(false)); - assert_eq!(and_op(&evaluator, &json!([{"value": false}, {"value": false}])), json!(false)); + assert_eq!( + and_op(&evaluator, &json!([{"value": true}, {"value": false}])), + json!(false) + ); + assert_eq!( + and_op(&evaluator, &json!([{"value": false}, {"value": true}])), + json!(false) + ); + assert_eq!( + and_op(&evaluator, &json!([{"value": false}, {"value": false}])), + json!(false) + ); } #[test] @@ -220,17 +209,35 @@ mod tests { fn test_or_any_true() { let evaluator = make_evaluator(); assert_eq!(or_op(&evaluator, &json!([{"value": true}])), json!(true)); - assert_eq!(or_op(&evaluator, &json!([{"value": true}, {"value": true}])), json!(true)); - assert_eq!(or_op(&evaluator, &json!([{"value": true}, {"value": false}])), json!(true)); - assert_eq!(or_op(&evaluator, &json!([{"value": false}, {"value": true}])), json!(true)); + assert_eq!( + or_op(&evaluator, &json!([{"value": true}, {"value": true}])), + json!(true) + ); + assert_eq!( + or_op(&evaluator, &json!([{"value": true}, {"value": false}])), + json!(true) + ); + assert_eq!( + or_op(&evaluator, &json!([{"value": false}, {"value": true}])), + json!(true) + ); } #[test] fn test_or_all_false() { let evaluator = make_evaluator(); assert_eq!(or_op(&evaluator, &json!([{"value": false}])), json!(false)); - assert_eq!(or_op(&evaluator, &json!([{"value": false}, {"value": false}])), json!(false)); - assert_eq!(or_op(&evaluator, &json!([{"value": false}, {"value": false}, {"value": false}])), json!(false)); + assert_eq!( + or_op(&evaluator, &json!([{"value": false}, {"value": false}])), + json!(false) + ); + assert_eq!( + or_op( + &evaluator, + &json!([{"value": false}, {"value": false}, {"value": false}]) + ), + json!(false) + ); } #[test] @@ -275,137 +282,368 @@ mod tests { assert_eq!(not_op(&evaluator, &json!({"value": 1})), json!(false)); assert_eq!(not_op(&evaluator, &json!({"value": 0})), json!(true)); assert_eq!(not_op(&evaluator, &json!({"value": null})), json!(true)); - assert_eq!(not_op(&evaluator, &json!({"var": "returning"})), json!(false)); + assert_eq!( + not_op(&evaluator, &json!({"var": "returning"})), + json!(false) + ); } #[test] fn test_eq_op_numbers() { let evaluator = make_evaluator(); - assert_eq!(eq_op(&evaluator, &json!([{"value": 0}, {"value": 0}])), json!(true)); - assert_eq!(eq_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), json!(true)); - assert_eq!(eq_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), json!(false)); - assert_eq!(eq_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), json!(true)); - assert_eq!(eq_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), json!(false)); + assert_eq!( + eq_op(&evaluator, &json!([{"value": 0}, {"value": 0}])), + json!(true) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), + json!(true) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), + json!(false) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), + json!(true) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), + json!(false) + ); } #[test] fn test_eq_op_strings() { let evaluator = make_evaluator(); - assert_eq!(eq_op(&evaluator, &json!([{"value": ""}, {"value": ""}])), json!(true)); - assert_eq!(eq_op(&evaluator, &json!([{"value": "abc"}, {"value": "abc"}])), json!(true)); - assert_eq!(eq_op(&evaluator, &json!([{"value": "abc"}, {"value": "def"}])), json!(false)); - assert_eq!(eq_op(&evaluator, &json!([{"var": "name"}, {"value": "John"}])), json!(true)); + assert_eq!( + eq_op(&evaluator, &json!([{"value": ""}, {"value": ""}])), + json!(true) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"value": "abc"}, {"value": "abc"}])), + json!(true) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"value": "abc"}, {"value": "def"}])), + json!(false) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"var": "name"}, {"value": "John"}])), + json!(true) + ); } #[test] fn test_eq_op_booleans() { let evaluator = make_evaluator(); - assert_eq!(eq_op(&evaluator, &json!([{"value": true}, {"value": true}])), json!(true)); - assert_eq!(eq_op(&evaluator, &json!([{"value": false}, {"value": false}])), json!(true)); - assert_eq!(eq_op(&evaluator, &json!([{"value": true}, {"value": false}])), json!(false)); + assert_eq!( + eq_op(&evaluator, &json!([{"value": true}, {"value": true}])), + json!(true) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"value": false}, {"value": false}])), + json!(true) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"value": true}, {"value": false}])), + json!(false) + ); } #[test] fn test_eq_op_null() { let evaluator = make_evaluator(); - assert_eq!(eq_op(&evaluator, &json!([{"value": null}, {"value": null}])), json!(true)); - assert_eq!(eq_op(&evaluator, &json!([{"value": null}, {"value": 0}])), json!(false)); + assert_eq!( + eq_op(&evaluator, &json!([{"value": null}, {"value": null}])), + json!(true) + ); + assert_eq!( + eq_op(&evaluator, &json!([{"value": null}, {"value": 0}])), + json!(false) + ); } #[test] fn test_gt_op() { let evaluator = make_evaluator(); - assert_eq!(gt_op(&evaluator, &json!([{"value": 1}, {"value": 0}])), json!(true)); - assert_eq!(gt_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), json!(false)); - assert_eq!(gt_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), json!(false)); - assert_eq!(gt_op(&evaluator, &json!([{"var": "age"}, {"value": 20}])), json!(true)); - assert_eq!(gt_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), json!(false)); - assert_eq!(gt_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), json!(false)); + assert_eq!( + gt_op(&evaluator, &json!([{"value": 1}, {"value": 0}])), + json!(true) + ); + assert_eq!( + gt_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), + json!(false) + ); + assert_eq!( + gt_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), + json!(false) + ); + assert_eq!( + gt_op(&evaluator, &json!([{"var": "age"}, {"value": 20}])), + json!(true) + ); + assert_eq!( + gt_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), + json!(false) + ); + assert_eq!( + gt_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), + json!(false) + ); } #[test] fn test_gt_op_strings() { let evaluator = make_evaluator(); - assert_eq!(gt_op(&evaluator, &json!([{"value": "b"}, {"value": "a"}])), json!(true)); - assert_eq!(gt_op(&evaluator, &json!([{"value": "a"}, {"value": "b"}])), json!(false)); - assert_eq!(gt_op(&evaluator, &json!([{"value": "a"}, {"value": "a"}])), json!(false)); + assert_eq!( + gt_op(&evaluator, &json!([{"value": "b"}, {"value": "a"}])), + json!(true) + ); + assert_eq!( + gt_op(&evaluator, &json!([{"value": "a"}, {"value": "b"}])), + json!(false) + ); + assert_eq!( + gt_op(&evaluator, &json!([{"value": "a"}, {"value": "a"}])), + json!(false) + ); } #[test] fn test_gte_op() { let evaluator = make_evaluator(); - assert_eq!(gte_op(&evaluator, &json!([{"value": 1}, {"value": 0}])), json!(true)); - assert_eq!(gte_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), json!(true)); - assert_eq!(gte_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), json!(false)); - assert_eq!(gte_op(&evaluator, &json!([{"var": "age"}, {"value": 20}])), json!(true)); - assert_eq!(gte_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), json!(true)); - assert_eq!(gte_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), json!(false)); + assert_eq!( + gte_op(&evaluator, &json!([{"value": 1}, {"value": 0}])), + json!(true) + ); + assert_eq!( + gte_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), + json!(true) + ); + assert_eq!( + gte_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), + json!(false) + ); + assert_eq!( + gte_op(&evaluator, &json!([{"var": "age"}, {"value": 20}])), + json!(true) + ); + assert_eq!( + gte_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), + json!(true) + ); + assert_eq!( + gte_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), + json!(false) + ); } #[test] fn test_lt_op() { let evaluator = make_evaluator(); - assert_eq!(lt_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), json!(true)); - assert_eq!(lt_op(&evaluator, &json!([{"value": 1}, {"value": 0}])), json!(false)); - assert_eq!(lt_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), json!(false)); - assert_eq!(lt_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), json!(true)); - assert_eq!(lt_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), json!(false)); - assert_eq!(lt_op(&evaluator, &json!([{"var": "age"}, {"value": 20}])), json!(false)); + assert_eq!( + lt_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), + json!(true) + ); + assert_eq!( + lt_op(&evaluator, &json!([{"value": 1}, {"value": 0}])), + json!(false) + ); + assert_eq!( + lt_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), + json!(false) + ); + assert_eq!( + lt_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), + json!(true) + ); + assert_eq!( + lt_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), + json!(false) + ); + assert_eq!( + lt_op(&evaluator, &json!([{"var": "age"}, {"value": 20}])), + json!(false) + ); } #[test] fn test_lte_op() { let evaluator = make_evaluator(); - assert_eq!(lte_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), json!(true)); - assert_eq!(lte_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), json!(true)); - assert_eq!(lte_op(&evaluator, &json!([{"value": 1}, {"value": 0}])), json!(false)); - assert_eq!(lte_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), json!(true)); - assert_eq!(lte_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), json!(true)); - assert_eq!(lte_op(&evaluator, &json!([{"var": "age"}, {"value": 20}])), json!(false)); + assert_eq!( + lte_op(&evaluator, &json!([{"value": 0}, {"value": 1}])), + json!(true) + ); + assert_eq!( + lte_op(&evaluator, &json!([{"value": 1}, {"value": 1}])), + json!(true) + ); + assert_eq!( + lte_op(&evaluator, &json!([{"value": 1}, {"value": 0}])), + json!(false) + ); + assert_eq!( + lte_op(&evaluator, &json!([{"var": "age"}, {"value": 30}])), + json!(true) + ); + assert_eq!( + lte_op(&evaluator, &json!([{"var": "age"}, {"value": 25}])), + json!(true) + ); + assert_eq!( + lte_op(&evaluator, &json!([{"var": "age"}, {"value": 20}])), + json!(false) + ); } #[test] fn test_in_op_string_contains() { let evaluator = make_evaluator(); - assert_eq!(in_op(&evaluator, &json!([{"value": "abc"}, {"value": "abcdefghijk"}])), json!(true)); - assert_eq!(in_op(&evaluator, &json!([{"value": "def"}, {"value": "abcdefghijk"}])), json!(true)); - assert_eq!(in_op(&evaluator, &json!([{"value": "xyz"}, {"value": "abcdefghijk"}])), json!(false)); + assert_eq!( + in_op( + &evaluator, + &json!([{"value": "abc"}, {"value": "abcdefghijk"}]) + ), + json!(true) + ); + assert_eq!( + in_op( + &evaluator, + &json!([{"value": "def"}, {"value": "abcdefghijk"}]) + ), + json!(true) + ); + assert_eq!( + in_op( + &evaluator, + &json!([{"value": "xyz"}, {"value": "abcdefghijk"}]) + ), + json!(false) + ); } #[test] fn test_in_op_array_contains() { let evaluator = make_evaluator(); - assert_eq!(in_op(&evaluator, &json!([{"value": 1}, {"value": [1, 2, 3]}])), json!(true)); - assert_eq!(in_op(&evaluator, &json!([{"value": 2}, {"value": [1, 2, 3]}])), json!(true)); - assert_eq!(in_op(&evaluator, &json!([{"value": 4}, {"value": [1, 2, 3]}])), json!(false)); - assert_eq!(in_op(&evaluator, &json!([{"value": 1}, {"value": []}])), json!(false)); + assert_eq!( + in_op(&evaluator, &json!([{"value": 1}, {"value": [1, 2, 3]}])), + json!(true) + ); + assert_eq!( + in_op(&evaluator, &json!([{"value": 2}, {"value": [1, 2, 3]}])), + json!(true) + ); + assert_eq!( + in_op(&evaluator, &json!([{"value": 4}, {"value": [1, 2, 3]}])), + json!(false) + ); + assert_eq!( + in_op(&evaluator, &json!([{"value": 1}, {"value": []}])), + json!(false) + ); } #[test] fn test_in_op_object_contains_key() { let evaluator = make_evaluator(); - assert_eq!(in_op(&evaluator, &json!([{"value": "a"}, {"value": {"a": 1, "b": 2}}])), json!(true)); - assert_eq!(in_op(&evaluator, &json!([{"value": "b"}, {"value": {"a": 1, "b": 2}}])), json!(true)); - assert_eq!(in_op(&evaluator, &json!([{"value": "c"}, {"value": {"a": 1, "b": 2}}])), json!(false)); + assert_eq!( + in_op( + &evaluator, + &json!([{"value": "a"}, {"value": {"a": 1, "b": 2}}]) + ), + json!(true) + ); + assert_eq!( + in_op( + &evaluator, + &json!([{"value": "b"}, {"value": {"a": 1, "b": 2}}]) + ), + json!(true) + ); + assert_eq!( + in_op( + &evaluator, + &json!([{"value": "c"}, {"value": {"a": 1, "b": 2}}]) + ), + json!(false) + ); } #[test] fn test_match_op() { let evaluator = make_evaluator(); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": ""}])), json!(true)); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": "abc"}])), json!(true)); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": "ijk"}])), json!(true)); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": "^abc"}])), json!(true)); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": "ijk$"}])), json!(true)); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": "def"}])), json!(true)); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": "b.*j"}])), json!(true)); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": "xyz"}])), json!(false)); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": ""}]) + ), + json!(true) + ); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": "abc"}]) + ), + json!(true) + ); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": "ijk"}]) + ), + json!(true) + ); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": "^abc"}]) + ), + json!(true) + ); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": "ijk$"}]) + ), + json!(true) + ); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": "def"}]) + ), + json!(true) + ); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": "b.*j"}]) + ), + json!(true) + ); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": "xyz"}]) + ), + json!(false) + ); } #[test] fn test_match_op_with_null() { let evaluator = make_evaluator(); - assert_eq!(match_op(&evaluator, &json!([{"value": null}, {"value": "abc"}])), json!(false)); - assert_eq!(match_op(&evaluator, &json!([{"value": "abcdefghijk"}, {"value": null}])), json!(false)); + assert_eq!( + match_op(&evaluator, &json!([{"value": null}, {"value": "abc"}])), + json!(false) + ); + assert_eq!( + match_op( + &evaluator, + &json!([{"value": "abcdefghijk"}, {"value": null}]) + ), + json!(false) + ); } } diff --git a/src/lib.rs b/src/lib.rs index e20815d..58bd0ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,32 @@ -pub mod murmur3; -pub mod md5; -pub mod utils; +//! ABsmartly SDK for Rust - A/B testing and feature flagging. +//! +//! This crate provides a Rust client for the ABsmartly platform, enabling +//! A/B testing, feature flagging, and experimentation in Rust applications. + +#![cfg_attr( + test, + allow( + clippy::unwrap_used, + clippy::str_to_string, + clippy::needless_pass_by_value, + clippy::unreadable_literal, + unused_results, + ) +)] + pub mod assigner; +pub mod context; pub mod jsonexpr; pub mod matcher; +pub mod md5; pub mod models; -pub mod context; +pub mod murmur3; pub mod sdk; +pub mod utils; -pub use sdk::{SDK, SDKConfig, SDKError}; pub use context::Context; -pub use models::*; +pub use models::{ + Assignment, Attribute, ContextData, ContextOptions, ContextParams, ContextState, + CustomFieldValue, ExperimentData, Exposure, Goal, PublishParams, Unit, Variant, +}; +pub use sdk::{SDKConfig, SDKError, SDK}; diff --git a/src/matcher.rs b/src/matcher.rs index 2aa268c..7d5b2c4 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -1,19 +1,28 @@ +//! Audience matching using JSON-based filter expressions. + use serde_json::Value; use std::collections::HashMap; use crate::jsonexpr::JsonExpr; +/// Evaluates audience filter expressions against a set of attribute variables. +#[derive(Debug)] pub struct AudienceMatcher { json_expr: JsonExpr, } impl AudienceMatcher { + /// Creates a new audience matcher. pub fn new() -> Self { Self { json_expr: JsonExpr::new(), } } + /// Evaluates an audience JSON string against the provided variables. + /// + /// Returns `Some(true)` if the audience matches, `Some(false)` if it doesn't, + /// or `None` if the audience string is invalid or has no filter. pub fn evaluate(&self, audience_string: &str, vars: &HashMap) -> Option { match serde_json::from_str::(audience_string) { Ok(audience) => { @@ -66,11 +75,26 @@ mod tests { let matcher = AudienceMatcher::new(); let vars = HashMap::new(); - assert_eq!(matcher.evaluate(r#"{"filter":[{"value":5}]}"#, &vars), Some(true)); - assert_eq!(matcher.evaluate(r#"{"filter":[{"value":true}]}"#, &vars), Some(true)); - assert_eq!(matcher.evaluate(r#"{"filter":[{"value":1}]}"#, &vars), Some(true)); - assert_eq!(matcher.evaluate(r#"{"filter":[{"value":null}]}"#, &vars), Some(false)); - assert_eq!(matcher.evaluate(r#"{"filter":[{"value":0}]}"#, &vars), Some(false)); + assert_eq!( + matcher.evaluate(r#"{"filter":[{"value":5}]}"#, &vars), + Some(true) + ); + assert_eq!( + matcher.evaluate(r#"{"filter":[{"value":true}]}"#, &vars), + Some(true) + ); + assert_eq!( + matcher.evaluate(r#"{"filter":[{"value":1}]}"#, &vars), + Some(true) + ); + assert_eq!( + matcher.evaluate(r#"{"filter":[{"value":null}]}"#, &vars), + Some(false) + ); + assert_eq!( + matcher.evaluate(r#"{"filter":[{"value":0}]}"#, &vars), + Some(false) + ); } #[test] diff --git a/src/md5.rs b/src/md5.rs index 0501152..e4d0cf9 100644 --- a/src/md5.rs +++ b/src/md5.rs @@ -1,5 +1,8 @@ +//! MD5 hashing utility used internally for unit hashing. + use md5::{Digest, Md5}; +/// Computes the MD5 digest of the given byte slice, returning a 16-byte array. pub fn md5(data: &[u8]) -> [u8; 16] { let mut hasher = Md5::new(); hasher.update(data); diff --git a/src/models.rs b/src/models.rs index eaf9748..d75b7bb 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,3 +1,5 @@ +//! Data models for ABsmartly context, experiments, and event publishing. + use serde::{Deserialize, Deserializer, Serialize}; use std::collections::HashMap; @@ -9,157 +11,236 @@ where Ok(opt.unwrap_or_default()) } +/// Top-level context data containing all experiment configurations. #[derive(Debug, Clone, Serialize, Deserialize, Default)] #[serde(rename_all = "camelCase")] pub struct ContextData { + /// The list of experiments in this context. #[serde(default)] pub experiments: Vec, } +/// Configuration data for a single experiment. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ExperimentData { + /// Unique experiment identifier. pub id: i64, + /// Human-readable experiment name. pub name: String, + /// The unit type used for assignment (e.g., `session_id`). #[serde(default)] pub unit_type: Option, + /// Current iteration of the experiment. #[serde(default)] pub iteration: i64, + /// If non-zero, all users are assigned to this variant. #[serde(default)] pub full_on_variant: i64, + /// Traffic allocation split weights. #[serde(default)] pub traffic_split: Vec, + /// High bits of the traffic assignment seed. #[serde(default)] pub traffic_seed_hi: u32, + /// Low bits of the traffic assignment seed. #[serde(default)] pub traffic_seed_lo: u32, + /// JSON audience filter expression. #[serde(default, deserialize_with = "deserialize_null_string")] pub audience: String, + /// When true, audience mismatches result in control assignment. #[serde(default)] pub audience_strict: bool, + /// Variant assignment split weights. #[serde(default)] pub split: Vec, + /// High bits of the variant assignment seed. #[serde(default)] pub seed_hi: u32, + /// Low bits of the variant assignment seed. #[serde(default)] pub seed_lo: u32, + /// Variant configurations. #[serde(default)] pub variants: Vec, + /// Experiment-level variables. #[serde(default)] pub variables: HashMap, + /// Custom field values attached to the experiment. #[serde(default)] pub custom_field_values: Option>, } +/// A variant's configuration payload. #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct Variant { + /// JSON configuration string for this variant. #[serde(default)] pub config: Option, } +/// A custom field value associated with an experiment. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct CustomFieldValue { + /// Field name. pub name: String, + /// Field value as a string. pub value: String, + /// Field type (e.g., "text", "number", "json", "boolean"). #[serde(rename = "type")] pub field_type: String, } +/// Tracks the computed assignment for a unit in an experiment. +#[allow(clippy::struct_excessive_bools)] #[derive(Debug, Clone, Default)] pub struct Assignment { + /// Experiment identifier. pub id: i64, + /// Experiment iteration. pub iteration: i64, + /// Full-on variant value from the experiment. pub full_on_variant: i64, + /// The unit type used for this assignment. pub unit_type: Option, + /// The assigned variant index. pub variant: i32, + /// Whether this assignment was overridden. pub overridden: bool, + /// Whether the unit was successfully assigned. pub assigned: bool, + /// Whether the assignment has been exposed (logged). pub exposed: bool, + /// Whether the unit is eligible for the experiment. pub eligible: bool, + /// Whether the experiment is in full-on mode. pub full_on: bool, + /// Whether this is a custom assignment. pub custom: bool, + /// Whether the audience filter did not match. pub audience_mismatch: bool, + /// The traffic split at the time of assignment. pub traffic_split: Option>, + /// Resolved variable values for the assigned variant. pub variables: Option>, + /// Attribute sequence number at the time of assignment. pub attrs_seq: u64, } +/// An exposure event recorded when a treatment is accessed. +#[allow(clippy::struct_excessive_bools)] #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Exposure { + /// Experiment identifier. pub id: i64, + /// Experiment name. pub name: String, + /// Timestamp of exposure in milliseconds since epoch. pub exposed_at: i64, + /// The unit identifier. pub unit: Option, + /// The assigned variant index. pub variant: i32, + /// Whether the unit was assigned. pub assigned: bool, + /// Whether the unit was eligible. pub eligible: bool, + /// Whether the assignment was overridden. pub overridden: bool, + /// Whether the experiment is in full-on mode. pub full_on: bool, + /// Whether this is a custom assignment. pub custom: bool, + /// Whether the audience filter did not match. pub audience_mismatch: bool, } +/// A user attribute with its value and timestamp. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Attribute { + /// Attribute name. pub name: String, + /// Attribute value. pub value: serde_json::Value, + /// Timestamp when the attribute was set, in milliseconds since epoch. pub set_at: i64, } +/// A goal achievement event. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Goal { + /// Goal name. pub name: String, + /// Optional properties associated with the goal. pub properties: Option>, + /// Timestamp when the goal was achieved, in milliseconds since epoch. pub achieved_at: i64, } +/// A unit identifier used in publish requests. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Unit { + /// The unit type (e.g., `session_id`). #[serde(rename = "type")] pub unit_type: String, + /// The hashed unit identifier. pub uid: Option, } +/// Parameters sent when publishing context events to the ABsmartly API. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct PublishParams { + /// Timestamp of publication in milliseconds since epoch. pub published_at: i64, + /// Units involved in this publish. pub units: Vec, + /// Whether unit identifiers are hashed. pub hashed: bool, + /// Exposure events to publish. #[serde(skip_serializing_if = "Option::is_none")] pub exposures: Option>, + /// Goal events to publish. #[serde(skip_serializing_if = "Option::is_none")] pub goals: Option>, + /// Attribute events to publish. #[serde(skip_serializing_if = "Option::is_none")] pub attributes: Option>, } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// The lifecycle state of a context. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum ContextState { + /// Context is loading data. + #[default] Loading, + /// Context is ready for use. Ready, + /// Context failed to initialize. Failed, + /// Context is publishing final events. Finalizing, + /// Context has been finalized and is no longer usable. Finalized, } -impl Default for ContextState { - fn default() -> Self { - Self::Loading - } -} - +/// Parameters for creating a new context. #[derive(Debug, Clone)] pub struct ContextParams { + /// Map of unit type to unit identifier. pub units: HashMap, } +/// Options for context behavior. #[derive(Debug, Clone, Default)] pub struct ContextOptions { + /// Delay in milliseconds before auto-publishing events. pub publish_delay: i64, + /// Interval in milliseconds for automatic context refresh. pub refresh_period: i64, } diff --git a/src/murmur3.rs b/src/murmur3.rs index dab8e14..fbae8b6 100644 --- a/src/murmur3.rs +++ b/src/murmur3.rs @@ -1,24 +1,27 @@ -const C1: u32 = 0xcc9e2d51; -const C2: u32 = 0x1b873593; -const C3: u32 = 0xe6546b64; +//! `MurmurHash3` (32-bit) implementation used for deterministic unit assignment. + +const C1: u32 = 0xcc9e_2d51; +const C2: u32 = 0x1b87_3593; +const C3: u32 = 0xe654_6b64; fn fmix32(mut h: u32) -> u32 { h ^= h >> 16; - h = h.wrapping_mul(0x85ebca6b); + h = h.wrapping_mul(0x85eb_ca6b); h ^= h >> 13; - h = h.wrapping_mul(0xc2b2ae35); + h = h.wrapping_mul(0xc2b2_ae35); h ^= h >> 16; h } fn rotl32(a: u32, b: u32) -> u32 { - (a << b) | (a >> (32 - b)) + a.rotate_left(b) } fn scramble32(block: u32) -> u32 { rotl32(block.wrapping_mul(C1), 15).wrapping_mul(C2) } +/// Computes a 32-bit `MurmurHash3` of the given byte slice with the specified seed. pub fn murmur3_32(key: &[u8], seed: u32) -> u32 { let mut hash = seed; @@ -36,24 +39,26 @@ pub fn murmur3_32(key: &[u8], seed: u32) -> u32 { let mut remaining: u32 = 0; match key.len() & 3 { 3 => { - remaining ^= (key[i + 2] as u32) << 16; - remaining ^= (key[i + 1] as u32) << 8; - remaining ^= key[i] as u32; + remaining ^= u32::from(key[i + 2]) << 16; + remaining ^= u32::from(key[i + 1]) << 8; + remaining ^= u32::from(key[i]); hash ^= scramble32(remaining); } 2 => { - remaining ^= (key[i + 1] as u32) << 8; - remaining ^= key[i] as u32; + remaining ^= u32::from(key[i + 1]) << 8; + remaining ^= u32::from(key[i]); hash ^= scramble32(remaining); } 1 => { - remaining ^= key[i] as u32; + remaining ^= u32::from(key[i]); hash ^= scramble32(remaining); } _ => {} } - hash ^= key.len() as u32; + #[allow(clippy::cast_possible_truncation)] + let len = key.len() as u32; + hash ^= len; fmix32(hash) } @@ -63,49 +68,49 @@ mod tests { #[test] fn test_empty_string() { - assert_eq!(murmur3_32(b"", 0), 0x00000000); + assert_eq!(murmur3_32(b"", 0), 0x0000_0000); } #[test] fn test_space() { - assert_eq!(murmur3_32(b" ", 0), 0x7ef49b98); + assert_eq!(murmur3_32(b" ", 0), 0x7ef4_9b98); } #[test] fn test_single_char() { - assert_eq!(murmur3_32(b"t", 0), 0xca87df4d); + assert_eq!(murmur3_32(b"t", 0), 0xca87_df4d); } #[test] fn test_two_chars() { - assert_eq!(murmur3_32(b"te", 0), 0xedb8ee1b); + assert_eq!(murmur3_32(b"te", 0), 0xedb8_ee1b); } #[test] fn test_three_chars() { - assert_eq!(murmur3_32(b"tes", 0), 0x0bb90e5a); + assert_eq!(murmur3_32(b"tes", 0), 0x0bb9_0e5a); } #[test] fn test_four_chars() { - assert_eq!(murmur3_32(b"test", 0), 0xba6bd213); + assert_eq!(murmur3_32(b"test", 0), 0xba6b_d213); } #[test] fn test_with_seed_deadbeef() { - assert_eq!(murmur3_32(b"test", 0xdeadbeef), 0xaa22d41a); + assert_eq!(murmur3_32(b"test", 0xdead_beef), 0xaa22_d41a); } #[test] fn test_with_seed_1() { - assert_eq!(murmur3_32(b"test", 1), 0x99c02ae2); + assert_eq!(murmur3_32(b"test", 1), 0x99c0_2ae2); } #[test] fn test_long_string() { assert_eq!( murmur3_32(b"The quick brown fox jumps over the lazy dog", 0), - 0x2e4ff723 + 0x2e4f_f723 ); } } diff --git a/src/sdk.rs b/src/sdk.rs index 11d0b88..9b8de46 100644 --- a/src/sdk.rs +++ b/src/sdk.rs @@ -1,20 +1,31 @@ +//! SDK client for communicating with the ABsmartly API. + use crate::context::Context; use crate::models::{ContextData, ContextOptions}; use reqwest::Client; -use serde::{Deserialize, Serialize}; +use serde::Serialize; +/// Configuration for connecting to the ABsmartly API. #[derive(Debug, Clone)] pub struct SDKConfig { + /// The ABsmartly API endpoint URL. pub endpoint: String, + /// API key for authentication. pub api_key: String, + /// Application name. pub application: String, + /// Environment name (e.g., "production", "development"). pub environment: String, + /// Optional agent identifier. pub agent: Option, + /// Request timeout in milliseconds (default: 3000). pub timeout_ms: Option, + /// Number of retry attempts for failed requests (default: 5). pub retries: Option, } impl SDKConfig { + /// Creates a new SDK configuration with the required fields. pub fn new( endpoint: impl Into, api_key: impl Into, @@ -32,16 +43,22 @@ impl SDKConfig { } } + /// Sets the agent identifier (builder pattern). + #[must_use] pub fn with_agent(mut self, agent: impl Into) -> Self { self.agent = Some(agent.into()); self } + /// Sets the request timeout in milliseconds (builder pattern). + #[must_use] pub fn with_timeout(mut self, timeout_ms: u64) -> Self { self.timeout_ms = Some(timeout_ms); self } + /// Sets the number of retry attempts (builder pattern). + #[must_use] pub fn with_retries(mut self, retries: u32) -> Self { self.retries = Some(retries); self @@ -60,32 +77,43 @@ struct UnitRequest { uid: String, } +/// The main ABsmartly SDK client. +#[derive(Debug)] pub struct SDK { config: SDKConfig, client: Client, } +/// Errors that can occur when using the SDK. #[derive(Debug, thiserror::Error)] pub enum SDKError { + /// An HTTP request to the ABsmartly API failed. #[error("HTTP request failed: {0}")] HttpError(#[from] reqwest::Error), + /// The SDK configuration is invalid. #[error("Invalid configuration: {0}")] ConfigError(String), } impl SDK { + /// Creates a new SDK client with the given configuration. + /// + /// # Errors + /// + /// Returns an error if any required configuration field is empty or if the + /// HTTP client fails to build. pub fn new(config: SDKConfig) -> Result { if config.endpoint.is_empty() { - return Err(SDKError::ConfigError("endpoint is required".to_string())); + return Err(SDKError::ConfigError("endpoint is required".to_owned())); } if config.api_key.is_empty() { - return Err(SDKError::ConfigError("api_key is required".to_string())); + return Err(SDKError::ConfigError("api_key is required".to_owned())); } if config.application.is_empty() { - return Err(SDKError::ConfigError("application is required".to_string())); + return Err(SDKError::ConfigError("application is required".to_owned())); } if config.environment.is_empty() { - return Err(SDKError::ConfigError("environment is required".to_string())); + return Err(SDKError::ConfigError("environment is required".to_owned())); } let client = Client::builder() @@ -97,6 +125,11 @@ impl SDK { Ok(Self { config, client }) } + /// Creates a context by fetching experiment data from the ABsmartly API. + /// + /// # Errors + /// + /// Returns an error if the API request fails after all retry attempts. pub async fn create_context( &self, units: I, @@ -124,7 +157,8 @@ impl SDK { let url = format!("{}/context", self.config.endpoint.trim_end_matches('/')); - let mut retries = self.config.retries.unwrap_or(5); + let max_retries = self.config.retries.unwrap_or(5); + let mut retries = max_retries; let mut last_error = None; while retries > 0 { @@ -144,39 +178,34 @@ impl SDK { Ok(resp) => { if resp.status().is_success() { let data: ContextData = resp.json().await?; - return Ok(self.create_context_with_internal(units_vec, data, options)); + return Ok(Self::build_context(units_vec, data, options)); } else if resp.status().is_server_error() { retries -= 1; - last_error = Some(SDKError::HttpError( - resp.error_for_status().unwrap_err(), - )); - tokio::time::sleep(std::time::Duration::from_millis( - 50 * (2_u64.pow((self.config.retries.unwrap_or(5) - retries) as u32)), - )) - .await; - continue; - } else { - return Err(SDKError::HttpError(resp.error_for_status().unwrap_err())); + if let Err(e) = resp.error_for_status() { + last_error = Some(SDKError::HttpError(e)); + } + } else if let Err(e) = resp.error_for_status() { + return Err(SDKError::HttpError(e)); } } Err(e) => { retries -= 1; last_error = Some(SDKError::HttpError(e)); - if retries > 0 { - tokio::time::sleep(std::time::Duration::from_millis( - 50 * (2_u64.pow((self.config.retries.unwrap_or(5) - retries) as u32)), - )) - .await; - } } } + + if retries > 0 { + let backoff = 50 * 2_u64.pow(max_retries - retries); + tokio::time::sleep(std::time::Duration::from_millis(backoff)).await; + } } Err(last_error.unwrap_or_else(|| { - SDKError::ConfigError("Failed to create context after retries".to_string()) + SDKError::ConfigError("Failed to create context after retries".to_owned()) })) } + /// Creates a context with pre-fetched experiment data. pub fn create_context_with( &self, units: I, @@ -192,18 +221,17 @@ impl SDK { .into_iter() .map(|(k, v)| (k.into(), v.into())) .collect(); - self.create_context_with_internal(units_vec, data, options) + Self::build_context(units_vec, data, options) } - fn create_context_with_internal( - &self, + fn build_context( units: Vec<(String, String)>, data: ContextData, _options: Option, ) -> Context { let mut context = Context::new(data); for (unit_type, uid) in units { - let _ = context.set_unit(&unit_type, &uid); + let _ignored = context.set_unit(&unit_type, &uid); } context } @@ -224,7 +252,9 @@ mod tests { } fn make_context_data() -> ContextData { - ContextData { experiments: vec![] } + ContextData { + experiments: vec![], + } } #[test] @@ -257,7 +287,10 @@ mod tests { ); assert_eq!(context.get_unit("session_id"), Some(&"user123".to_string())); - assert_eq!(context.get_unit("device_id"), Some(&"device456".to_string())); + assert_eq!( + context.get_unit("device_id"), + Some(&"device456".to_string()) + ); } #[test] @@ -273,7 +306,10 @@ mod tests { let context = sdk.create_context_with(units, data, None); assert_eq!(context.get_unit("session_id"), Some(&"user123".to_string())); - assert_eq!(context.get_unit("device_id"), Some(&"device456".to_string())); + assert_eq!( + context.get_unit("device_id"), + Some(&"device456".to_string()) + ); } #[test] @@ -288,7 +324,10 @@ mod tests { let context = sdk.create_context_with(units, data, None); assert_eq!(context.get_unit("session_id"), Some(&"user123".to_string())); - assert_eq!(context.get_unit("device_id"), Some(&"device456".to_string())); + assert_eq!( + context.get_unit("device_id"), + Some(&"device456".to_string()) + ); } #[test] diff --git a/src/utils.rs b/src/utils.rs index 138c653..ccddd15 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,16 +1,21 @@ +//! Utility functions for hashing, encoding, and variant selection. + use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use crate::md5::md5; +/// Encodes the given bytes as a URL-safe base64 string without padding. pub fn base64_url_no_padding(data: &[u8]) -> String { URL_SAFE_NO_PAD.encode(data) } +/// Hashes a unit value using MD5 and returns the result as a URL-safe base64 string. pub fn hash_unit(value: &str) -> String { let hash = md5(value.as_bytes()); base64_url_no_padding(&hash) } +/// Selects a variant index from a probability split based on the given probability value. pub fn choose_variant(split: &[f64], prob: f64) -> usize { let mut cum_sum = 0.0; for (i, &weight) in split.iter().enumerate() { @@ -22,8 +27,9 @@ pub fn choose_variant(split: &[f64], prob: f64) -> usize { split.len().saturating_sub(1) } +/// Compares two slices for element-wise equality. pub fn array_equals_shallow(a: &[T], b: &[T]) -> bool { - a.len() == b.len() && a.iter().zip(b.iter()).all(|(x, y)| x == y) + a == b } #[cfg(test)] @@ -51,7 +57,10 @@ mod tests { #[test] fn test_base64_url_no_padding_special_chars() { let special = "special characters açb↓c".as_bytes(); - assert_eq!(base64_url_no_padding(special), "c3BlY2lhbCBjaGFyYWN0ZXJzIGHDp2LihpNj"); + assert_eq!( + base64_url_no_padding(special), + "c3BlY2lhbCBjaGFyYWN0ZXJzIGHDp2LihpNj" + ); } #[test]