Skip to content

Commit c396c6d

Browse files
committed
Shorten use of config::{Validator, Accessor, Overridable, Config}
1 parent a76d734 commit c396c6d

13 files changed

Lines changed: 80 additions & 74 deletions

File tree

src/notify.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::Deserialize;
55
use spdlog::prelude::*;
66

77
use crate::{
8-
config::{self, Overridable},
8+
config::{Accessor, Overridable, Validator},
99
platform::*,
1010
source::Notification,
1111
};
@@ -14,11 +14,11 @@ use crate::{
1414
#[serde(tag = "platform")]
1515
pub enum NotifierConfig {
1616
#[serde(rename = "QQ")]
17-
Qq(config::Accessor<qq::notify::ConfigParams>),
18-
Telegram(config::Accessor<telegram::notify::ConfigParams>),
17+
Qq(Accessor<qq::notify::ConfigParams>),
18+
Telegram(Accessor<telegram::notify::ConfigParams>),
1919
}
2020

21-
impl config::Validator for NotifierConfig {
21+
impl Validator for NotifierConfig {
2222
fn validate(&self) -> anyhow::Result<()> {
2323
match self {
2424
Self::Qq(p) => p.validate(),
@@ -35,16 +35,15 @@ impl NotifierConfig {
3535
{
3636
match self {
3737
Self::Qq(n) => {
38-
let new: <qq::notify::ConfigParams as config::Overridable>::Override =
39-
new.try_into()?;
40-
Ok(Self::Qq(config::Accessor::new_then_validate(
38+
let new: <qq::notify::ConfigParams as Overridable>::Override = new.try_into()?;
39+
Ok(Self::Qq(Accessor::new_then_validate(
4140
n.into_inner().override_into(new),
4241
)?))
4342
}
4443
Self::Telegram(n) => {
45-
let new: <telegram::notify::ConfigParams as config::Overridable>::Override =
44+
let new: <telegram::notify::ConfigParams as Overridable>::Override =
4645
new.try_into()?;
47-
Ok(Self::Telegram(config::Accessor::new_then_validate(
46+
Ok(Self::Telegram(Accessor::new_then_validate(
4847
n.into_inner().override_into(new),
4948
)?))
5049
}
@@ -68,7 +67,7 @@ pub trait NotifierTrait: PlatformTrait {
6867
) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send + 'a>>;
6968
}
7069

71-
pub fn notifier(params: config::Accessor<NotifierConfig>) -> Box<dyn NotifierTrait> {
70+
pub fn notifier(params: Accessor<NotifierConfig>) -> Box<dyn NotifierTrait> {
7271
match params.into_inner() {
7372
NotifierConfig::Qq(p) => Box::new(qq::notify::Notifier::new(p)),
7473
NotifierConfig::Telegram(p) => Box::new(telegram::notify::Notifier::new(p)),

src/platform/bilibili/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ pub mod source;
33
use reqwest::header::{self, HeaderMap, HeaderValue};
44
use serde::Deserialize;
55

6-
use crate::{config, helper, prop};
6+
use crate::{
7+
config::{Accessor, Validator},
8+
helper, prop,
9+
};
710

811
#[derive(Clone, Debug, PartialEq, Deserialize)]
912
pub struct ConfigGlobal {
10-
pub playback: config::Accessor<Option<source::playback::ConfigGlobal>>,
13+
pub playback: Accessor<Option<source::playback::ConfigGlobal>>,
1114
}
1215

13-
impl config::Validator for ConfigGlobal {
16+
impl Validator for ConfigGlobal {
1417
fn validate(&self) -> anyhow::Result<()> {
1518
self.playback.validate()?;
1619
Ok(())

src/platform/bilibili/source/live.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tokio::sync::Mutex;
1414

1515
use super::super::{bilibili_request_builder, upgrade_to_https, Response};
1616
use crate::{
17-
config,
17+
config::{Accessor, Validator},
1818
platform::{PlatformMetadata, PlatformTrait},
1919
source::{
2020
FetcherTrait, LiveStatus, LiveStatusKind, Status, StatusKind, StatusSource,
@@ -27,7 +27,7 @@ pub struct ConfigParams {
2727
pub user_id: u64,
2828
}
2929

30-
impl config::Validator for ConfigParams {
30+
impl Validator for ConfigParams {
3131
fn validate(&self) -> anyhow::Result<()> {
3232
Ok(())
3333
}
@@ -60,7 +60,7 @@ struct ResponseDataRoom {
6060
}
6161

6262
pub struct Fetcher {
63-
params: config::Accessor<ConfigParams>,
63+
params: Accessor<ConfigParams>,
6464
room_data_cache: Mutex<Option<ResponseDataRoom>>,
6565
}
6666

@@ -85,7 +85,7 @@ impl fmt::Display for Fetcher {
8585
}
8686

8787
impl Fetcher {
88-
pub fn new(params: config::Accessor<ConfigParams>) -> Self {
88+
pub fn new(params: Accessor<ConfigParams>) -> Self {
8989
Self {
9090
params,
9191
room_data_cache: Mutex::new(None),

src/platform/bilibili/source/playback/bililive_recorder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use warp::Filter;
1616

1717
use super::PLATFORM_METADATA;
1818
use crate::{
19-
config,
19+
config::{Accessor, Validator},
2020
source::{Document, Playback, PlaybackFormat, StatusSource, Update, UpdateKind},
2121
};
2222

@@ -26,7 +26,7 @@ pub struct ConfigBililiveRecorder {
2626
pub working_directory: PathBuf,
2727
}
2828

29-
impl config::Validator for ConfigBililiveRecorder {
29+
impl Validator for ConfigBililiveRecorder {
3030
fn validate(&self) -> anyhow::Result<()> {
3131
self.listen_webhook.to_addr()?;
3232
Ok(())
@@ -51,13 +51,13 @@ impl ConfigListen {
5151
}
5252

5353
pub struct BililiveRecorder {
54-
config: config::Accessor<ConfigBililiveRecorder>,
54+
config: Accessor<ConfigBililiveRecorder>,
5555
senders: Arc<Mutex<HashMap<u64, mpsc::Sender<Update>>>>,
5656
is_listening: Mutex<bool>,
5757
}
5858

5959
impl BililiveRecorder {
60-
pub fn new(config: config::Accessor<ConfigBililiveRecorder>) -> Self {
60+
pub fn new(config: Accessor<ConfigBililiveRecorder>) -> Self {
6161
Self {
6262
config,
6363
senders: Arc::new(Mutex::new(HashMap::new())),

src/platform/bilibili/source/playback/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ use spdlog::prelude::*;
1010
use tokio::sync::mpsc;
1111

1212
use crate::{
13-
config,
13+
config::{Accessor, Config, Validator},
1414
platform::{PlatformMetadata, PlatformTrait},
1515
source::{ListenerTrait, Update},
1616
};
1717

1818
#[derive(Clone, Debug, PartialEq, Deserialize)]
1919
pub struct ConfigGlobal {
20-
pub bililive_recorder: config::Accessor<ConfigBililiveRecorder>,
20+
pub bililive_recorder: Accessor<ConfigBililiveRecorder>,
2121
}
2222

23-
impl config::Validator for ConfigGlobal {
23+
impl Validator for ConfigGlobal {
2424
fn validate(&self) -> anyhow::Result<()> {
2525
self.bililive_recorder.validate()?;
2626
Ok(())
@@ -32,9 +32,9 @@ pub struct ConfigParams {
3232
pub room_id: u64,
3333
}
3434

35-
impl config::Validator for ConfigParams {
35+
impl Validator for ConfigParams {
3636
fn validate(&self) -> anyhow::Result<()> {
37-
config::Config::global()
37+
Config::global()
3838
.platform()
3939
.bilibili
4040
.as_ref()
@@ -56,7 +56,7 @@ const PLATFORM_METADATA: PlatformMetadata = PlatformMetadata {
5656

5757
static BACKEND: Lazy<BililiveRecorder> = Lazy::new(|| {
5858
BililiveRecorder::new(
59-
config::Config::global()
59+
Config::global()
6060
.platform()
6161
.bilibili
6262
.as_ref()
@@ -70,7 +70,7 @@ static BACKEND: Lazy<BililiveRecorder> = Lazy::new(|| {
7070
});
7171

7272
pub struct Listener {
73-
params: config::Accessor<ConfigParams>,
73+
params: Accessor<ConfigParams>,
7474
}
7575

7676
impl PlatformTrait for Listener {
@@ -95,7 +95,7 @@ impl fmt::Display for Listener {
9595
}
9696

9797
impl Listener {
98-
pub fn new(params: config::Accessor<ConfigParams>) -> Self {
98+
pub fn new(params: Accessor<ConfigParams>) -> Self {
9999
Self { params }
100100
}
101101

src/platform/bilibili/source/space.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use tokio::sync::Mutex;
1616

1717
use super::super::{upgrade_to_https, Response};
1818
use crate::{
19-
config,
19+
config::{Accessor, Validator},
2020
platform::{PlatformMetadata, PlatformTrait},
2121
prop,
2222
source::{
@@ -30,7 +30,7 @@ pub struct ConfigParams {
3030
pub user_id: u64,
3131
}
3232

33-
impl config::Validator for ConfigParams {
33+
impl Validator for ConfigParams {
3434
fn validate(&self) -> anyhow::Result<()> {
3535
Ok(())
3636
}
@@ -451,7 +451,7 @@ mod data {
451451
}
452452

453453
pub struct Fetcher {
454-
params: config::Accessor<ConfigParams>,
454+
params: Accessor<ConfigParams>,
455455
// We cache all blocked posts and filter them again later, because the API sometimes
456456
// incorrectly returns fans-only posts for guests, this leads us to incorrectly assume that
457457
// these are new normal posts.
@@ -479,7 +479,7 @@ impl fmt::Display for Fetcher {
479479
}
480480

481481
impl Fetcher {
482-
pub fn new(params: config::Accessor<ConfigParams>) -> Self {
482+
pub fn new(params: Accessor<ConfigParams>) -> Self {
483483
Self {
484484
params,
485485
blocked: Mutex::new(BlockedPostIds(HashSet::new())),

src/platform/bilibili/source/video.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde_json as json;
77

88
use super::super::{bilibili_request_builder, upgrade_to_https, Response};
99
use crate::{
10-
config,
10+
config::{Accessor, Validator},
1111
platform::{PlatformMetadata, PlatformTrait},
1212
source::{
1313
FetcherTrait, Post, PostAttachment, PostAttachmentImage, PostContent, PostUrl, Posts,
@@ -21,7 +21,7 @@ pub struct ConfigParams {
2121
pub series_id: u64,
2222
}
2323

24-
impl config::Validator for ConfigParams {
24+
impl Validator for ConfigParams {
2525
fn validate(&self) -> anyhow::Result<()> {
2626
Ok(())
2727
}
@@ -58,7 +58,7 @@ mod data {
5858
}
5959

6060
pub struct Fetcher {
61-
params: config::Accessor<ConfigParams>,
61+
params: Accessor<ConfigParams>,
6262
}
6363

6464
impl PlatformTrait for Fetcher {
@@ -82,7 +82,7 @@ impl fmt::Display for Fetcher {
8282
}
8383

8484
impl Fetcher {
85-
pub fn new(params: config::Accessor<ConfigParams>) -> Self {
85+
pub fn new(params: Accessor<ConfigParams>) -> Self {
8686
Self { params }
8787
}
8888

src/platform/qq/notify/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use spdlog::prelude::*;
66

77
use super::{lagrange, ConfigChat};
88
use crate::{
9-
config::{self, Config},
9+
config::{self, Accessor, Config, Overridable, Validator},
1010
notify::NotifierTrait,
1111
platform::{PlatformMetadata, PlatformTrait},
1212
source::{
@@ -26,9 +26,9 @@ pub struct ConfigParams {
2626
pub from: String,
2727
}
2828

29-
impl config::Validator for ConfigParams {
29+
impl Validator for ConfigParams {
3030
fn validate(&self) -> anyhow::Result<()> {
31-
let _account = config::Config::global()
31+
let _account = Config::global()
3232
.platform()
3333
.qq
3434
.as_ref()
@@ -61,7 +61,7 @@ pub struct ConfigOverride {
6161
pub from: Option<String>,
6262
}
6363

64-
impl config::Overridable for ConfigParams {
64+
impl Overridable for ConfigParams {
6565
type Override = ConfigOverride;
6666

6767
fn override_into(self, new: Self::Override) -> Self
@@ -81,7 +81,7 @@ impl config::Overridable for ConfigParams {
8181
}
8282

8383
pub struct Notifier {
84-
params: config::Accessor<ConfigParams>,
84+
params: Accessor<ConfigParams>,
8585
backend: lagrange::LagrangeOnebot<'static>,
8686
}
8787

@@ -101,7 +101,7 @@ impl NotifierTrait for Notifier {
101101
}
102102

103103
impl Notifier {
104-
pub fn new(params: config::Accessor<ConfigParams>) -> Self {
104+
pub fn new(params: Accessor<ConfigParams>) -> Self {
105105
let lagrange = lagrange::LagrangeOnebot::new(
106106
&Config::global()
107107
.platform()

src/platform/telegram/notify/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use tokio::sync::Mutex;
1818

1919
use super::{ConfigChat, ConfigToken};
2020
use crate::{
21-
config::{self, AsSecretRef, Config},
21+
config::{self, Accessor, AsSecretRef, Config, Overridable, Validator},
2222
helper,
2323
notify::NotifierTrait,
2424
platform::{PlatformMetadata, PlatformTrait},
@@ -40,11 +40,11 @@ pub struct ConfigParams {
4040
pub token: Option<ConfigToken>,
4141
}
4242

43-
impl config::Validator for ConfigParams {
43+
impl Validator for ConfigParams {
4444
fn validate(&self) -> anyhow::Result<()> {
4545
match &self.token {
4646
Some(token) => token.validate(),
47-
None => match config::Config::global()
47+
None => match Config::global()
4848
.platform()
4949
.telegram
5050
.as_ref()
@@ -67,7 +67,7 @@ impl fmt::Display for ConfigParams {
6767
}
6868
}
6969

70-
impl config::Overridable for ConfigParams {
70+
impl Overridable for ConfigParams {
7171
type Override = ConfigOverride;
7272

7373
fn override_into(self, new: Self::Override) -> Self
@@ -98,7 +98,7 @@ pub struct ConfigOverride {
9898
}
9999

100100
pub struct Notifier {
101-
params: config::Accessor<ConfigParams>,
101+
params: Accessor<ConfigParams>,
102102
current_live: Mutex<Option<CurrentLive>>,
103103
}
104104

@@ -120,7 +120,7 @@ impl NotifierTrait for Notifier {
120120
}
121121

122122
impl Notifier {
123-
pub fn new(params: config::Accessor<ConfigParams>) -> Self {
123+
pub fn new(params: Accessor<ConfigParams>) -> Self {
124124
Self {
125125
params,
126126
current_live: Mutex::new(None),

0 commit comments

Comments
 (0)