Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

参与贡献

一旦您拥有 fork 和所有必需的软件,就可以开始参与了。 本指南涵盖 IDE 设置和调试。 虽然我们以 Visual Studio Code 为例,但 Martin 可以使用任何支持 Rust 的编辑器进行开发。

特定编辑器指南(点击展开)

Visual Studio Code

安装这些必要的扩展:

Vim/Neovim

使用 rustaceanvim

Emacs

使用以下任一:

RustRover

RustRover 开箱即用支持 rust

Zed

Zed 开箱即用支持 rust

快速开发设置

在深入 IDE 配置之前,请确保您的开发环境已准备就绪:

# 验证所有必需的工具已安装
just validate-tools

# 启动开发环境
just start  # 启动测试数据库
just help   # 显示常用命令

使用 launch.json 调试

通常,您需要使用特定参数或配置文件调试 martin 以修复问题或添加功能。

最方便的方法是生成 launch.json 并修改它。

生成

在键盘上按 F1,然后输入 “Generate Launch Configurations from Cargo.toml”。执行它并将其保存到您的 .vscode 目录。

修改

假设您想使用以下命令调试 Martin:

martin postgres://postgres:postgres@localhost:5411/db

您可以在 launch.json 中找到 Debug executable 'martin',如下所示:

{
    "type": "lldb",
    "request": "launch",
    "name": "Debug executable 'martin'",
    "cargo": {
        "args": [
            "build",
            "--bin=martin",
            "--package=martin"
        ],
        "filter": {
            "name": "martin",
            "kind": "bin"
        }
    },
    "args": [],
    "cwd": "${workspaceFolder}"
},

只需在其后复制并粘贴,然后像这样修改您粘贴的内容:

{
    "type": "lldb",
    "request": "launch",
    "name": "my first debug", // 随意命名
    "cargo": {
        "args": [
            "build",
            "--bin=martin",
            "--package=martin"
        ],
        "filter": {
            "name": "martin",
            "kind": "bin"
        }
    },
    "args": ["postgres://postgres:postgres@localhost:5411/db"], // 在此处添加您的参数
     "env": {
         "DEFAULT_SRID": 4490, // 在此处添加您的环境变量
     },
    "cwd": "${workspaceFolder}"
},

添加断点

转到 martin 代码中您感兴趣的任何部分并添加断点。

我们在 martin 的开始处添加一个断点。

use clap::Parser;
use log::{error, info, log_enabled};
use martin::args::{Args, OsEnv};
use martin::srv::new_server;
use martin::{read_config, Config, MartinResult};

const VERSION: &str = env!("CARGO_PKG_VERSION");

async fn start(args: Args) -> MartinResult<()> {
    info!("Starting Martin v{VERSION}");

调试

单击 Visual Studio Code 左侧面板上的 Run and Debug。选择 my first debug 并在键盘上按 F5。

等待断点被命中。