Skip to content

Commit

Permalink
add some Rust patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Mar 9, 2024
1 parent fd60818 commit 3d7394c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
13 changes: 12 additions & 1 deletion .grit/grit.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
version: 0.0.1
patterns:
- name: github.com/getgrit/stdlib#*
- name: github.com/getgrit/stdlib#*
- name: no_println_in_lsp
description: Don't use println!() in LSP code, it breaks the LSP stdio protocol.
level: error
body: |
engine marzano(0.1)
language rust
`println!($_)` => . where {
$filename <: not includes "test.rs",
$absolute_filename <: includes "apps/marzano/lsp",
}
23 changes: 23 additions & 0 deletions .grit/patterns/flushable_unwrap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Force flushing

Prevent returning without flushing the emitter in `apply_pattern.rs`.

```grit
language rust
pattern flushable_return() {
`$expression?` where {
$expression <: not includes "flush",
$expression <: not within `let mut emitter: MessengerVariant = $_;`,
$expression => `flushable_unwrap!(emitter, $expression)`
}
}
file(name=includes "apply_pattern.rs", $body) where {
$body <: contains `pub(crate) async fn run_apply_pattern(
$_
) -> Result<()> { $func }` where {
$func <: contains flushable_return()
}
}
```
28 changes: 28 additions & 0 deletions .grit/patterns/no_panic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
level: info
---

# Avoid panic

Panics should be avoided in core Grit code. Instead, use `?` to propagate errors.

```grit
language rust
file($name, $body) where {
$name <: includes "marzano/lsp",
$body <: contains `$foo.unwrap()` => `$foo?`,
}
```

## Sample

```rust
// @filename: marzano/lsp/foo.rs
let x = bar().unwrap();
```

```rust
// @filename: marzano/lsp/foo.rs
let x = bar()?;
```

0 comments on commit 3d7394c

Please sign in to comment.