mcpField
R
rust-cli-agent-style

rust-cli-agent-style

github.com/pproenca开发21857 次安装11 周前更新
代码仓库

关于 rust-cli-agent-style

基于 OpenAI Codex CLI 与 Agent 代码库整理的 Rust 编码规范技能,适用于构建生产级 Rust 命令行工具或 Agent 系统,涵盖错误处理、工作区、异步代码、命名和风格。

主要功能

  1. 01错误处理规范
  2. 02工作区组织方式
  3. 03异步与 Trait 模式
  4. 04命名约定
  5. 05代码风格与 lint 指引
  6. 06跨 crate 依赖实践

适用场景

  1. 01构建 Rust 命令行工具
  2. 02开发 Rust Agent 系统
  3. 03组织 Rust 工作区
  4. 04审查生产级 Rust 代码

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/pproenca/dot-skills --skill rust-cli-agent-style

安装后在 Claude Code 中输入/rust-cli-agent-style确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

AGENTS.md
README.md
metadata.json
assets/templates/
_template.md
references/
_sections.md
cross-workspace-deps.md
cross-workspace-lints.md
err-anyhow-application.md
err-context-chain.md
err-doc-errors.md
err-from-derive.md
err-io-result.md
err-map-err-conversion.md
err-no-expect.md
err-no-unwrap.md
err-structured-variants.md
err-thiserror-domain.md
err-transparent.md
mod-async-trait-macro.md
mod-builder-pattern.md
mod-derive-order.md
mod-extension-trait-suffix.md
mod-generic-constraints.md
mod-impl-block-order.md
mod-jsonschema-derive.md
mod-newtype-pattern.md
mod-serde-rename.md
mod-struct-visibility.md
mod-trait-bounds.md
mod-type-alias-complex.md

详细介绍

OpenAI Codex Rust CLI Agent 最佳实践

本技能将教你按照 OpenAI Codex 代码库的风格编写 Rust 代码——这是一个生产级 CLI/Agent 系统,包含 50 个 crate 和 787 个 Rust 文件。

主要特点

  • Edition 2024,并采用严格的 Clippy 配置
  • 非测试代码中不使用 unwrap/expect(在工作区级别强制执行)
  • Tokio 异步运行时,并正确设置 Send + Sync 约束
  • 库错误使用 thiserror,应用程序代码使用 anyhow
  • 采用扁平工作区结构,集中管理依赖项

适用场景

在以下场景中应用此技能:

  • 使用 Rust 构建 CLI 工具或 Agent 系统
  • 使用 Tokio 编写 Rust 异步代码
  • 设计 Rust 工作区组织结构
  • 实现错误处理模式
  • 开发生产级 Rust 代码库

快速参考

关键规则(必须遵守)
规则说明
err-no-unwrap非测试代码中绝不使用 unwrap()
err-no-expect库代码中避免使用 expect()
err-thiserror-domain使用 thiserror 处理领域错误
err-context-chain使用 .context() 为错误添加上下文
错误处理
规则说明
err-anyhow-application入口点使用 anyhow::Result
err-from-derive使用 #[from] 进行错误转换
err-transparent对包装错误使用 #[error(transparent)]
err-structured-variants在错误变体中包含相关数据
err-io-resultI/O 函数使用 std::io::Result
err-map-err-conversion使用 map_err 进行错误转换
err-doc-errors说明错误条件
组织结构
规则说明
org-workspace-flat使用 utils 子目录的扁平工作区
org-crate-naming目录使用 kebab-case,并添加项目名称前缀
org-module-visibility内部 API 使用 pub(crate)
org-test-common-crate使用共享测试工具 crate
org-integration-tests-suite在 suite 目录中存放测试
org-feature-modules按功能组织模块
org-handlers-subdir在专用子目录中存放处理器
org-errors-file在专用文件中存放错误定义
组件模式
规则描述
mod-derive-order统一 derive 宏的顺序
mod-async-trait-macro对异步 trait 使用 #[async_trait]
mod-trait-bounds并发 trait 使用 Send + Sync + 'static
mod-extension-trait-suffix扩展 trait 使用 Ext 后缀
mod-builder-pattern复杂配置使用 Builder 模式
mod-type-alias-complex复杂泛型使用类型别名
mod-impl-block-order统一 impl 块的顺序
mod-generic-constraints复杂约束使用 Where 子句
mod-newtype-pattern使用 Newtype 确保类型安全
mod-struct-visibility字段私有,构造函数公开
mod-serde-rename为线格式使用 Serde rename
mod-jsonschema-deriveAPI 类型使用 JsonSchema
命名约定
规则描述
name-async-no-suffix异步函数不使用 _async 后缀
name-try-prefix-fallible可失败的构造函数使用 try_ 前缀
name-with-prefix-builderBuilder 方法使用 with_ 前缀
name-handler-suffix处理器使用 Handler 后缀
name-error-suffix错误类型使用 Error 后缀
name-result-type-aliasCrate 专用的 Result 别名
name-const-env-var环境常量使用 _ENV_VAR 后缀
name-request-responseRequest/Response 类型配对
name-options-suffix配置集合使用 Options 后缀
name-info-suffix只读数据使用 Info 后缀
name-provider-suffix服务使用 Provider 后缀
name-client-suffixAPI 客户端使用 Client 后缀
name-manager-suffix生命周期管理使用 Manager 后缀
name-bool-is-prefix布尔值使用 is_/has_/should_ 前缀
name-plural-collections集合使用复数名称
风格
规则描述
style-import-granularity每条 use 语句导入一个项目
style-deny-stdout库中禁止使用 stdout/stderr
style-inline-format-args内联 format 参数
style-module-docs模块级文档
style-expect-reason使用 #[expect] 时注明 lint 原因
style-cfg-test-module在 mod tests 中编写单元测试
跨 Crate
规则描述
cross-workspace-lints工作区级 lint 配置
cross-workspace-deps集中管理依赖版本

示例:正确处理错误

use thiserror::Error;
use anyhow::Context;

// Domain error with thiserror
#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("failed to read config file: {path}")]
    ReadFailed {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },

    #[error(transparent)]
    Parse(#[from] toml::de::Error),
}

// Library function returns domain error
pub fn load_config(path: &Path) -> Result<Config, ConfigError> {
    let content = fs::read_to_string(path)
        .map_err(|source| ConfigError::ReadFailed {
            path: path.to_owned(),
            source,
        })?;
    toml::from_str(&content).map_err(Into::into)
}

// Application code uses anyhow with context
fn main() -> anyhow::Result<()> {
    let config = load_config(Path::new("config.toml"))
        .context("failed to load configuration")?;
    run(config).await
}

来源

模式提取自 OpenAI Codex(codex-rs/ 子目录)——一个用于生产环境的 Rust 代码库,包含 50 个 crate 和 787 个 Rust 文件。

评论

支持 Markdown