セクション 6 / 25
スライス
目次
スライスは製品の1つの機能です。名前はsnake_caseの単数形の名詞です:recording、transcript、board、trigger。recordings_managerやutilsではありません。製品にフロントエンドがある場合、スライス名はフロントエンドの機能フォルダーと一致させます。そうすれば、同じ単語で両側のコードが見つかります。
スライスは2つのティアに存在します。ドメインはcoreに、エントリ(コマンド、DTO、イベント)はappにあります。
6.1. coreのスライス
core/src/<slice>/
mod.rs public API of the slice: only `mod …;` and `pub use …;`
model.rs domain types, state enums, identifiers; no I/O, no async
logic.rs pure functions: validation, decisions, mappings, calculations
store.rs this slice's reads from SQL and files, and its write commands through the writer
service.rs use cases: async, composes logic + store + drivers; no state of its own
error.rs optional: the slice's error enum
actor/ only for a slice with background state, section 9
tests.rs optional: unit tests of the slice
ルール:
- スライスが必要としないファイルは存在しません。 小さなスライスは、およそ400行を超えるまで、モデルとサービスを含む1つの
mod.rsだけで構いません。 - 大きくなったファイルは同じ名前のフォルダーになります。
logic.rsはlogic/mod.rsとlogic/<topic>.rsになります。役割は変わらず、ファイルの数だけが変わります。 - 大きくなりすぎたスライスはサブスライスに分かれます。
run/はrun/start/、run/step/、run/finish/になり、それぞれ同じファイル構成を持ちます。親には再エクスポートを書いたmod.rsだけが残ります。 mod.rsはスライスの玄関です。 Nxライブラリのindex.tsに相当します。ほかのスライスはここを通してのみインポートします。
典型的なmod.rsです。
mod logic;
mod model;
mod service;
mod store;
pub use model::{Transcript, TranscriptId, Word};
pub use service::TranscriptService;
6.2. appのスライス
app/src/<slice>/
mod.rs `mod …;` and `pub mod commands;` (the command list itself is in lib.rs, 6.4)
commands.rs #[tauri::command] functions; at most 10 lines each
dto.rs wire types: serde camelCase, #[serde(other)] on enums, From<model>, From<error> for IpcError
events.rs event names and payloads sent to the window; snapshot subscription loops
app crateの残りの部分です。
app/src/
main.rs calls lib::run(); nothing else
lib.rs run(): Builder, plugins, setup, generate_handler! with every command, grouped by slice
state.rs AppState: the composition root, section 11
error.rs entry error → code and text for the window
platform.rs optional: window and menu settings that depend on the operating system, section 12.3
doors/ entries other than the window: MCP server, bridge for agents, remote hub
6.3. コマンドの基準形
#[tauri::command]
pub async fn transcript_search(
state: State<'_, AppState>,
input: SearchInput,
) -> Result<Vec<TranscriptHitDto>, IpcError> {
let hits = state.transcripts.search(input.into()).await?;
Ok(hits.into_iter().map(Into::into).collect())
}
コマンドにif、ループ、スライスへの2回の呼び出しが必要なら、そのロジックはservice.rsかlogic.rsに属します。10行という制限は書き方の好みの問題ではありません。変換だけを行うコマンドには、専用の単体テストが要りません。そのロジックはスライスでテストされているからです。残る証明は人が目にする文であり、それはエントリを通したエンドツーエンドでテストします(セクション14)。
6.4. コマンドの登録
tauri::generate_handler!にはすべてのコマンドのフルパスが必要なので、lib.rsに1つのリストを置きます。リストはスライスごとに1ブロックずつまとめておきます。そうすれば、コマンドの追加は隣のコマンドの横に1行足すだけで済みます。
pub fn run() {
tauri::Builder::default()
.setup(setup)
.invoke_handler(tauri::generate_handler![
recording::commands::recording_start,
recording::commands::recording_stop,
recording::commands::recording_list,
transcript::commands::transcript_read,
transcript::commands::transcript_search,
board::commands::board_list,
board::commands::board_sync,
])
.run(tauri::generate_context!())
.expect("the window could not start");
}
スライスに存在していてもこのリストにないコマンドは、コンパイルは通りますが、黙って何もしません。セクション16には、両者を比較し、ずれたときに失敗する小さなチェックが含まれています。
6.5. スライスの命名
- 1つの機能に1つの名詞、単数形で:
triggersではなくtrigger。 - 技術の名前ではなく、ユーザーが認識するものの名前で:
whisper_pipelineではなくtranscript。 - 2つの機能がいつも一緒に変わるなら、それは1つのスライスです。1つの機能に独立して変わる部分があるなら、それはサブスライスです。
- 横断的なポリシーもスライスです。 アクセス制御、制限、予算、シークレットの保護は
core/<policy>/(たとえばcore/guard/)に置き、ほかのすべてのスライスがそれを呼び出します。