From 0215bacc42136391268c2317c849a64ad0021b91 Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 00:14:54 +0100 Subject: [PATCH 01/25] added lychee config and workflow fixed xtask exit code --- .github/workflows/check-links.yml | 30 ++++++++++++++++++++++ ci/dictionary.txt | 2 +- ci/lychee.toml | 41 +++++++++++++++++++++++++++++++ xtask/src/tests.rs | 11 ++++++--- 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/check-links.yml create mode 100644 ci/lychee.toml diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml new file mode 100644 index 00000000..e8c92826 --- /dev/null +++ b/.github/workflows/check-links.yml @@ -0,0 +1,30 @@ +name: Links + +on: + pull_request: + branches: + - master + +jobs: + linkChecker: + runs-on: ubuntu-latest + permissions: + issues: write # required for peter-evans/create-issue-from-file + steps: + - uses: actions/checkout@v4 + + - name: Link Checker + id: lychee + uses: lycheeverse/lychee-action@v2 + with: + fail: false + # Use a custom GitHub token, (fix ratelimit issues) + # token: ${{ secrets.CUSTOM_TOKEN }} + + - name: Create Issue From File + if: steps.lychee.outputs.exit_code != 0 + uses: peter-evans/create-issue-from-file@v5 + with: + title: Link Checker Report + content-filepath: ./lychee/out.md + labels: report, automated issue diff --git a/ci/dictionary.txt b/ci/dictionary.txt index c2daf226..e2547f00 100644 --- a/ci/dictionary.txt +++ b/ci/dictionary.txt @@ -348,4 +348,4 @@ YYYY zurich enum thiserror -tempfile \ No newline at end of file +tempfile diff --git a/ci/lychee.toml b/ci/lychee.toml new file mode 100644 index 00000000..5ff30f90 --- /dev/null +++ b/ci/lychee.toml @@ -0,0 +1,41 @@ +# Don't show interactive progress bar while checking links. +no_progress = true + +# Path to summary output file. +output = "lychee-report.md" + +# Enable link caching. This can be helpful to avoid checking the same links on +# multiple runs. +cache = true + +# Discard all cached requests older than this duration. +max_cache_age = "2d" + +user_agent = "curl/7.83. 1" + +# Website timeout from connect to response finished. +timeout = 20 + +# Minimum wait time in seconds between retries of failed requests. +retry_wait_time = 20 + +# Comma-separated list of accepted status codes for valid links. +# Supported values are: +# +# accept = ["200..=204", "429"] +# accept = "200..=204, 429" +# accept = ["200", "429"] +# accept = "200, 429" +accept = ["200", "429"] + +# Request method +method = "get" + +# Custom request headers +headers = [] + +# Exclude loopback IP address range and localhost from checking. +exclude_loopback = false + +# Check mail addresses +include_mail = true \ No newline at end of file diff --git a/xtask/src/tests.rs b/xtask/src/tests.rs index 924851a2..2175509b 100644 --- a/xtask/src/tests.rs +++ b/xtask/src/tests.rs @@ -31,14 +31,17 @@ fn run_all_tests() -> Result<(), Box> { if !failures.is_empty() { println!("\n--- Test Summary ---"); for name in failures { - println!("āŒ {name} failed! Re-run with the command:"); - println!(" cargo xtask test {name}"); + eprintln!("āŒ {name} failed! Re-run with the command:"); + eprintln!(" cargo xtask test {name}"); } + Err(Box::new(std::io::Error::new( + std::io::ErrorKind::Other, + "test failed", + ))) } else { println!("\nšŸŽ‰ All tests passed!"); + Ok(()) } - - Ok(()) } fn cargo_test() -> Result<(), Box> { From 2baa8729faf71e1ea4976eb5344451abcf6625d2 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 9 Jan 2025 10:47:57 +0100 Subject: [PATCH 02/25] Update lychee.toml --- ci/lychee.toml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/ci/lychee.toml b/ci/lychee.toml index 5ff30f90..9cec40c8 100644 --- a/ci/lychee.toml +++ b/ci/lychee.toml @@ -20,13 +20,7 @@ timeout = 20 retry_wait_time = 20 # Comma-separated list of accepted status codes for valid links. -# Supported values are: -# -# accept = ["200..=204", "429"] -# accept = "200..=204, 429" -# accept = ["200", "429"] -# accept = "200, 429" -accept = ["200", "429"] +accept = ["200", "429"] # 429 for ratelimits # Request method method = "get" @@ -38,4 +32,4 @@ headers = [] exclude_loopback = false # Check mail addresses -include_mail = true \ No newline at end of file +include_mail = true From 9b4aa015fdb6063b6058a6cbe633d06a87a229f9 Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 12:30:58 +0100 Subject: [PATCH 03/25] fixed workflow run only on pr --- .github/workflows/check-links.yml | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index e8c92826..8601d265 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -9,7 +9,7 @@ jobs: linkChecker: runs-on: ubuntu-latest permissions: - issues: write # required for peter-evans/create-issue-from-file + pull-requests: write steps: - uses: actions/checkout@v4 @@ -18,13 +18,16 @@ jobs: uses: lycheeverse/lychee-action@v2 with: fail: false - # Use a custom GitHub token, (fix ratelimit issues) + # Use a custom GitHub token to avoid rate limit issues # token: ${{ secrets.CUSTOM_TOKEN }} - - name: Create Issue From File - if: steps.lychee.outputs.exit_code != 0 - uses: peter-evans/create-issue-from-file@v5 + - name: Post Comment on Pull Request + if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0 + uses: marocchino/sticky-pull-request-comment@v2 with: - title: Link Checker Report - content-filepath: ./lychee/out.md - labels: report, automated issue + header: Link Checker Report + message: | + The link checker found some issues. Please review the report below: + ``` + ${{ steps.lychee.outputs.report }} + ``` From e51a3cbffe1586478ea72f391b51f5995e05e42d Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 12:40:42 +0100 Subject: [PATCH 04/25] fixed workflow invalid msg --- .github/workflows/check-links.yml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index 8601d265..11ee54f5 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -17,9 +17,13 @@ jobs: id: lychee uses: lycheeverse/lychee-action@v2 with: - fail: false - # Use a custom GitHub token to avoid rate limit issues - # token: ${{ secrets.CUSTOM_TOKEN }} + fail: true + output: ./lychee/out.md + + - name: Read Report File + id: read_report + run: | + echo "::set-output name=content::$(cat ./lychee/out.md | jq -R -s '.')" - name: Post Comment on Pull Request if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0 @@ -29,5 +33,4 @@ jobs: message: | The link checker found some issues. Please review the report below: ``` - ${{ steps.lychee.outputs.report }} - ``` + ${{ steps.read_report.outputs.content }} From 755f83be08c752ec8b6c3396d150a4afd6a94005 Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 12:42:44 +0100 Subject: [PATCH 05/25] fixed workflow exit --- .github/workflows/check-links.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index 11ee54f5..aca7bac4 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -17,7 +17,7 @@ jobs: id: lychee uses: lycheeverse/lychee-action@v2 with: - fail: true + fail: false output: ./lychee/out.md - name: Read Report File @@ -34,3 +34,7 @@ jobs: The link checker found some issues. Please review the report below: ``` ${{ steps.read_report.outputs.content }} + + - name: Fail Workflow + if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0 + run: exit 1 From 5e1b399bf2e9b6ff00aae315a53113aa8cf84959 Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 12:47:57 +0100 Subject: [PATCH 06/25] fixed fmt issues --- .github/workflows/check-links.yml | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index aca7bac4..b400cb84 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -20,20 +20,13 @@ jobs: fail: false output: ./lychee/out.md - - name: Read Report File - id: read_report - run: | - echo "::set-output name=content::$(cat ./lychee/out.md | jq -R -s '.')" - - name: Post Comment on Pull Request if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0 - uses: marocchino/sticky-pull-request-comment@v2 - with: - header: Link Checker Report - message: | - The link checker found some issues. Please review the report below: - ``` - ${{ steps.read_report.outputs.content }} + run: | + echo "The link checker found some issues. Please review the report below:" > comment.md + echo "" >> comment.md + cat ./lychee/out.md >> comment.md + gh pr comment ${{ github.event.pull_request.number }} --body-file comment.md - name: Fail Workflow if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0 From 23579ae002d7565725af7457c633f7569a8d80cb Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 12:51:22 +0100 Subject: [PATCH 07/25] added github token --- .github/workflows/check-links.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index b400cb84..7ba77ed0 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -8,6 +8,8 @@ on: jobs: linkChecker: runs-on: ubuntu-latest + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} permissions: pull-requests: write steps: From dbe34d942c819c4f3680d4d580b46931f90fe3f8 Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 13:12:57 +0100 Subject: [PATCH 08/25] updated xtask lychee to use config file fixed link error in intro (about.md) --- src/intro.md | 2 +- xtask/src/tests.rs | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/intro.md b/src/intro.md index 47abbda4..d604e1ad 100644 --- a/src/intro.md +++ b/src/intro.md @@ -4,7 +4,7 @@ This _Rust Cookbook_ is a collection of simple examples that demonstrate good practices to accomplish common programming tasks, using the crates of the Rust ecosystem. -[Read more about _Rust Cookbook_](about.html), including tips for +[Read more about _Rust Cookbook_](about.md), including tips for how to read the book, how to use the examples, and notes on conventions. ## Contributing diff --git a/xtask/src/tests.rs b/xtask/src/tests.rs index 2175509b..f1612511 100644 --- a/xtask/src/tests.rs +++ b/xtask/src/tests.rs @@ -90,12 +90,8 @@ fn link_checker() -> Result<(), Box> { .current_dir(project_root()) .args([ "./book", - "--retry-wait-time", - "20", - "--max-retries", - "3", - "--accept", - "429", // accept 429 (ratelimit) errors as valid + "--config", + "./ci/lychee.toml" ]) .status()?; From f52199f44b21af14c0db9ae9fbe62716263506a8 Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 13:17:22 +0100 Subject: [PATCH 09/25] workflow update use lychee cache --- .github/workflows/check-links.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index 7ba77ed0..a25d1eec 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -15,13 +15,28 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Restore lychee cache + uses: actions/cache@v4 + with: + path: .lycheecache + key: cache-lychee-${{ github.sha }} + restore-keys: cache-lychee- + - name: Link Checker id: lychee uses: lycheeverse/lychee-action@v2 with: fail: false + args: "--config ./ci/lychee.toml" output: ./lychee/out.md + - name: Save lychee cache + uses: actions/cache/save@v4 + if: always() + with: + path: .lycheecache + key: ${{ steps.restore-cache.outputs.cache-primary-key }} + - name: Post Comment on Pull Request if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0 run: | From edbbe501fb39175a78cea6ee9e7290bbc0c3ad3b Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 13:27:21 +0100 Subject: [PATCH 10/25] fixed wrong output and cache --- .github/workflows/check-links.yml | 3 ++- ci/lychee.toml | 3 --- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index a25d1eec..dde7addb 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -16,6 +16,7 @@ jobs: - uses: actions/checkout@v4 - name: Restore lychee cache + id: restore-cache uses: actions/cache@v4 with: path: .lycheecache @@ -35,7 +36,7 @@ jobs: if: always() with: path: .lycheecache - key: ${{ steps.restore-cache.outputs.cache-primary-key }} + key: cache-lychee-${{ github.sha }} - name: Post Comment on Pull Request if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0 diff --git a/ci/lychee.toml b/ci/lychee.toml index 9cec40c8..402a61ef 100644 --- a/ci/lychee.toml +++ b/ci/lychee.toml @@ -1,9 +1,6 @@ # Don't show interactive progress bar while checking links. no_progress = true -# Path to summary output file. -output = "lychee-report.md" - # Enable link caching. This can be helpful to avoid checking the same links on # multiple runs. cache = true From e7372bfcfac31a31c25d8bf341f746fb6c43177f Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 13:34:33 +0100 Subject: [PATCH 11/25] fix link checker --- .github/workflows/check-links.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index dde7addb..18bfd165 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -28,8 +28,8 @@ jobs: uses: lycheeverse/lychee-action@v2 with: fail: false - args: "--config ./ci/lychee.toml" output: ./lychee/out.md + args: "--base . --config ./ci/lychee.toml ." - name: Save lychee cache uses: actions/cache/save@v4 From 04bf9c34258c69ae17a59aa641376c82152cfa2b Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 13:56:27 +0100 Subject: [PATCH 12/25] fixed cache save and creation --- .github/workflows/check-links.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index 18bfd165..3bd1a426 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -10,6 +10,7 @@ jobs: runs-on: ubuntu-latest env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CACHE_KEY: cache-lychee-${{ github.sha }} permissions: pull-requests: write steps: @@ -20,7 +21,7 @@ jobs: uses: actions/cache@v4 with: path: .lycheecache - key: cache-lychee-${{ github.sha }} + key: ${{ }} restore-keys: cache-lychee- - name: Link Checker @@ -36,7 +37,7 @@ jobs: if: always() with: path: .lycheecache - key: cache-lychee-${{ github.sha }} + key: ${{ steps.restore-cache.outputs.cache-primary-key || env.CACHE_KEY }} # fallback in case the cache wasn't created yet - name: Post Comment on Pull Request if: github.event_name == 'pull_request' && steps.lychee.outputs.exit_code != 0 From 871258c2c470bdf54f2ca22766194c13ecfab14a Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 14:05:40 +0100 Subject: [PATCH 13/25] invalid syntax fix --- .github/workflows/check-links.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml index 3bd1a426..6217b255 100644 --- a/.github/workflows/check-links.yml +++ b/.github/workflows/check-links.yml @@ -21,7 +21,7 @@ jobs: uses: actions/cache@v4 with: path: .lycheecache - key: ${{ }} + key: ${{ env.CACHE_KEY }} restore-keys: cache-lychee- - name: Link Checker From 8bf7e96c113ded008f8af5b21def504aadb0677e Mon Sep 17 00:00:00 2001 From: Arteii Date: Thu, 9 Jan 2025 15:23:24 +0100 Subject: [PATCH 14/25] Fix #722 (Broken Links) - CONTRIBUTING.md (removed link as its not required and no longer available) - src/encoding/string/url-encode.md (updated form_urlencoded is not a part of url its re-exported) - README.md (removed travis CI link [404](https://www.travis-ci.com/rust-lang-nursery/rust-cookbook)) - src/development_tools/debugging/config_log/log-custom.md (log4rs::config::Config is a re-export from log4rs::config::runtime::Config) - src/web/scraping/broken.md (tokio::spawn to tokio::task::spawn) - src/web/clients/api/rate-limited.md (removed file rust src broken links broken and is not used or displayed on the current cookbook build) - src/cryptography/hashing/hmac.md (updated description, added tag type hint, and updated links) --- CONTRIBUTING.md | 2 - README.md | 1 - src/cryptography/hashing/hmac.md | 16 +++-- .../debugging/config_log/log-custom.md | 2 +- src/encoding/string/url-encode.md | 4 +- src/web/clients/api/rate-limited.md | 68 ------------------- src/web/scraping/broken.md | 4 +- 7 files changed, 16 insertions(+), 81 deletions(-) delete mode 100644 src/web/clients/api/rate-limited.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a300d36e..101f4c6c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -257,13 +257,11 @@ after the code sample. > generator [`rand::Rng`]. > > The [distributions available are documented here][rand-distributions]. -> An example using the [`Normal`] distribution is shown below. [uniform distribution]: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous) [`Distribution::sample`]: https://docs.rs/rand/*/rand/distributions/trait.Distribution.html#tymethod.sample [`rand::Rng`]: https://docs.rs/rand/*/rand/trait.Rng.html [rand-distributions]: https://docs.rs/rand/*/rand/distributions/index.html -[`Normal`]: https://docs.rs/rand/*/rand/distributions/struct.Normal.html #### Code diff --git a/README.md b/README.md index bc8140ca..c1097ab7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # A Rust Cookbook   [![Build Status travis]][travis] [Build Status travis]: https://api.travis-ci.com/rust-lang-nursery/rust-cookbook.svg?branch=master -[travis]: https://travis-ci.com/rust-lang-nursery/rust-cookbook **[Read it here]**. diff --git a/src/cryptography/hashing/hmac.md b/src/cryptography/hashing/hmac.md index 704d891a..90f442d0 100644 --- a/src/cryptography/hashing/hmac.md +++ b/src/cryptography/hashing/hmac.md @@ -2,8 +2,9 @@ [![ring-badge]][ring] [![cat-cryptography-badge]][cat-cryptography] -Uses [`ring::hmac`] to creates a [`hmac::Signature`] of a string then verifies the signature is correct. - +The [`hmac::sign`] method is used to calculate the HMAC digest (also called a tag) of the message using the provided key. +The resulting [`hmac::Tag`] structure contains the raw bytes of the HMAC, +which can later be verified with[`hmac::verify`] to ensure the message has not been tampered with and comes from a trusted source. ```rust,edition2018 use ring::{hmac, rand}; @@ -17,12 +18,17 @@ fn main() -> Result<(), Unspecified> { let key = hmac::Key::new(hmac::HMAC_SHA256, &key_value); let message = "Legitimate and important message."; - let signature = hmac::sign(&key, message.as_bytes()); + let signature: hmac::Tag = hmac::sign(&key, message.as_bytes()); hmac::verify(&key, message.as_bytes(), signature.as_ref())?; Ok(()) } ``` -[`hmac::Signature`]: https://briansmith.org/rustdoc/ring/hmac/struct.Signature.html -[`ring::hmac`]: https://briansmith.org/rustdoc/ring/hmac/ +[`ring::hmac`]: https://docs.rs/ring/*/ring/hmac/index.html + +[`hmac::sign`]: https://docs.rs/ring/*/ring/hmac/fn.sign.html + +[`hmac::Tag`]: https://docs.rs/ring/*/ring/hmac/struct.Tag.html + +[`hmac::verify`]: https://docs.rs/ring/*/ring/hmac/fn.verify.html diff --git a/src/development_tools/debugging/config_log/log-custom.md b/src/development_tools/debugging/config_log/log-custom.md index 1bf7b648..5ebac32b 100644 --- a/src/development_tools/debugging/config_log/log-custom.md +++ b/src/development_tools/debugging/config_log/log-custom.md @@ -47,6 +47,6 @@ fn main() -> Result<()> { ``` [`log4rs::append::file::FileAppender`]: https://docs.rs/log4rs/*/log4rs/append/file/struct.FileAppender.html -[`log4rs::config::Config`]: https://docs.rs/log4rs/*/log4rs/config/struct.Config.html +[`log4rs::config::Config`]: https://docs.rs/log4rs/*/log4rs/config/runtime/struct.Config.html [`log4rs::encode::pattern`]: https://docs.rs/log4rs/*/log4rs/encode/pattern/index.html [`log::LevelFilter`]: https://docs.rs/log/*/log/enum.LevelFilter.html diff --git a/src/encoding/string/url-encode.md b/src/encoding/string/url-encode.md index c907e12a..61ca48af 100644 --- a/src/encoding/string/url-encode.md +++ b/src/encoding/string/url-encode.md @@ -23,7 +23,7 @@ fn main() { } ``` -[`form_urlencoded::byte_serialize`]: https://docs.rs/url/*/url/form_urlencoded/fn.byte_serialize.html -[`form_urlencoded::parse`]: https://docs.rs/url/*/url/form_urlencoded/fn.parse.html +[`form_urlencoded::byte_serialize`]: https://docs.rs//fn.byte_serialize.html +[`form_urlencoded::parse`]: https://docs.rs/form_urlencoded/*/form_urlencoded/fn.parse.html [application/x-www-form-urlencoded]: https://url.spec.whatwg.org/#application/x-www-form-urlencoded diff --git a/src/web/clients/api/rate-limited.md b/src/web/clients/api/rate-limited.md deleted file mode 100644 index 635e8e53..00000000 --- a/src/web/clients/api/rate-limited.md +++ /dev/null @@ -1,68 +0,0 @@ -## Handle a rate-limited API - -[![reqwest-badge]][reqwest] [![hyper-badge]][hyper] [![cat-net-badge]][cat-net] - -This example uses the [GitHub API - Rate limiting], as an example of how to -handle remote server errors. This example uses the [`hyper::header!`] macro -to parse the response header and checks for [`reqwest::StatusCode::Forbidden`]. -If the response exceeds the rate limit, the example waits and retries. - -```rust,edition2018,no_run -# use error_chain::error_chain; - -use std::time::{Duration, UNIX_EPOCH}; -use std::thread; -use reqwest::StatusCode; -# -# error_chain! { -# foreign_links { -# Io(std::io::Error); -# Time(std::time::SystemTimeError); -# Reqwest(reqwest::Error); -# } -# } - -header! { (XRateLimitLimit, "X-RateLimit-Limit") => [usize] } -header! { (XRateLimitRemaining, "X-RateLimit-Remaining") => [usize] } -header! { (XRateLimitReset, "X-RateLimit-Reset") => [u64] } - -fn main() -> Result<()> { - loop { - let url = "https://api.github.com/users/rust-lang-nursery "; - let client = reqwest::Client::new(); - let response = client.get(url).send()?; - - let rate_limit = response.headers().get::().ok_or( - "response doesn't include the expected X-RateLimit-Limit header", - )?; - - let rate_remaining = response.headers().get::().ok_or( - "response doesn't include the expected X-RateLimit-Remaining header", - )?; - - let rate_reset_at = response.headers().get::().ok_or( - "response doesn't include the expected X-RateLimit-Reset header", - )?; - - let rate_reset_within = Duration::from_secs(**rate_reset_at) - UNIX_EPOCH.elapsed()?; - - if response.status() == StatusCode::Forbidden && **rate_remaining == 0 { - println!("Sleeping for {} seconds.", rate_reset_within.as_secs()); - thread::sleep(rate_reset_within); - return main(); - } else { - println!( - "Rate limit is currently {}/{}, the reset of this limit will be within {} seconds.", - **rate_remaining, - **rate_limit, - rate_reset_within.as_secs(), - ); - break; - } - } - Ok(()) -} -``` - -[`hyper::header!`]: https://doc.servo.org/hyper/header/index.html#defining-custom-headers -[`reqwest::StatusCode::Forbidden`]: https://docs.rs/reqwest/*/reqwest/struct.StatusCode.html#associatedconstant.FORBIDDEN diff --git a/src/web/scraping/broken.md b/src/web/scraping/broken.md index 79243460..02990003 100644 --- a/src/web/scraping/broken.md +++ b/src/web/scraping/broken.md @@ -6,7 +6,7 @@ Call `get_base_url` to retrieve the base URL. If the document has a base tag, get the href [`attr`] from base tag. [`Position::BeforePath`] of the original URL acts as a default. -Iterates through links in the document and creates a [`tokio::spawn`] task that will +Iterates through links in the document and creates a [`tokio::task::spawn`] task that will parse an individual link with [`url::ParseOptions`] and [`Url::parse`]). The task makes a request to the links with [reqwest] and verifies [`StatusCode`]. Then the tasks `await` completion before ending the program. @@ -28,6 +28,6 @@ fn main() -> anyhow::Result<()> { [`attr`]: https://docs.rs/select/*/select/node/struct.Node.html#method.attr [`Position::BeforePath`]: https://docs.rs/url/*/url/enum.Position.html#variant.BeforePath [`StatusCode`]: https://docs.rs/reqwest/*/reqwest/struct.StatusCode.html -[`tokio::spawn`]: https://docs.rs/tokio/*/tokio/fn.spawn.html +[`tokio::task::spawn`]: https://docs.rs/tokio/*/tokio/task/fn.spawn.html [`url::Parse`]: https://docs.rs/url/*/url/struct.Url.html#method.parse [`url::ParseOptions`]: https://docs.rs/url/*/url/struct.ParseOptions.html From b949055ca62c08823935dcd9741ffe417008ea4b Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 15:19:01 +0100 Subject: [PATCH 15/25] added mdbook workflow --- .github/workflows/deploy-mdbook.yml | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/deploy-mdbook.yml diff --git a/.github/workflows/deploy-mdbook.yml b/.github/workflows/deploy-mdbook.yml new file mode 100644 index 00000000..07d80071 --- /dev/null +++ b/.github/workflows/deploy-mdbook.yml @@ -0,0 +1,39 @@ +name: Build and Deploy mdBook + +on: + push: + branches: + - main + workflow_dispatch: + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up cache for mdbook + id: cache-mdbook + uses: actions/cache@v3 + with: + path: | + ~/.cargo/bin/mdbook + key: mdbook-v0.4.43 + + - name: Install mdBook (if not cached) + if: steps.cache-mdbook.outputs.cache-hit != 'true' + run: | + curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.43/mdbook-v0.4.43-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C ~/.cargo/bin + chmod +x ~/.cargo/bin/mdbook + + - name: Build mdBook + run: | + mdbook build + + - name: Deploy to GitHub Pages + uses: peaceiris/actions-gh-pages@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./book From 49e417de1174905132e1a43a46345118787d0577 Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 15:19:39 +0100 Subject: [PATCH 16/25] fixed branch name --- .github/workflows/deploy-mdbook.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-mdbook.yml b/.github/workflows/deploy-mdbook.yml index 07d80071..8dbf948e 100644 --- a/.github/workflows/deploy-mdbook.yml +++ b/.github/workflows/deploy-mdbook.yml @@ -3,7 +3,7 @@ name: Build and Deploy mdBook on: push: branches: - - main + - master workflow_dispatch: jobs: From 6bd160ceed0b946cf2fd6a4b98c8189fa9c2620f Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 16:14:11 +0100 Subject: [PATCH 17/25] added test report using workflow by https://github.com/innoq/action-cargo-test-report --- .github/workflows/mdbook-test.yml | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/mdbook-test.yml diff --git a/.github/workflows/mdbook-test.yml b/.github/workflows/mdbook-test.yml new file mode 100644 index 00000000..e4063beb --- /dev/null +++ b/.github/workflows/mdbook-test.yml @@ -0,0 +1,38 @@ +name: Test Rust Project + +on: + pull_request: + branches: + - master + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Rust + uses: actions/setup-rust@v1 + with: + rust-version: nighlty + + - name: Run tests + run: | + cargo +nightly test -- -Z unstable-options --format json --report-time | tee results.json + + - name: Prepare junit report + id: cargo_reporter + uses: innoq/action-cargo-test-report + with: + cargo-test-report-json: 'results.json' + + - name: Publish Test Report + uses: mikepenz/action-junit-report@main + if: always() + with: + check_name: Test Report + fail_on_failure: true + require_tests: true + summary: ${{ steps.cargo_reporter.outputs.summary }} \ No newline at end of file From 4d0e9f04a5cf0e92043c4ff0bc8a993e8aee1abd Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 16:17:04 +0100 Subject: [PATCH 18/25] fixed missing version --- .github/workflows/mdbook-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mdbook-test.yml b/.github/workflows/mdbook-test.yml index e4063beb..6a18f088 100644 --- a/.github/workflows/mdbook-test.yml +++ b/.github/workflows/mdbook-test.yml @@ -24,7 +24,7 @@ jobs: - name: Prepare junit report id: cargo_reporter - uses: innoq/action-cargo-test-report + uses: innoq/action-cargo-test-report@v1 with: cargo-test-report-json: 'results.json' From 672d6918c331a0b9617fc2fa0b15e7499273d73b Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 16:20:13 +0100 Subject: [PATCH 19/25] fixed workflow rust setup --- .github/workflows/mdbook-test.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/mdbook-test.yml b/.github/workflows/mdbook-test.yml index 6a18f088..09e3ffc3 100644 --- a/.github/workflows/mdbook-test.yml +++ b/.github/workflows/mdbook-test.yml @@ -14,9 +14,10 @@ jobs: uses: actions/checkout@v3 - name: Set up Rust - uses: actions/setup-rust@v1 + uses: actions-rust-lang/setup-rust-toolchain@v1 with: - rust-version: nighlty + cache: 'true' + toolchain: nightly - name: Run tests run: | From dd83b4e45d0cf168b983ccd79b50def09ecb8a04 Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 16:41:18 +0100 Subject: [PATCH 20/25] removed caro2junit use nightly features instead --- .github/workflows/mdbook-test.yml | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/.github/workflows/mdbook-test.yml b/.github/workflows/mdbook-test.yml index 09e3ffc3..ba90bdac 100644 --- a/.github/workflows/mdbook-test.yml +++ b/.github/workflows/mdbook-test.yml @@ -21,19 +21,13 @@ jobs: - name: Run tests run: | - cargo +nightly test -- -Z unstable-options --format json --report-time | tee results.json - - - name: Prepare junit report - id: cargo_reporter - uses: innoq/action-cargo-test-report@v1 - with: - cargo-test-report-json: 'results.json' + cargo +nightly test -- -Z unstable-options --format junit --report-time | tee results.xml - name: Publish Test Report - uses: mikepenz/action-junit-report@main + uses: mikepenz/action-junit-report@v5.2.0 if: always() with: check_name: Test Report fail_on_failure: true require_tests: true - summary: ${{ steps.cargo_reporter.outputs.summary }} \ No newline at end of file + report_paths: '**/results.xml' \ No newline at end of file From ab5877487206f6395cfbaf4551c0c2db6c87a64e Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 16:48:58 +0100 Subject: [PATCH 21/25] simplify workflow --- .github/workflows/mdbook-test.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/mdbook-test.yml b/.github/workflows/mdbook-test.yml index ba90bdac..99e23755 100644 --- a/.github/workflows/mdbook-test.yml +++ b/.github/workflows/mdbook-test.yml @@ -21,13 +21,12 @@ jobs: - name: Run tests run: | - cargo +nightly test -- -Z unstable-options --format junit --report-time | tee results.xml + cargo +nightly test -- -Z unstable-options --format junit --report-time - name: Publish Test Report uses: mikepenz/action-junit-report@v5.2.0 if: always() with: - check_name: Test Report fail_on_failure: true require_tests: true - report_paths: '**/results.xml' \ No newline at end of file + summary: ${{ steps.cargo_reporter.outputs.summary }} \ No newline at end of file From 56b72695f96e653b66dd13e084b7122d4952a0d9 Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 16:53:21 +0100 Subject: [PATCH 22/25] fixed wrong summary --- .github/workflows/mdbook-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/mdbook-test.yml b/.github/workflows/mdbook-test.yml index 99e23755..b5a2673d 100644 --- a/.github/workflows/mdbook-test.yml +++ b/.github/workflows/mdbook-test.yml @@ -20,6 +20,7 @@ jobs: toolchain: nightly - name: Run tests + id: cargo_test run: | cargo +nightly test -- -Z unstable-options --format junit --report-time @@ -28,5 +29,4 @@ jobs: if: always() with: fail_on_failure: true - require_tests: true - summary: ${{ steps.cargo_reporter.outputs.summary }} \ No newline at end of file + summary: ${{ steps.cargo_test.outputs.summary }} From 1f300191c32affa88b4157b32baed75805feff0c Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 16:59:02 +0100 Subject: [PATCH 23/25] maybe fixed now? --- .github/workflows/mdbook-test.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/mdbook-test.yml b/.github/workflows/mdbook-test.yml index b5a2673d..a59de8eb 100644 --- a/.github/workflows/mdbook-test.yml +++ b/.github/workflows/mdbook-test.yml @@ -22,11 +22,16 @@ jobs: - name: Run tests id: cargo_test run: | - cargo +nightly test -- -Z unstable-options --format junit --report-time + cargo +nightly test -- -Z unstable-options --format junit --report-time --test-threads=1 --output junit-reports/TEST-results.xml + + - name: List files for debugging + run: ls -R - name: Publish Test Report uses: mikepenz/action-junit-report@v5.2.0 if: always() with: fail_on_failure: true + require_tests: true summary: ${{ steps.cargo_test.outputs.summary }} + report_paths: junit-reports/TEST-*.xml From 3a29ae48f000b80fff84e02bb513ae6f118a8adb Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 17:09:28 +0100 Subject: [PATCH 24/25] exclude main.rs test --- .github/workflows/mdbook-test.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/mdbook-test.yml b/.github/workflows/mdbook-test.yml index a59de8eb..59b32714 100644 --- a/.github/workflows/mdbook-test.yml +++ b/.github/workflows/mdbook-test.yml @@ -22,9 +22,10 @@ jobs: - name: Run tests id: cargo_test run: | - cargo +nightly test -- -Z unstable-options --format junit --report-time --test-threads=1 --output junit-reports/TEST-results.xml + cargo +nightly test --test skeptic -- -Z unstable-options --format junit --report-time --test-threads=1 | tee ./TEST-cookbook.xml - name: List files for debugging + if: always() run: ls -R - name: Publish Test Report @@ -34,4 +35,4 @@ jobs: fail_on_failure: true require_tests: true summary: ${{ steps.cargo_test.outputs.summary }} - report_paths: junit-reports/TEST-*.xml + report_paths: '**/TEST-*.xml' From be26fb2653b0b96c1a851e23fd0161682d9b5b62 Mon Sep 17 00:00:00 2001 From: Arteii Date: Fri, 10 Jan 2025 17:23:00 +0100 Subject: [PATCH 25/25] fixed broken link --- src/encoding/string/url-encode.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/encoding/string/url-encode.md b/src/encoding/string/url-encode.md index 61ca48af..0fc3f726 100644 --- a/src/encoding/string/url-encode.md +++ b/src/encoding/string/url-encode.md @@ -23,7 +23,7 @@ fn main() { } ``` -[`form_urlencoded::byte_serialize`]: https://docs.rs//fn.byte_serialize.html +[`form_urlencoded::byte_serialize`]: https://docs.rs/form_urlencoded/*/form_urlencoded/fn.byte_serialize.html [`form_urlencoded::parse`]: https://docs.rs/form_urlencoded/*/form_urlencoded/fn.parse.html [application/x-www-form-urlencoded]: https://url.spec.whatwg.org/#application/x-www-form-urlencoded