mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-01 10:40:14 +08:00
89 lines
5.0 KiB
Markdown
89 lines
5.0 KiB
Markdown
# Workspace Crate Architecture
|
|
|
|
## Choose the boundary
|
|
|
|
- Apply Functional Core, Imperative Shell and Ports and Adapters. Keep domain
|
|
types, invariants, parsing, validation, and deterministic transitions
|
|
independent of Tauri, concrete OS APIs, and implicit application state.
|
|
- Keep use cases, async coordination, cancellation, and timeouts in
|
|
framework-independent application modules. Pass dependencies explicitly.
|
|
Define ports where an external effect needs a contract. Adapters implement
|
|
the contracts owned by their consumers. Keep filesystem, network, IPC, process,
|
|
and OS-specific operations in their adapters.
|
|
- Extract a capability whose invariants can be owned behind a small API. Before
|
|
creating a crate, identify its owner, what callers supply, and what can be
|
|
verified through the crate API. Extract a crate when it provides a useful
|
|
dependency, portability, testing, or ownership boundary. Keep a module when
|
|
the proposed boundary only relocates implementation or introduces a
|
|
pass-through API.
|
|
- Keep capability crates independent by default. Compose peer capabilities in
|
|
the application and pass inputs or callbacks through the consumer's API.
|
|
Depend on another workspace crate for foundational mechanisms such as logging
|
|
and task primitives, or for a shared domain contract required to enforce the
|
|
same invariant. Keep tightly coupled rules together when separating them
|
|
would duplicate that contract or split its ownership.
|
|
- Keep dependencies pointing toward domain and application contracts. Resolve
|
|
app paths and read current settings at the application boundary. Pass in the
|
|
required values, resources, or callbacks. For example,
|
|
[logging](clash-verge-logging/src/lib.rs) owns its pipeline and writers while the
|
|
app supplies directories and limits through `LoggerConfig`.
|
|
- Run framework-independent tests without starting the desktop app. A platform
|
|
adapter can own runtime startup when its resource lifetime requires it, as
|
|
[signal](clash-verge-signal/src/lib.rs) does for Linux shutdown.
|
|
|
|
## Design the API
|
|
|
|
- Start with concrete types for data and outcomes. Use a trait when methods must
|
|
cooperate under one contract, a closure for one supplied operation, and
|
|
`impl Future` when the caller owns scheduling. Add type parameters and bounds
|
|
where the contract needs them. Use dynamic dispatch when the API needs to hold
|
|
heterogeneous implementations, as [draft](clash-verge-draft/src/lib.rs) does
|
|
for the layers claimed by `DraftTransaction`.
|
|
- Keep state and its invariants with one owner. Expose snapshots, typed
|
|
operations, or guards. Draft owns claim, commit, and rollback of its in-memory
|
|
layers. The caller owns persistence and the recovery of external effects.
|
|
- Name product workspace libraries with the `clash-verge-` prefix and inherit
|
|
workspace lints. Reuse workspace dependencies where available and declare
|
|
only the features the crate needs.
|
|
|
|
## Handle fallible input
|
|
|
|
- Define crate-owned error types with `thiserror` in a dedicated `error` module.
|
|
Re-export public errors through their established import paths.
|
|
- Crates may use `unwrap()` or `expect()` for internal invariants. Propagate
|
|
errors from required external data and use defaults only when the input
|
|
contract gives the default a meaning.
|
|
|
|
## Own background work
|
|
|
|
- Give ordered start, replacement, and cleanup to one task owner. Reuse
|
|
[`clash-verge-task`](clash-verge-task/src/lib.rs) for coalescing scheduled jobs
|
|
and replaceable sessions. Keep domain policy, execution callbacks, and runtime
|
|
startup at the call site. Select a driver whose delivery guarantees match the
|
|
caller's needs.
|
|
- Use typed inputs. Coalesce only when intermediate requests may be skipped.
|
|
Reply with the actual outcome when a caller waits for completion. Preserve
|
|
transaction boundaries and keep the reply path free of locks held by the
|
|
waiting caller. Cancel only at safe boundaries and own issued effects through
|
|
completion or explicit reconciliation.
|
|
- Distinguish requested effects from completed effects in typed outcomes.
|
|
Define how failures, cancellation, and timeouts affect publication, retry,
|
|
rollback, or reconciliation.
|
|
|
|
## Validate new crates
|
|
|
|
- Fuzz the public boundary before tuning. Exercise malformed input, fallback
|
|
behavior, and failures that can affect user data. Preserve the corpus and
|
|
record the baseline revision and dependency resolution.
|
|
- Try at least one targeted performance improvement. Compare the baseline and
|
|
candidate on the same workload and build settings. Record stable gains, flat
|
|
cases, and regressions, and justify which changes to keep.
|
|
- Fuzz the final candidate again. Use the baseline corpus and compare outputs,
|
|
diagnostics, and errors with the baseline where behavior should be preserved.
|
|
|
|
Verify the extracted contract through its public API when practical. Use
|
|
`cargo test -p <crate>` for focused tests and keep native behavior checks at the
|
|
app boundary. For timing, prefer Tokio's paused clock for task drivers or a
|
|
small clock interface where time is an input, as in
|
|
[`clash-verge-limiter`](clash-verge-limiter/src/lib.rs).
|