Skip to main content

GridColumns

Derive Macro GridColumns 

#[derive(GridColumns)]
{
    // Attributes available to this derive:
    #[grid_columns]
    #[grid_column]
}
Expand description

Derives GridColumns for an enum of columns (feature derive). Derives GridColumns for an enum of columns (feature derive of gpui-kit-grid).

Each unit variant is a column. Declaration order is the schema order: the order of all() and the index ix(). The column id, which saved layouts store, is the variant name in snake_case unless an attribute sets it.

use gpui_kit_grid::GridColumns;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, GridColumns)]
enum TradeColumn {
    Id,         // "id"
    Instrument, // "instrument"
    BidSize,    // "bid_size"
    // A saved layout's id, kept after a rename.
    #[grid_column(id = "px")]
    Price,
}

assert_eq!(TradeColumn::BidSize.id(), "bid_size");
assert_eq!(TradeColumn::Price.id(), "px");
assert_eq!(TradeColumn::Price.ix().get(), 3);
assert_eq!(TradeColumn::all()[3], TradeColumn::Price);

The expansion is the implementation one would write by hand: all lists the variants, id and ix match on them. ix follows declaration order rather than the discriminant, so Id = 10 does not shift it. With the same names, the ids, the order and the indices are those of a hand-written implementation that spells its ids in snake_case, so moving to the derive keeps saved layouts.

§Attributes

AttributeWhereValueDefault
id = "…"#[grid_column(…)] on a variantThe column id: not empty, unique in the enumThe variant name by rename_all
rename_all = "…"#[grid_columns(…)] on the enum"snake_case", "kebab-case", "camelCase", "PascalCase" (the name as written) or "lowercase""snake_case"
crate = "…"#[grid_columns(…)] on the enumThe path of the grid crate when it is renamed or re-exported, such as "my_ui::grid"::gpui_kit_grid

Names split into words as the heck crate splits them: BidSize is bid_size, HTTPStatus is http_status and Leg2Price is leg2_price.

A variant may carry #[deprecated] (the expansion does not warn) and #[cfg(…)]: indices count the variants of the build, while ids do not depend on it, so a saved layout survives a feature switch.

§Errors

The derive refuses, pointing at the part at fault:

  • a struct or a union, and an enum with generic parameters;
  • a variant with fields, an enum without variants and a 129th variant (a grid has at most 128 columns);
  • an empty or repeated column id;
  • an unknown or repeated key, and an unknown rename_all rule.

It reports every error of the enum at once. The enum derives Clone, Copy, Debug, PartialEq, Eq and Hash itself: GridColumns requires them and the derive does not add them.

ⓘ
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, GridColumns)]
enum TradeColumn {
    Price,
    // error: the column id `price` is already used by `Price`
    #[grid_column(id = "price")]
    Px,
}