buster/api/.cursor/rules/libs.mdc

134 lines
3.5 KiB
Plaintext
Raw Normal View History

2025-02-14 06:05:10 +08:00
---
description: This is helpful for building libs for our web server to interact with.
2025-04-02 02:13:40 +08:00
globs: libs/**/*.{rs
2025-03-01 01:35:55 +08:00
alwaysApply: false
2025-02-14 06:05:10 +08:00
---
# Library Construction Guide
## Directory Structure
```
libs/
├── my_lib/
│ ├── Cargo.toml # Library-specific manifest
│ ├── src/
│ │ ├── lib.rs # Library root
2025-04-02 02:13:40 +08:00
│ │ ├── types.rs # Data structures and types
2025-02-14 06:05:10 +08:00
│ │ ├── utils/ # Utility functions
│ │ └── errors.rs # Custom error types
│ └── tests/ # Integration tests
```
## Cargo.toml Template
```toml
[package]
name = "my_lib"
version = "0.1.0"
2025-03-01 01:35:55 +08:00
edition = "2021"
2025-02-14 06:05:10 +08:00
2025-03-01 01:35:55 +08:00
# Dependencies should be inherited from workspace
2025-02-14 06:05:10 +08:00
[dependencies]
2025-03-01 01:35:55 +08:00
# Use workspace dependencies
anyhow = { workspace = true }
chrono = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
uuid = { workspace = true }
diesel = { workspace = true }
diesel-async = { workspace = true }
# Add other workspace dependencies as needed
2025-02-14 06:05:10 +08:00
# Development dependencies
[dev-dependencies]
2025-03-01 01:35:55 +08:00
tokio-test = { workspace = true }
# Add other workspace dev dependencies as needed
2025-02-14 06:05:10 +08:00
2025-03-01 01:35:55 +08:00
# Feature flags
2025-02-14 06:05:10 +08:00
[features]
default = []
2025-03-01 01:35:55 +08:00
# Define library-specific features here
2025-02-14 06:05:10 +08:00
```
## Best Practices
### 1. Workspace Integration
2025-03-01 01:35:55 +08:00
- Use `{ workspace = true }` for common dependencies
- Never specify library-specific versions for dependencies that exist in the workspace
- All dependencies should be managed by the workspace
2025-02-14 06:05:10 +08:00
- Keep feature flags modular and specific to the library's needs
### 2. Library Structure
- Keep the library focused on a single responsibility
- Use clear module hierarchies
- Export public API through `lib.rs`
- Follow the workspace's common patterns
Example `lib.rs`:
```rust
//! My Library documentation
//!
//! This library provides...
// Re-export common workspace types if needed
pub use common_types::{Result, Error};
pub mod models;
pub mod utils;
mod errors;
// Re-exports
pub use errors::Error;
pub use models::{ImportantType, AnotherType};
```
### 3. Error Handling
- Use the workspace's common error types where appropriate
- Define library-specific errors only when needed
- Implement conversions to/from workspace error types
Example `errors.rs`:
```rust
use thiserror::Error;
use common_types::Error as WorkspaceError;
#[derive(Error, Debug)]
pub enum Error {
#[error("library specific error: {0}")]
LibrarySpecific(String),
#[error(transparent)]
Workspace(#[from] WorkspaceError),
}
```
### 4. Testing
- Follow workspace testing conventions
- Use shared test utilities from workspace when available
- Keep library-specific test helpers in the library
- Use workspace-defined test macros if available
### 5. Documentation
- Follow workspace documentation style
- Link to related workspace documentation
- Document workspace integration points
- Include examples showing workspace type usage
### 6. Integration Points
- Define clear boundaries with other workspace crates
- Use workspace traits and interfaces
- Share common utilities through workspace-level crates
- Consider cross-crate testing
### 7. Development Workflow
- Run workspace-level tests when making changes
- Update workspace documentation if needed
- Follow workspace versioning strategy
- Use workspace-level CI/CD pipelines
### 8. Dependencies
2025-03-01 01:35:55 +08:00
- All dependencies should be inherited from the workspace
- Never add library-specific dependency versions
2025-02-14 06:05:10 +08:00
- Keep dependencies minimal and focused
2025-03-01 01:35:55 +08:00
- The workspace will manage all dependency versions