-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix clippy issues, remove some dep #452
Changes from 1 commit
e14b823
64e4bf5
9ca1749
e74e842
0cb160c
b27ab89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,20 @@ | |
|
||
**Other Languages:** [English](README.en.md) | ||
|
||
Telegram RSS 机器人 [@RustRssBot](http://t.me/RustRssBot) | ||
Telegram RSS 机器人 [@RustRssBot](http://t.me/RustRssBot) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 space? |
||
|
||
**支持:** | ||
- [x] RSS 0.9 | ||
- [x] RSS 0.91 | ||
- [x] RSS 0.92 | ||
- [x] RSS 0.93 | ||
- [x] RSS 0.94 | ||
- [x] RSS 1.0 | ||
- [x] RSS 2.0 | ||
- [x] Atom 0.3 | ||
- [x] Atom 1.0 | ||
- [x] JSON Feed 1 | ||
|
||
- [x] RSS 0.9 | ||
- [x] RSS 0.91 | ||
- [x] RSS 0.92 | ||
- [x] RSS 0.93 | ||
- [x] RSS 0.94 | ||
- [x] RSS 1.0 | ||
- [x] RSS 2.0 | ||
- [x] Atom 0.3 | ||
- [x] Atom 1.0 | ||
- [x] JSON Feed 1 | ||
|
||
## 使用 | ||
|
||
|
@@ -25,13 +26,13 @@ Telegram RSS 机器人 [@RustRssBot](http://t.me/RustRssBot) | |
|
||
## 下载 | ||
|
||
可直接从 [Releases](https://github.com/iovxw/rssbot/releases) 下载预编译的程序(带 `zh` 的为中文版), Linux 版本为 *musl* 静态链接, 无需其他依赖 | ||
可直接从 [Releases](https://github.com/iovxw/rssbot/releases) 下载预编译的程序(带 `zh` 的为中文版), Linux 版本为 _musl_ 静态链接, 无需其他依赖 | ||
|
||
## 编译 | ||
|
||
**请先尝试从上面下载, 如不可行或者有其他需求再手动编译** | ||
|
||
先安装 *Rust Nightly* 以及 *Cargo* (推荐使用 [`rustup`](https://www.rustup.rs/)), 然后: | ||
先安装 _Rust Nightly_ 以及 _Cargo_ (推荐使用 [`rustup`](https://www.rustup.rs/)), 然后: | ||
|
||
``` | ||
cargo build --release | ||
|
@@ -56,7 +57,7 @@ OPTIONS: | |
multiple times to allow multiple admins | ||
--api-uri <tgapi-uri> Custom telegram api URI [default: https://api.telegram.org/] | ||
-d, --database <path> Path to database [default: ./rssbot.json] | ||
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2097152] | ||
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2M] | ||
--max-interval <seconds> Maximum fetch interval [default: 43200] | ||
--min-interval <seconds> Minimum fetch interval [default: 300] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ use std::path::PathBuf; | |
use std::process; | ||
use std::sync::Arc; | ||
|
||
use anyhow::Context; | ||
use anyhow::{anyhow, Context}; | ||
use hyper_proxy::{Intercept, Proxy}; | ||
use std::sync::OnceLock; | ||
use structopt::StructOpt; | ||
|
@@ -67,10 +67,10 @@ pub struct Opt { | |
)] | ||
// default is 12 hours | ||
max_interval: u32, | ||
/// Maximum feed size, 0 is unlimited | ||
#[structopt(long, value_name = "bytes", default_value = "2097152")] | ||
// default is 2MiB | ||
max_feed_size: u64, | ||
/// Maximum feed size, 0 is unlimited. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the docstring that will show up in --help, don't add |
||
#[structopt(long, value_name = "bytes", default_value = "2M")] | ||
// Default is 2M. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2M and 2MiB are different |
||
max_feed_size: String, | ||
/// Private mode, only specified user can use this bot. | ||
/// This argument can be passed multiple times to allow multiple admins | ||
#[structopt( | ||
|
@@ -105,6 +105,22 @@ fn check_interval(s: String) -> Result<(), String> { | |
}) | ||
} | ||
|
||
/// Parse human readable size into bytes. | ||
fn parse_human_size(s: &str) -> anyhow::Result<u64> { | ||
const BASE: u64 = 1024; | ||
let s = s.trim().trim_end_matches(|x| x == 'B' || x == 'b'); | ||
match s.chars().last().map(|x| x.to_ascii_lowercase()) { | ||
Some('b') => Ok(s[..s.len() - 1].parse()?), | ||
Some('k') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE), | ||
Some('m') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(2)), | ||
Some('g') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(3)), | ||
Some('t') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(4)), | ||
Some(x) if x.is_ascii_digit() => Ok(s.parse()?), | ||
Some(x) => Err(anyhow!("invalid size character: {}", x)), | ||
None => Err(anyhow!("empty size")), | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
enable_fail_fast(); | ||
|
@@ -125,7 +141,11 @@ async fn main() -> anyhow::Result<()> { | |
.context("Initialization failed, check your network and Telegram token")?; | ||
|
||
let bot_name = me.user.username.clone().unwrap(); | ||
crate::client::init_client(&bot_name, opt.insecure, opt.max_feed_size); | ||
crate::client::init_client( | ||
&bot_name, | ||
opt.insecure, | ||
parse_human_size(&opt.max_feed_size).context("Invalid max_feed_size")?, | ||
); | ||
|
||
BOT_NAME.set(bot_name).unwrap(); | ||
BOT_ID.set(me.user.id).unwrap(); | ||
|
@@ -173,3 +193,16 @@ fn print_error<E: std::error::Error>(err: E) { | |
.show_backtrace(true) | ||
); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_human_size() { | ||
assert_eq!(parse_human_size("2M").unwrap(), 2_097_152); | ||
assert_eq!(parse_human_size("2G").unwrap(), 2_147_483_648); | ||
assert_eq!(parse_human_size("2mb").unwrap(), 2_097_152); | ||
assert_eq!(parse_human_size("2097152").unwrap(), 2_097_152); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't change the style