/ src / lightspeed / agent / isa.gleam
isa.gleam
 1  //// Orthogonal instruction set for the Lightspeed runtime.
 2  
 3  /// Runtime instruction.
 4  pub type Instruction {
 5    Mount(route: String, csrf_token: String)
 6    Render(view_id: String)
 7    Patch(target: String, html: String)
 8    PushEvent(name: String, payload: String)
 9    Navigate(to: String)
10    Subscribe(topic: String)
11    Unsubscribe(topic: String)
12    Shutdown(reason: String)
13  }
14  
15  /// Stable opcode string for logs, tests, and protocol mapping.
16  pub fn opcode(instruction: Instruction) -> String {
17    case instruction {
18      Mount(_, _) -> "mount"
19      Render(_) -> "render"
20      Patch(_, _) -> "patch"
21      PushEvent(_, _) -> "push_event"
22      Navigate(_) -> "navigate"
23      Subscribe(_) -> "subscribe"
24      Unsubscribe(_) -> "unsubscribe"
25      Shutdown(_) -> "shutdown"
26    }
27  }
28  
29  /// True when the instruction is intended for the browser/client runtime.
30  pub fn is_client_instruction(instruction: Instruction) -> Bool {
31    case instruction {
32      Patch(_, _) -> True
33      Navigate(_) -> True
34      _ -> False
35    }
36  }
37  
38  /// True when the instruction is intended for the server runtime.
39  pub fn is_server_instruction(instruction: Instruction) -> Bool {
40    case instruction {
41      Mount(_, _) -> True
42      Render(_) -> True
43      PushEvent(_, _) -> True
44      Subscribe(_) -> True
45      Unsubscribe(_) -> True
46      Shutdown(_) -> True
47      _ -> False
48    }
49  }
50  
51  /// Human-readable instruction summary for traces.
52  pub fn describe(instruction: Instruction) -> String {
53    case instruction {
54      Mount(route, _) -> "mount " <> route
55      Render(view_id) -> "render " <> view_id
56      Patch(target, _) -> "patch " <> target
57      PushEvent(name, _) -> "push_event " <> name
58      Navigate(to) -> "navigate " <> to
59      Subscribe(topic) -> "subscribe " <> topic
60      Unsubscribe(topic) -> "unsubscribe " <> topic
61      Shutdown(reason) -> "shutdown " <> reason
62    }
63  }