Skip to content

Commit 82ecd7f

Browse files
committed
🦄 refactor: 优化格式化配置,补充部分注释
1 parent 059e8cb commit 82ecd7f

11 files changed

Lines changed: 296 additions & 100 deletions

File tree

Cargo.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/external-media-integration/Cargo.toml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,53 @@ edition = "2024"
77
crate-type = ["cdylib"]
88

99
[dependencies]
10+
# N-API
1011
napi = { version = "3.8", features = ["tokio_rt", "napi9"] }
1112
napi-derive = "3.5"
1213

13-
anyhow = "1.0"
14-
serde = { version = "1.0", features = ["derive"] }
15-
serde_json = "1.0"
14+
# Async Runtime
15+
tokio = { version = "1", features = ["full"] }
1616

17+
# Log
1718
tracing = "0.1"
18-
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
1919
tracing-appender = "0.2"
20+
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
2021

21-
discord-rich-presence = "1.0"
22+
# Error & Serialize
23+
anyhow = "1.0"
24+
serde = { version = "1.0", features = ["derive"] }
25+
serde_json = "1.0"
2226

27+
# Utilities
28+
discord-rich-presence = "1.0"
2329
url = "2.5"
2430

25-
tokio = { version = "1", features = ["full"] }
31+
[build-dependencies]
32+
napi-build = "2.3"
2633

34+
# Windows
2735
[target.'cfg(target_os = "windows")'.dependencies.windows]
2836
version = "0.62"
2937
features = ["Media_Playback", "Storage_Streams"]
3038

39+
# Linux
3140
[target.'cfg(target_os = "linux")'.dependencies]
3241
mpris-server = "0.9"
3342
tempfile = "3.24"
3443

44+
# macOS
3545
[target.'cfg(target_os = "macos")'.dependencies]
3646
block2 = "0.6"
3747
objc2 = "0.6"
3848
objc2-app-kit = "0.3"
3949
objc2-foundation = "0.3"
4050
objc2-media-player = "0.3"
4151

42-
[build-dependencies]
43-
napi-build = "2.3"
44-
52+
# Clippy Lints
4553
[lints.clippy]
4654
pedantic = { level = "warn", priority = -1 }
4755
nursery = { level = "warn", priority = -1 }
4856

4957
cast_possible_truncation = "allow"
5058
cast_precision_loss = "allow"
59+
doc_markdown = "allow"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
comment_width = 100
2+
format_code_in_doc_comments = true
3+
imports_granularity = "Crate"
4+
imports_layout = "Vertical"
5+
wrap_comments = true
6+
group_imports = "StdExternalCrate"

native/external-media-integration/src/discord.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
1-
use std::sync::mpsc::{self, Receiver, Sender};
2-
use std::sync::{LazyLock, Mutex};
3-
use std::thread;
4-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
1+
use std::{
2+
sync::{
3+
LazyLock,
4+
Mutex,
5+
mpsc::{
6+
self,
7+
Receiver,
8+
Sender,
9+
},
10+
},
11+
thread,
12+
time::{
13+
Duration,
14+
SystemTime,
15+
UNIX_EPOCH,
16+
},
17+
};
518

6-
use discord_rich_presence::activity::{
7-
Activity, ActivityType, Assets, Button, StatusDisplayType, Timestamps,
19+
use discord_rich_presence::{
20+
DiscordIpc,
21+
DiscordIpcClient,
22+
activity::{
23+
Activity,
24+
ActivityType,
25+
Assets,
26+
Button,
27+
StatusDisplayType,
28+
Timestamps,
29+
},
30+
};
31+
use tracing::{
32+
debug,
33+
info,
34+
warn,
835
};
9-
use discord_rich_presence::{DiscordIpc, DiscordIpcClient};
10-
use tracing::{debug, info, warn};
1136

1237
use crate::model::{
13-
DiscordConfigPayload, DiscordDisplayMode, MetadataPayload, PlayStatePayload, PlaybackStatus,
38+
DiscordConfigPayload,
39+
DiscordDisplayMode,
40+
MetadataPayload,
41+
PlayStatePayload,
42+
PlaybackStatus,
1443
TimelinePayload,
1544
};
1645

native/external-media-integration/src/lib.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
#![deny(missing_docs)]
22

33
//! 一个 Electron 的原生插件,用于将播放信息同步到系统的媒体控件和/或 Discord RPC
4-
5-
use napi::Result;
6-
use napi::bindgen_prelude::{Function, Unknown};
7-
use napi::threadsafe_function::UnknownReturnValue;
4+
//!
5+
//! 目前支持 Windows、Linux 和 MacOS 的媒体控件交互
6+
7+
use napi::{
8+
Result,
9+
bindgen_prelude::{
10+
Function,
11+
Unknown,
12+
},
13+
threadsafe_function::UnknownReturnValue,
14+
};
815
use napi_derive::napi;
916

1017
mod discord;
@@ -13,11 +20,15 @@ mod model;
1320
mod sys_media;
1421

1522
use model::{
16-
DiscordConfigPayload, MetadataPayload, PlayModePayload, PlayStatePayload, TimelinePayload,
23+
DiscordConfigPayload,
24+
MetadataParam,
25+
MetadataPayload,
26+
PlayModePayload,
27+
PlayStatePayload,
28+
SystemMediaEvent,
29+
TimelinePayload,
1730
};
1831

19-
use crate::model::{MetadataParam, SystemMediaEvent};
20-
2132
/// 初始化插件
2233
///
2334
/// ### 参数
@@ -167,7 +178,8 @@ pub fn disable_discord_rpc() {
167178
///
168179
/// ### 参数
169180
///
170-
/// * `payload` - 配置信息,可以配置是否在暂停后也显示 Discord Activity 和 状态显示风格。详情请查看 [`DiscordConfigPayload`]
181+
/// * `payload` - 配置信息,可以配置是否在暂停后也显示 Discord Activity 和 状态显示风格。详情请查看
182+
/// [`DiscordConfigPayload`]
171183
#[napi]
172184
pub fn update_discord_config(payload: DiscordConfigPayload) {
173185
discord::update_config(payload);

native/external-media-integration/src/logger.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
use anyhow::{Context, Result};
2-
use std::fs;
3-
use std::path::PathBuf;
4-
use std::sync::OnceLock;
5-
use tracing::{error, trace};
6-
use tracing_appender::non_blocking::WorkerGuard;
7-
use tracing_appender::rolling::RollingFileAppender;
8-
use tracing_subscriber::{filter::LevelFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
1+
use std::{
2+
fs,
3+
path::PathBuf,
4+
sync::OnceLock,
5+
};
6+
7+
use anyhow::{
8+
Context,
9+
Result,
10+
};
11+
use tracing::{
12+
error,
13+
trace,
14+
};
15+
use tracing_appender::{
16+
non_blocking::WorkerGuard,
17+
rolling::RollingFileAppender,
18+
};
19+
use tracing_subscriber::{
20+
filter::LevelFilter,
21+
fmt,
22+
layer::SubscriberExt,
23+
util::SubscriberInitExt,
24+
};
925

1026
static LOG_GUARD: OnceLock<WorkerGuard> = OnceLock::new();
1127

native/external-media-integration/src/model.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ impl fmt::Debug for MetadataPayload {
7575
}
7676

7777
#[napi(object)]
78-
#[allow(clippy::doc_markdown)]
7978
pub struct MetadataParam {
8079
pub song_name: String,
8180
pub author_name: String,
@@ -99,7 +98,8 @@ pub struct MetadataParam {
9998

10099
/// 当前歌曲时长,单位是毫秒
101100
///
102-
/// 用于 Linux、MacOS、Discord RPC 的元数据更新。Windows 使用 [`TimelinePayload`] 的 `total_time` 字段。
101+
/// 用于 Linux、MacOS、Discord RPC 的元数据更新。Windows 使用 [`TimelinePayload`] 的
102+
/// `total_time` 字段。
103103
pub duration: Option<f64>,
104104
}
105105

@@ -117,6 +117,9 @@ impl From<MetadataParam> for MetadataPayload {
117117
}
118118
}
119119

120+
// 使用 string_enum 加上 --no-const-enum 编译参数可以神奇地让 napi-rs
121+
// 把枚举生成为字符串联合类型,这样就可以直接从 index.d.ts 导入它们而不用再复制一份了
122+
120123
#[napi(string_enum)]
121124
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
122125
pub enum PlaybackStatus {
@@ -157,9 +160,8 @@ pub struct PlayModePayload {
157160

158161
/// Discord 显示模式枚举
159162
///
160-
/// 控制 Discord 左下角 "正在听 XXX" 的显示内容
163+
/// 不打开详细信息面板时,在用户名下方显示的小字
161164
#[napi(string_enum)]
162-
#[allow(clippy::doc_markdown)]
163165
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
164166
pub enum DiscordDisplayMode {
165167
/// Listening to SPlayer
@@ -175,7 +177,10 @@ pub enum DiscordDisplayMode {
175177
#[derive(Debug, Clone)]
176178
pub struct DiscordConfigPayload {
177179
/// 暂停时是否显示
180+
///
181+
/// 注意暂停时进度会固定为 0
178182
pub show_when_paused: bool,
179-
/// 显示模式
183+
184+
/// 显示模式,参考 [`DiscordDisplayMode`]
180185
pub display_mode: Option<DiscordDisplayMode>,
181186
}

native/external-media-integration/src/sys_media/linux.rs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,56 @@
1-
use std::io::Write;
2-
use std::sync::{Arc, RwLock};
3-
use std::time::{SystemTime, UNIX_EPOCH};
4-
use std::{process, thread};
1+
use std::{
2+
io::Write,
3+
process,
4+
sync::{
5+
Arc,
6+
RwLock,
7+
},
8+
thread,
9+
time::{
10+
SystemTime,
11+
UNIX_EPOCH,
12+
},
13+
};
514

615
use anyhow::Result;
7-
use mpris_server::zbus::zvariant::ObjectPath;
816
use mpris_server::{
9-
LoopStatus as MprisLoopStatus, Metadata, PlaybackStatus as MprisPlaybackStatus, Player, Time,
17+
LoopStatus as MprisLoopStatus,
18+
Metadata,
19+
PlaybackStatus as MprisPlaybackStatus,
20+
Player,
21+
Time,
22+
zbus::zvariant::ObjectPath,
1023
};
1124
use napi::threadsafe_function::ThreadsafeFunctionCallMode;
1225
use tempfile::NamedTempFile;
13-
use tokio::runtime::Runtime;
14-
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel};
15-
use tracing::{debug, error};
16-
17-
use crate::model::{PlaybackStatus, RepeatMode, SystemMediaEventType};
26+
use tokio::{
27+
runtime::Runtime,
28+
sync::mpsc::{
29+
UnboundedReceiver,
30+
UnboundedSender,
31+
unbounded_channel,
32+
},
33+
};
34+
use tracing::{
35+
debug,
36+
error,
37+
};
1838

19-
use super::{
20-
MetadataPayload, PlayModePayload, PlayStatePayload, SystemMediaControls, SystemMediaEvent,
21-
SystemMediaThreadsafeFunction, TimelinePayload,
39+
use crate::{
40+
model::{
41+
MetadataPayload,
42+
PlayModePayload,
43+
PlayStatePayload,
44+
PlaybackStatus,
45+
RepeatMode,
46+
SystemMediaEvent,
47+
SystemMediaEventType,
48+
TimelinePayload,
49+
},
50+
sys_media::{
51+
SystemMediaControls,
52+
SystemMediaThreadsafeFunction,
53+
},
2254
};
2355

2456
/// 主线程和后台 D-Bus 现成通信的指令

0 commit comments

Comments
 (0)