result.go RawHistoryFolder
1package palette
2
3import (
4 "time"
5
6 "github.com/bnema/vev/internal/domain"
7 "github.com/bnema/vev/internal/ports"
8 "github.com/bnema/vev/internal/usecase/command"
9)
10
11// ResultKind identifies the immutable target represented by a palette row.
12type ResultKind uint8
13
14const (
15 ResultKindCommand ResultKind = iota
16 ResultKindActiveSession
17 ResultKindStoppedSession
18 ResultKindRecentRoute
19)
20
21// Result is an immutable palette target. Its kind is the sole discriminator
22// for its private command or session payload.
23type Result struct {
24 kind ResultKind
25 command command.Command
26 session sessionPayload
27 route routePayload
28}
29
30type sessionPayload struct {
31 name string
32 createdAt time.Time
33 sessionID domain.SessionID
34}
35
36type routePayload struct {
37 label string
38 action ports.RouteNavigationAction
39}
40
41// NewCommandResult creates a static command palette target.
42func NewCommandResult(cmd command.Command) Result {
43 return Result{kind: ResultKindCommand, command: cmd}
44}
45
46// NewActiveSessionResult creates an immutable active named-session target.
47func NewActiveSessionResult(name string, createdAt time.Time, sessionID domain.SessionID) Result {
48 return Result{
49 kind: ResultKindActiveSession,
50 session: sessionPayload{name: name, createdAt: createdAt, sessionID: sessionID},
51 }
52}
53
54// NewStoppedSessionResult creates an immutable stopped named-session target.
55func NewStoppedSessionResult(name string, createdAt time.Time) Result {
56 return Result{
57 kind: ResultKindStoppedSession,
58 session: sessionPayload{name: name, createdAt: createdAt},
59 }
60}
61
62// NewRecentRouteResult creates an immutable client-ledger route target.
63func NewRecentRouteResult(label string, action ports.RouteNavigationAction) Result {
64 return Result{kind: ResultKindRecentRoute, route: routePayload{label: label, action: action}}
65}
66
67func (r Result) Kind() ResultKind { return r.kind }
68
69func (r Result) DisplayText() string {
70 if r.kind == ResultKindCommand {
71 return r.command.Code
72 }
73 if r.kind == ResultKindRecentRoute {
74 return activeSessionDisplayPrefix + r.route.label
75 }
76 return r.sessionDisplayPrefix() + r.session.name
77}
78
79func (r Result) sessionDisplayPrefix() string {
80 if r.kind == ResultKindStoppedSession {
81 return stoppedSessionDisplayPrefix
82 }
83 return activeSessionDisplayPrefix
84}
85
86func (r Result) SearchText() string {
87 return r.DisplayText()
88}
89
90// Command returns the command payload only for command results.
91func (r Result) Command() (command.Command, bool) {
92 return r.command, r.kind == ResultKindCommand
93}
94
95// SessionName returns the session name only for session results.
96func (r Result) SessionName() (string, bool) {
97 return r.session.name, r.kind == ResultKindActiveSession || r.kind == ResultKindStoppedSession
98}
99
100// SessionCreatedAt returns the session creation time only for session results.
101func (r Result) SessionCreatedAt() (time.Time, bool) {
102 return r.session.createdAt, r.kind == ResultKindActiveSession || r.kind == ResultKindStoppedSession
103}
104
105// SessionID returns the lifecycle identity only for active session results.
106func (r Result) SessionID() (domain.SessionID, bool) {
107 return r.session.sessionID, r.kind == ResultKindActiveSession
108}
109
110// RouteNavigationAction returns the exact client-ledger target only for a
111// recent-route result.
112func (r Result) RouteNavigationAction() (ports.RouteNavigationAction, bool) {
113 return r.route.action, r.kind == ResultKindRecentRoute
114}
115
116const (
117 activeSessionDisplayPrefix = "Switch to session "
118 stoppedSessionDisplayPrefix = "Resume session "
119)
120
121// CommandResults converts static commands to immutable palette targets.
122func CommandResults(commands []command.Command) []Result {
123 results := make([]Result, len(commands))
124 for i, cmd := range commands {
125 results[i] = NewCommandResult(cmd)
126 }
127 return results
128}