# Building a plugin

One command writes a plugin that already builds, passes the battery and says `Ready`; from there you change its manifest, its contributions and its permissions.

## Scaffold it

```sh
bun run plugin tides                  # Rust
bun run plugin tides assemblyscript   # AssemblyScript
bun run plugin tides javascript       # JavaScript
bun install
bun run index
```

`bun run plugin <name>` copies the template of the language into `plugins/<name>` and puts the name wherever the template says `template`. The name is one lowercase word of letters and digits; it refuses a keyword of Rust and a name a package of the workspace already has.

## What the folder holds

| Language | Source | The file Blossom loads |
|---|---|---|
| Rust | `src/lib.rs`, a `cdylib` crate | `target/wasm32-unknown-unknown/release/blossom_<name>.wasm` |
| AssemblyScript | `assembly/index.ts` | `build/<name>.wasm` |
| JavaScript | `source/index.ts` | `build/<name>.js`, one bundled ES module |

Every folder also carries a `package.json` that joins it to `bun run build`, `bun run lint`, `bun run test` and `bun run format`, and a `README.md` saying what it shows, what it asks for, what it reads and what it sends.

## The four exports

| Export | The host asks it | It answers |
|---|---|---|
| `describe` | Right after loading the module | The manifest — the only required one |
| `indicator` | When the user opens peek | A label and a value, or nothing |
| `command` | When the user picks a command | Nothing, or a panel |
| `panel` | When the user opens its panel | A panel |

The host never asks for what the manifest does not declare. A plugin of version 2 adds a fifth export, `wake`; see [Capabilities and limits](capabilities.md).

## The blocks of a panel

| Block | In Rust | Holds |
|---|---|---|
| Rows | `Block::Rows { rows }` | A title, an optional subtitle, an optional value and an optional state |
| Chips | `Block::Chips { chips }` | Neutral chips, each a label |
| Code | `Block::Code { text }` | The only text that may hold several lines |
| Buttons | `Block::Buttons { buttons }` | A label and a command of this plugin |
| Meters | `Block::Meters { meters }` | A label, an optional value and an optional progress |
| Links | `Block::Links { links }` | A label and the address of a page to open |

A block holds one item at least: when there is nothing to list, send a chip that says so or leave the block out.

## A plugin in full

```rust
use blossom_plugin::{
    Block, CONTRACT_VERSION, Contributions, Indicator, Panel, Permissions, Plugin, PluginManifest,
    Row,
};

#[derive(Default)]
struct Tides;

impl Plugin for Tides {
    fn describe(&self) -> PluginManifest {
        PluginManifest {
            contract_version: CONTRACT_VERSION,
            display_name: "Tides".to_owned(),
            logo: None,
            contributions: Contributions {
                has_indicator: true,
                commands: Vec::new(),
                has_panel: true,
            },
            permissions: Permissions::default(),
            settings: Vec::new(),
        }
    }

    fn indicator(&mut self) -> Option<Indicator> {
        Some(Indicator { label: "Tides".to_owned(), value: "Ready".to_owned() })
    }

    fn panel(&mut self) -> Option<Panel> {
        Some(Panel {
            title: "Tides".to_owned(),
            subtitle: None,
            blocks: vec![Block::Rows {
                rows: vec![Row {
                    title: "Ready".to_owned(),
                    subtitle: None,
                    value: None,
                    status: None,
                }],
            }],
        })
    }
}

blossom_plugin::plugin!(Tides);
```

## The manifest

`describe` returns it, and the host refuses the module whole if any part breaks the contract.

```json
{
  "contractVersion": 1,
  "displayName": "Tides",
  "contributions": {
    "indicator": true,
    "commands": [{ "commandId": "refresh", "title": "Refresh" }],
    "panel": true
  },
  "permissions": { "network": { "hosts": ["api.example.com"] } },
  "settings": [{ "key": "city", "title": "City", "default": "Lisbon" }]
}
```

- **`contractVersion`** is required: `1`, or `2` for a plugin that uses timers, alerts or a kept text.
- **`displayName`** is required; **`logo`** is optional, and one the host cannot use is dropped.
- **`contributions`** declares what the plugin draws; a `commandId` is lowercase letters, digits, dots, underscores and hyphens, 64 characters at most.
- **`permissions`** asks for the network and for files, and nothing else is reachable; see [Capabilities and limits](capabilities.md).
- **`settings`** declares up to eight lines of text the user fills in, read with `settings::values()`.

> [!NOTE]
> The manifest carries no identifier: the host names a plugin after its file, so a plugin cannot choose who it is.
