Skip to content

Commit c82be70

Browse files
authored
chore: create Recurse ML rules (#1214)
* Create rules * Don't specify explicit pathnames for effective comments
1 parent 74f686f commit c82be70

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

.recurseml.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules: .rules/

.rules/effective_comments.mdc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
description: Effective Code Comments
3+
globs: "*"
4+
alwaysApply: true
5+
---
6+
7+
8+
For more context read: https://blog.codinghorror.com/code-tells-you-how-comments-tell-you-why/
9+
10+
- NEVER write comments for code that can be understood from the code itself
11+
- Avoid redundant comments that restate the obvious
12+
- Use comments to clarify the intent behind complex logic or decisions
13+
- Use comments when non-obvious assumptions are made
14+
- Keep TODO comments specific with assignees when possible
15+
- ALWAYS update comments when the underlying code changes
16+
17+
18+
SCOPE: this only applies to code comments blocks NOT executable code (such as logging statements)
19+
20+
# Solution
21+
22+
Fix redundant comments by removing them completely.
23+
Never attempt to add additional clarification to the comments.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
description: Exhaustive Pattern Matching with match
3+
globs: "**/*.rs"
4+
alwaysApply: true
5+
---
6+
7+
For more context read: https://doc.rust-lang.org/book/ch06-02-match.html
8+
9+
- ALWAYS use `match` instead of nested `if-else` chains for enum handling
10+
- Use `match` for exhaustive pattern matching to prevent runtime panics
11+
- Add match guards (`if` conditions) for conditional patterns within matches
12+
- Handle all variants explicitly - avoid catch-all patterns when possible
13+
- Use `match` for HTTP methods, response types, and middleware returns in Robyn
14+
- Pattern match on `Option` and `Result` types instead of using `unwrap()`
15+
16+
SCOPE: This applies to Rust enum handling, especially for HttpMethod, ResponseType, and MiddlewareReturn types in the Robyn codebase.
17+
18+
# Solution
19+
20+
Replace nested if-else chains with match statements:
21+
22+
```rust
23+
// Instead of:
24+
if method == HttpMethod::GET {
25+
handle_get()
26+
} else if method == HttpMethod::POST {
27+
handle_post()
28+
} else {
29+
handle_other()
30+
}
31+
32+
// Use:
33+
match method {
34+
HttpMethod::GET => handle_get(),
35+
HttpMethod::POST => handle_post(),
36+
_ => handle_other(),
37+
}
38+
```
39+
40+
Use match guards for conditional logic:
41+
42+
```rust
43+
match response_type {
44+
ResponseType::Standard(resp) if resp.status_code >= 400 => handle_error(resp),
45+
ResponseType::Standard(resp) => handle_success(resp),
46+
ResponseType::Streaming(stream) => handle_stream(stream),
47+
}
48+
```

.rules/proper_error_handling.mdc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
description: Proper Error Handling with Result and ? Operator
3+
globs: "**/*.rs"
4+
alwaysApply: true
5+
---
6+
7+
For more context read: https://doc.rust-lang.org/book/ch09-00-error-handling.html
8+
9+
- NEVER use `unwrap()` or `expect()` in production code without clear justification
10+
- Use the `?` operator for error propagation in functions returning `Result`
11+
- Handle `Result` and `Option` types explicitly with `match` or combinators
12+
- Create custom error types for domain-specific errors in Robyn
13+
- Use `map_err()` to transform errors when interfacing with PyO3
14+
- Log errors at appropriate levels before returning them
15+
- Return meaningful error messages for HTTP endpoints
16+
17+
WHY THIS MATTERS IN ROBYN:
18+
Robyn is a high-performance web framework that interfaces between Python and Rust. Poor error handling can:
19+
- Crash the entire web server with a single panic
20+
- Lose critical error information when interfacing with Python
21+
- Provide unhelpful error messages to API consumers
22+
- Make debugging production issues nearly impossible
23+
- Break the Python-Rust bridge unexpectedly
24+
25+
SCOPE: This applies to all Rust error handling, especially PyO3 integration, file I/O, and HTTP request processing in the Robyn codebase.
26+
27+
# Solution
28+
29+
Replace unwrap() with proper error handling:
30+
31+
```rust
32+
// Instead of:
33+
let value = some_operation().unwrap();
34+
35+
// Use:
36+
let value = some_operation()?;
37+
// Or handle explicitly:
38+
let value = match some_operation() {
39+
Ok(val) => val,
40+
Err(e) => {
41+
log::error!("Operation failed: {}", e);
42+
return Err(format!("Operation failed: {}", e));
43+
}
44+
};
45+
```
46+
47+
Use map_err for PyO3 integration:
48+
49+
```rust
50+
// Transform errors when interfacing with Python
51+
let result = python_operation()
52+
.map_err(|e| format!("Python operation failed: {}", e))?;
53+
```
54+
55+
Handle file operations safely:
56+
57+
```rust
58+
// Instead of:
59+
let content = std::fs::read_to_string(path).unwrap();
60+
61+
// Use:
62+
let content = std::fs::read_to_string(path)
63+
.map_err(|e| format!("Failed to read file {}: {}", path, e))?;
64+
```

0 commit comments

Comments
 (0)