rockbox/smart_playlists/rules

Composable builder for smart-playlist rule sets. Pipe-friendly.

import rockbox/smart_playlists
import rockbox/smart_playlists/rules

let r =
  rules.all_of()
  |> rules.where("play_count", rules.Gte, rules.int(10))
  |> rules.where("last_played", rules.Within, rules.string("30d"))
  |> rules.sort("play_count", rules.Desc)
  |> rules.limit(50)

let input = smart_playlists.new("Most played", rules.to_string(r))
let assert Ok(_) = smart_playlists.create(client, input)

any_of() swaps the top-level operator from AND to OR. Mix the two by nesting builders with where_group:

let r =
  rules.all_of()
  |> rules.where("genre", rules.Eq, rules.string("Rock"))
  |> rules.where_group(
    rules.any_of()
    |> rules.where("year", rules.Gte, rules.int(2000))
    |> rules.where("year", rules.Lte, rules.int(2010)),
  )

Types

How sibling rules combine.

pub type GroupOp {
  And
  Or
}

Constructors

  • And
  • Or

Comparison operators understood by the server.

VariantMeaning
Eqequals
Neqnot equals
Gtgreater than
Gtegreater than or equal
Ltless than
Lteless than or equal
Containssubstring match
Withinduration window (e.g. "30d", "7d")
pub type Op {
  Eq
  Neq
  Gt
  Gte
  Lt
  Lte
  Contains
  Within
}

Constructors

  • Eq
  • Neq
  • Gt
  • Gte
  • Lt
  • Lte
  • Contains
  • Within

A composable rule set. Build with all_of / any_of and chain where / sort / limit. Hand to to_string to get the JSON payload the GraphQL createSmartPlaylist mutation expects.

pub opaque type Rules

Sort direction.

pub type SortDir {
  Asc
  Desc
}

Constructors

  • Asc
  • Desc

Wrapped value handed to where. Build with int / string / bool / float / null, or raw if you have a Json already.

pub opaque type Value

Values

pub fn all_of() -> Rules

Start a builder where every rule must match (logical AND).

pub fn any_of() -> Rules

Start a builder where any single rule must match (logical OR).

pub fn bool(value: Bool) -> Value

Wrap a boolean.

pub fn float(value: Float) -> Value

Wrap a float.

pub fn int(value: Int) -> Value

Wrap an integer (e.g. play counts, years, durations in ms).

pub fn limit(builder: Rules, count: Int) -> Rules

Cap the result count.

pub fn null() -> Value

JSON null.

pub fn raw(value: json.Json) -> Value

Escape hatch for arbitrary JSON values (arrays, nested objects, etc.).

pub fn sort(
  builder: Rules,
  field: String,
  direction: SortDir,
) -> Rules

Set the result ordering.

pub fn string(value: String) -> Value

Wrap a string (e.g. titles, artists, genres, duration windows).

pub fn to_json(builder: Rules) -> json.Json

Encode the builder as a Json value — useful if you’re already working with JSON values.

pub fn to_string(builder: Rules) -> String

Encode the builder as a JSON string ready for the smart-playlist mutation.

pub fn where(
  builder: Rules,
  field: String,
  op: Op,
  value: Value,
) -> Rules

Append a rule to the current group.

pub fn where_group(parent: Rules, child: Rules) -> Rules

Nest another builder underneath this one. Useful for mixing AND / OR.

Search Document