From 7444d016f150b68872cff91eb1b0d911d209a21b Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi <gymmasssorla@gmail.com> Date: Mon, 9 Dec 2019 20:41:19 +0600 Subject: [PATCH 1/8] Create CODE_STYLE.md Related to https://github.com/teloxide/teloxide/issues/55. --- CODE_STYLE.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 CODE_STYLE.md diff --git a/CODE_STYLE.md b/CODE_STYLE.md new file mode 100644 index 00000000..61f860e4 --- /dev/null +++ b/CODE_STYLE.md @@ -0,0 +1,26 @@ +# Code style +This is a description of a coding style that every contributor must follow. Please, read the whole document before you start pushing code. + +## Generics +Generics are always written with `where`. + +Bad: + +```rust + pub fn new<N: Into<String>, + T: Into<String>, + P: Into<InputFile>, + E: Into<String>> + (user_id: i32, name: N, title: T, png_sticker: P, emojis: E) -> Self { ... } +``` + +Good: + +```rust + pub fn new<N, T, P, E>(user_id: i32, name: N, title: T, png_sticker: P, emojis: E) -> Self + where + N: Into<String>, + T: Into<String>, + P: Into<InputFile>, + E: Into<String> { ... } +``` From 2a1b459bc22d8673553e52ae37a1c90d6a7a8412 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi <hirrolot@gmail.com> Date: Wed, 8 Jan 2020 16:20:20 +0600 Subject: [PATCH 2/8] Update CODE_STYLE.md --- CODE_STYLE.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index 61f860e4..d0603921 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -24,3 +24,20 @@ Good: P: Into<InputFile>, E: Into<String> { ... } ``` + +## Comments +Comments must describe what your code does and mustn't describe how your code does it and bla-bla-bla. Be sure that your comments follow the grammar, including punctiation, the first capital letter and so on. + +Bad: + +```rust +/// this function make request to telegram +pub fn make_request(url: &str) -> String { ... } +``` + +Good: + +```rust +/// This function makes a request to Telegram. +pub fn make_request(url: &str) -> String { ... } +``` From 105318050209e390d24823851f21f88d28f27b5e Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi <hirrolot@gmail.com> Date: Wed, 8 Jan 2020 16:20:44 +0600 Subject: [PATCH 3/8] Update CODE_STYLE.md --- CODE_STYLE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index d0603921..f4fbb053 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -26,7 +26,7 @@ Good: ``` ## Comments -Comments must describe what your code does and mustn't describe how your code does it and bla-bla-bla. Be sure that your comments follow the grammar, including punctiation, the first capital letter and so on. +Comments must describe what your code does and mustn't describe how your code does it and bla-bla-bla. Be sure that your comments follow the grammar, including punctuation, the first capital letter and so on. Bad: From 28c5d9f8c2425611e9bf9b9ade1a5afbdb8943db Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi <hirrolot@gmail.com> Date: Wed, 8 Jan 2020 20:46:09 +0600 Subject: [PATCH 4/8] Use Self where possible (CODE_STYLE.md) --- CODE_STYLE.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index f4fbb053..c134a771 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -41,3 +41,49 @@ Good: /// This function makes a request to Telegram. pub fn make_request(url: &str) -> String { ... } ``` + +## Use Self where possible +Bad: + +```rust +impl ErrorKind { + fn print(&self) { + ErrorKind::Io => println!("Io"), + ErrorKind::Network => println!("Network"), + ErrorKind::Json => println!("Json"), + } +} +``` + +Good: +```rust +impl ErrorKind { + fn print(&self) { + Self::Io => println!("Io"), + Self::Network => println!("Network"), + Self::Json => println!("Json"), + } +} +``` + +<details> + <summary>More examples</summary> + +Bad: + +```rust +impl<'a> AnswerCallbackQuery<'a> { + pub(crate) fn new<C>(bot: &'a Bot, callback_query_id: C) -> AnswerCallbackQuery<'a> + where +C: Into<String>, { ... } +``` + +Good: + +```rust +impl<'a> AnswerCallbackQuery<'a> { + pub(crate) fn new<C>(bot: &'a Bot, callback_query_id: C) -> Self + where +C: Into<String>, { ... } +``` +</details> From 2f086daf8e7a8d6126d4e2d840256f0c07c16582 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi <hirrolot@gmail.com> Date: Sat, 25 Jan 2020 04:29:55 +0600 Subject: [PATCH 5/8] Update CODE_STYLE.md --- CODE_STYLE.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index c134a771..6dc4e2a8 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -42,6 +42,30 @@ Good: pub fn make_request(url: &str) -> String { ... } ``` +Also, link resources in your comments when possible: + +```rust +/// Download a file from Telegram. +/// +/// `path` can be obtained from the [`Bot::get_file`]. +/// +/// To download into [`AsyncWrite`] (e.g. [`tokio::fs::File`]), see +/// [`Bot::download_file`]. +/// +/// [`Bot::get_file`]: crate::bot::Bot::get_file +/// [`AsyncWrite`]: tokio::io::AsyncWrite +/// [`tokio::fs::File`]: tokio::fs::File +/// [`Bot::download_file`]: crate::Bot::download_file +#[cfg(feature = "unstable-stream")] +pub async fn download_file_stream( + &self, + path: &str, +) -> Result<impl Stream<Item = Result<Bytes, reqwest::Error>>, reqwest::Error> +{ + download_file_stream(&self.client, &self.token, path).await +} +``` + ## Use Self where possible Bad: From e473d58770ee56a12fa5ea207d726ced646057ec Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi <hirrolot@gmail.com> Date: Sat, 25 Jan 2020 04:47:47 +0600 Subject: [PATCH 6/8] Update CODE_STYLE.md --- CODE_STYLE.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index 6dc4e2a8..aaa9a311 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -111,3 +111,7 @@ impl<'a> AnswerCallbackQuery<'a> { C: Into<String>, { ... } ``` </details> + +# Naming + 1. Avoid unnecessary duplication (`Message::message_id` -> `Message::id` using `#[serde(rename = "message_id")]`). + 2. Use a generic parameter name `S` for streams, `Fut` for futures, `F` for functions (where possible). From 499e63ef06a90e8bc1473d1a08d5a3882431da5e Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi <hirrolot@gmail.com> Date: Sat, 25 Jan 2020 04:48:02 +0600 Subject: [PATCH 7/8] Update CODE_STYLE.md --- CODE_STYLE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index aaa9a311..359d4c08 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -112,6 +112,6 @@ C: Into<String>, { ... } ``` </details> -# Naming +## Naming 1. Avoid unnecessary duplication (`Message::message_id` -> `Message::id` using `#[serde(rename = "message_id")]`). 2. Use a generic parameter name `S` for streams, `Fut` for futures, `F` for functions (where possible). From adb6ad43198451d70e6c8624fd344681c9869c85 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi <hirrolot@gmail.com> Date: Sat, 25 Jan 2020 04:59:37 +0600 Subject: [PATCH 8/8] Update CODE_STYLE.md --- CODE_STYLE.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/CODE_STYLE.md b/CODE_STYLE.md index 359d4c08..384d7fa4 100644 --- a/CODE_STYLE.md +++ b/CODE_STYLE.md @@ -26,7 +26,7 @@ Good: ``` ## Comments -Comments must describe what your code does and mustn't describe how your code does it and bla-bla-bla. Be sure that your comments follow the grammar, including punctuation, the first capital letter and so on. + 1. Comments must describe what your code does and mustn't describe how your code does it and bla-bla-bla. Be sure that your comments follow the grammar, including punctuation, the first capital letter and so on. Bad: @@ -42,7 +42,7 @@ Good: pub fn make_request(url: &str) -> String { ... } ``` -Also, link resources in your comments when possible: + 2. Also, link resources in your comments when possible: ```rust /// Download a file from Telegram. @@ -115,3 +115,10 @@ C: Into<String>, { ... } ## Naming 1. Avoid unnecessary duplication (`Message::message_id` -> `Message::id` using `#[serde(rename = "message_id")]`). 2. Use a generic parameter name `S` for streams, `Fut` for futures, `F` for functions (where possible). + +## Deriving + 1. Derive `Copy`, `Eq`, `Hash`, `PartialEq`, `Clone`, `Debug` for public types when possible (note: if the default `Debug` implementation is weird, you should manually implement it by yourself). + 2. Derive `Default` when there is an algorithm to get a default value for your type. + +## Misc + 1. Use `Into<...>` only where there exists at least one conversion **and** it will be logically to use.