Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into scarb-2.8.2-standalon…
Browse files Browse the repository at this point in the history
…e-plugin
  • Loading branch information
piwonskp committed Sep 23, 2024
2 parents 6b8a6e5 + 8d346ba commit 93e5440
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 18 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb 2.4.4
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ cairo-lang-defs = "2.8.2"
cairo-lang-diagnostics = "2.8.2"
cairo-lang-parser = "2.8.2"
cairo-lang-syntax = "2.8.2"
regex = "1.10.6"
regex = "1.10.6"
36 changes: 28 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
# Hello cairo plugin

The repository demonstrates the usage of cairo plugin system. It contains the plugin which for each function annotated with `hello` attribute prints `Hello, <function-name>!`.

## Quickstart

### Prerequisites

Setup the cairo environment with the `asdf install` command.

### Usage

To use the plugin in your project annotate a function with `hello` attribute:

```
#[hello]
fn multiply(a: u8, b: u8) -> u8 {
a * b
}
```

An example project is located in [examples directory](./examples/cairo/).
An example project is located in [example directory](./example/).

### Execution
1. cd into cairo project directory:

2. cd into example directory:

```
cd examples/cairo/
cd example/
```

2. Run the plugin to compile project:

```
cargo run build .
```

3. Run compiled code:

```
scarb cairo-run --no-build
```

4. You should see the output:

```
Running examples
[DEBUG] 0x48656c6c6f2c206d61696e21 ('Hello, main!')
Expand All @@ -34,9 +51,12 @@ scarb cairo-run --no-build
[DEBUG] 0x48656c6c6f2c20666f6f21 ('Hello, foo!')
Run completed successfully, returning []
```

## Project structure
The package consists of two parts: business logic and boilerplate code. Boilerplate code is stored in `src/main.rs` and `src/boilerplate` directory. Business logic is stored in `src/plugin`.
Plugin directory contains three files:
* `config.rs` contains base properties of the plugin - its name, version and repository URL.
* `macro_plugin.rs` contains the plugin configuration. This is the place where you map item type to the function that processes it.
* `insert_hello.rs` contains the function that inserts hello message.

The package consists of two parts: business logic and boilerplate code. Boilerplate code is stored in `src/main.rs` and `src/boilerplate` directory. Business logic is stored in `src/plugin`.
Plugin directory contains three files:

- `config.rs` contains base properties of the plugin - its name, version and repository URL.
- `macro_plugin.rs` contains the plugin configuration. This is the place where you map item type to the function that processes it.
- `insert_hello.rs` contains the function that inserts hello message.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/cairo/Scarb.lock → example/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
version = 1

[[package]]
name = "examples"
name = "example"
version = "0.1.0"
dependencies = [
"hello_plugin",
Expand Down
7 changes: 4 additions & 3 deletions examples/cairo/Scarb.toml → example/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[package]
name = "examples"
name = "example"
version = "0.1.0"
cairo-version = "2.3.1"
edition = '2023_10'

[lib]
casm = true
sierra = true

[dependencies]

## Referencing using local package:
hello_plugin = { path = "../../" }
hello = { path = "../" }

## Referencing using git:
# hello = { git = "https://github.com/piwonskp/hello-cairo-plugin", crate = "hello" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@


mod m;
mod hello_module {
#[hello]
fn add(a: u8,b: u8) -> u8 {
fn add(a: u8, b: u8) -> u8 {
a + b
}
}
Expand All @@ -18,5 +16,5 @@ fn multiply(a: u8, b: u8) -> u8 {
fn main() {
hello_module::add(1, 5);
multiply(4, 7);
m::foo(6,2);
}
m::foo(6, 2);
}
File renamed without changes.
File renamed without changes.

0 comments on commit 93e5440

Please sign in to comment.