Merge pull request #997 from teloxide/distribution_fn_docs

Doc improvements & stuff
This commit is contained in:
Waffle Maybe 2024-02-15 21:10:03 +00:00 committed by GitHub
commit e1745ef0be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 188 additions and 20 deletions

View file

@ -3,6 +3,8 @@
# https://github.com/rust-lang/cargo/issues/10333
#
# "tokio/macros" and "tokio/rt-multi-thread" are required for examples
#
# N.B.: when changing this, also change `package.metadata.docs.rs`
docs = """doc
-Zrustdoc-scrape-examples
--features=full --features=nightly
@ -11,4 +13,10 @@ docs = """doc
[build]
# We pass "--cfg docsrs" when building docs to add `This is supported on feature="..." only.`
rustdocflags = ["--cfg", "docsrs", "-Znormalize-docs"]
#
# FIXME: add back `-Znormalize-docs` once <https://github.com/rust-lang/rust/issues/81091> is fixed
# or we don't depend on `generic-array` anymore
#
# N.B.: when changing this, also change `RUSTDOCFLAGS` in `.github/workflows/ci.yml` and
# `package.metadata.docs.rs.rustdoc-args` in `crates/teloxide/Cargo.toml`
rustdocflags = ["--cfg", "docsrs"]

View file

@ -9,7 +9,7 @@ name: Continuous integration
env:
RUSTFLAGS: "--cfg CI_REDIS -Dwarnings"
RUSTDOCFLAGS: -Dwarnings
RUSTDOCFLAGS: "--cfg docsrs -Dwarnings"
RUST_BACKTRACE: short
CARGO_INCREMENTAL: 0

View file

@ -1,15 +1,120 @@
# Contributing
Before contributing, please read [our code style](https://github.com/teloxide/teloxide/blob/master/CODE_STYLE.md) and [the license](https://github.com/teloxide/teloxide/blob/master/LICENSE).
> [!NOTE]
>
> These contributing instructions might not be fully up-to-date or complete.
> However, they should be a good starting point.
>
> If you find inaccuracies/missing things, please expand this or contact us.
To change the source code, fork the `master` branch of this repository and work inside your own branch. Then send us a PR into `master` branch and wait for the CI to check everything. However, you'd better check changes first locally:
## Reporting bugs, questions, feature requests
```
cargo clippy --all --all-features --all-targets
cargo test --all
RUSTDOCFLAGS="--cfg docsrs" cargo doc --open --all-features
# Using nightly rustfmt
cargo +nightly fmt --all -- --check
To report a bug or suggest new functionality, go to the [issues](https://github.com/teloxide/teloxide/issues). Try to make MRE (**M**inimal **R**eproducible **E**xample) and specify your `teloxide` version to let others help you.
If you want to ask a question, you can either
- open a new [GitHub discussion](https://github.com/teloxide/teloxide/discussions), or
- write to our Telegram group ([ENG](https://t.me/teloxide), [RU](https://t.me/teloxide_ru)).
## Code
### Style guide
Before writing code, please read [our code style](./CODE_STYLE.md).
### Git
To change the source code, you need a local copy of it. Fork the `master` branch of this repository via GitHub and clone your fork locally.
When working on a new thing, create a new branch with `git switch -c my-branch-name` (or other commands that work with branches). This way, it will be easier to manage when you want to do other things.
When your changes are ready, you can open a new GitHub pull request.
### Pull Requests
If your pull request fixes/resolves an existing [GitHub issue], please specify so in the PR description. For example:
> Fixes #991.
You can learn more about [using keywords in issues and pull requests] in the GitHub documentation.
If your pull request suggests new functionality or new changes, please explain your point of view and all the necessary details (pros, cons, why you chose the design you chose, your use cases, etc.)
In general, try to make PR title/description as clear as possible, as they are the primary ways of communicating your intent to the reviewer.
[GitHub issue]: https://github.com/teloxide/teloxide/issues
[using keywords in issues and pull requests]: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests
### Merge conflicts
If your branch has conflicts with master, please resolve them by doing something like this:
```shell
# Temporary switch to master branch
git switch master
# Pull changes from the upstream.
# You may need to use something different from "origin",
# depending on how you setup your remotes.
git pull origin master
# Switch back to your feature branch
git switch -
# Move your changes on top of changes in master branch.
git rebase master
# Here you'll need to resolve the conflicts,
# git commands will print some guidance.
# Once conflicts are resolved,
# forcefully push the changes to your fork
git push --force-with-lease
```
To report a bug, suggest new functionality, or ask a question, go to [Issues](https://github.com/teloxide/teloxide/issues). Try to make MRE (**M**inimal **R**eproducible **E**xample) and specify your teloxide version to let others help you.
### Testing
When you open a PR, it will be tested in the CI. We recommend you test the PR before opening it:
```shell
# Formatting (use `-- --check` if you only want to check)
cargo fmt --all
# Build
cargo build --features "full nightly"
# Run linter
cargo clippy --all-targets --features "full nightly"
# Running tests
cargo test --features "full nightly"
# Documentation (use --open if you want to open it in a browser)
# (note the -s, `docs` is an alias to pass some additional flags to `rustdoc`)
cargo docs
```
## @teloxidebot
`teloxide` uses @teloxidebot as a helper to manage PRs and issues. It is based on triagebot used by rustc developers, which docs can be found [here](https://forge.rust-lang.org/triagebot/index.html).
We will describe here a few most used @teloxidebot's features, but we still recommend you to read the docs.
### PR status tracking
`teloxide` uses `S-*` labels (mainly https://github.com/teloxide/teloxide/labels/S-waiting-on-author and https://github.com/teloxide/teloxide/labels/S-waiting-on-review) to track the status of pull requests.
You can change the status with `@teloxidebot review` and `@teloxidebot ready` (sets the status to https://github.com/teloxide/teloxide/labels/S-waiting-on-review) or `@teloxidebot author` (sets the status to https://github.com/teloxide/teloxide/labels/S-waiting-on-author).
Requesting a review from PR's assignee via GitHub UI will also change the status of the PR to waiting on review. Similarly, submitting a review that requests changes will change the status of the PR to waiting on author.
There is also https://github.com/teloxide/teloxide/labels/S-blocked, which can be set with `@teloxidebot blocked`.
Please note that your PR won't be reviewed unless it's waiting for review :)
### Labels
Normally, GitHub only allows privileged users to change labels. @teloxidebot allows anyone to add or remove certain [labels](https://github.com/teloxide/teloxide/labels/) with `@teloxidebot label +additional_label -removed_label`. See more in the [documentation](https://forge.rust-lang.org/triagebot/index.html).
### PR assignment
When a PR is created, @teloxidebot will automatically assign one of the maintainers to it. If you want to override this assignment, use `r? @ReviewerUsername`.

View file

@ -96,15 +96,15 @@ derive_more = "0.99"
thiserror = "1.0"
futures = "0.3.15"
pin-project = "1.0"
serde_with_macros = "1.4"
aquamarine = "0.1.11"
serde_with_macros = "3.4"
aquamarine = "0.5.0"
either = "1.9.0"
sqlx = { version = "0.7.3", optional = true, default-features = false, features = [
"macros",
"sqlite",
] }
redis = { version = "0.21", features = ["tokio-comp"], optional = true }
redis = { version = "0.24", features = ["tokio-comp"], optional = true }
serde_cbor = { version = "0.11", optional = true }
bincode = { version = "1.3", optional = true }
axum = { version = "0.6.0", optional = true }
@ -118,7 +118,7 @@ ahash = "=0.8.7"
[dev-dependencies]
rand = "0.8.3"
pretty_env_logger = "0.4.0"
pretty_env_logger = "0.5.0"
serde = "1"
serde_json = "1"
tokio = { version = "1.8", features = ["fs", "rt-multi-thread", "macros"] }
@ -130,7 +130,6 @@ tokio-stream = "0.1"
[package.metadata.docs.rs]
# NB: can't use `all-features = true`, because `sqlite-storage-nativetls` conflicts with `sqlite-storage-rustls`
features = ["full", "nightly"]
# FIXME: Add back "-Znormalize-docs" when https://github.com/rust-lang/rust/issues/93703 is fixed
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "dep_docsrs"]
cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"]

View file

@ -106,6 +106,59 @@ where
/// Specifies the distribution function that decides how updates are grouped
/// before execution.
///
/// ## Update grouping
///
/// When [`Dispatcher`] receives updates, it runs dispatching tree
/// (handlers) concurrently. This means that multiple updates can be
/// processed at the same time.
///
/// However, this is not always convenient. For example, if you have global
/// state, then you may want to process some updates sequentially, to
/// prevent state inconsistencies.
///
/// This is why `teloxide` allows grouping updates. Updates for which the
/// distribution function `f` returns the same "distribution key" `K` will
/// be run in sequence (while still being processed concurrently with the
/// updates with different distribution keys).
///
/// Updates for which `f` returns `None` will always be processed in
/// parallel.
///
/// ## Default distribution function
///
/// By default the distribution function is equivalent to `|upd|
/// upd.chat().map(|chat| chat.id)`, so updates from the same chat will be
/// processed sequentially.
///
/// This pair nicely with dialogue system, which has state attached to
/// chats.
///
/// ## Examples
///
/// Grouping updates by user who caused this update to happen:
///
/// ```
/// use teloxide::{dispatching::Dispatcher, dptree, Bot};
///
/// let bot = Bot::new("TOKEN");
/// let handler = dptree::entry() /* ... */;
/// let dp = Dispatcher::builder(bot, handler)
/// .distribution_function(|upd| upd.from().map(|user| user.id))
/// .build();
/// # let _: Dispatcher<_, (), _> = dp;
/// ```
///
/// Not grouping updates at all, always processing updates concurrently:
///
/// ```
/// use teloxide::{dispatching::Dispatcher, dptree, Bot};
///
/// let bot = Bot::new("TOKEN");
/// let handler = dptree::entry() /* ... */;
/// let dp = Dispatcher::builder(bot, handler).distribution_function(|_| None::<()>).build();
/// # let _: Dispatcher<_, (), _> = dp;
/// ```
#[must_use]
pub fn distribution_function<K>(
self,
@ -184,10 +237,13 @@ where
/// The base for update dispatching.
///
/// Updates from different chats are handled concurrently, whereas updates from
/// the same chats are handled sequentially. If the dispatcher is unable to
/// determine a chat ID of an incoming update, it will be handled concurrently.
/// Note that this behaviour can be altered with [`distribution_function`].
/// ## Update grouping
///
/// `Dispatcher` generally processes updates concurrently. However, by default,
/// updates from the same chat are processed sequentially. [Learn more about
/// update grouping].
///
/// [update grouping]: distribution_function#update-grouping
///
/// See also: ["Dispatching or
/// REPLs?"](../dispatching/index.html#dispatching-or-repls)