docs: 逐模块细化中文逻辑注释并覆盖测试与示例

This commit is contained in:
2026-11-12 10:31:35 +08:00
parent d29a1be329
commit cd23e70db1
123 changed files with 2664 additions and 249 deletions
+6 -1
View File
@@ -4,9 +4,10 @@
//! Clipboard text is always UTF-8, including Finder/LaunchServices launches without LANG.
use anyhow::{Result, bail};
#[cfg(target_os = "macos")]
/// 运行选定平台的剪贴板命令并检查其退出状态。
/// 构造固定 UTF-8 环境的系统命令;本函数不启动进程,退出状态由 read/write 检查。
fn command(program: &str) -> std::process::Command {
let mut cmd = std::process::Command::new(program);
// 从 Finder 等无 LANG 的环境启动时也显式指定 UTF-8,防止系统工具按其他编码转换中文。
cmd.env("LC_ALL", "en_US.UTF-8")
.env("LC_CTYPE", "en_US.UTF-8")
.env("LANG", "en_US.UTF-8");
@@ -16,6 +17,7 @@ fn command(program: &str) -> std::process::Command {
pub fn read() -> Result<String> {
#[cfg(target_os = "macos")]
{
// 直接启动固定路径程序并传递独立参数,不经过 shell;-Prefer txt 要求文本表示。
let output = command("/usr/bin/pbpaste")
.arg("-Prefer")
.arg("txt")
@@ -36,6 +38,7 @@ pub fn write(value: &str) -> Result<()> {
#[cfg(target_os = "macos")]
{
use std::io::Write;
// 正文只经子进程标准输入传递,不拼成命令参数,因此引号、换行和源码中的命令字符保持原意。
let mut child = command("/usr/bin/pbcopy")
.stdin(std::process::Stdio::piped())
.spawn()?;
@@ -44,6 +47,7 @@ pub fn write(value: &str) -> Result<()> {
.take()
.ok_or_else(|| anyhow::anyhow!("clipboard stdin is unavailable"))?;
stdin.write_all(value.as_bytes())?;
// 写完后先关闭管道发送 EOF,让 pbcopy 知道输入结束;之后等待退出状态,不能仅以写管道成功作为完成。
drop(stdin);
if !child.wait()?.success() {
bail!("system clipboard write failed");
@@ -52,6 +56,7 @@ pub fn write(value: &str) -> Result<()> {
}
#[cfg(not(target_os = "macos"))]
{
// 非 macOS 当前没有实现适配器,显式报错而不伪装成写入成功。
let _ = value;
bail!("system clipboard adapter is not available on this platform")
}