playlist-to-session bridge
This commit is contained in:
@@ -23,6 +23,7 @@ type PlaylistState struct {
|
||||
var (
|
||||
ErrPlaylistEmpty = errors.New("playlist is empty")
|
||||
ErrPlaylistIndexOutOfRange = errors.New("playlist index is out of range")
|
||||
ErrPlaylistNoSelection = errors.New("playlist has no selected entry")
|
||||
ErrUnknownPlaylistCommand = errors.New("unknown playlist command")
|
||||
)
|
||||
|
||||
@@ -91,3 +92,39 @@ func (s PlaylistState) Entry(playlist Playlist) (PlaylistEntry, bool) {
|
||||
}
|
||||
return playlist.Entries[s.CurrentIndex], true
|
||||
}
|
||||
|
||||
func ApplyPlaylistSelection(
|
||||
playlist Playlist,
|
||||
current PlaylistState,
|
||||
command PlaylistCommand,
|
||||
retry RetryPolicy,
|
||||
) (
|
||||
next PlaylistState,
|
||||
sessionCommand SessionCommand,
|
||||
apply bool,
|
||||
err error,
|
||||
) {
|
||||
next, err = ApplyPlaylistCommand(playlist, current, command)
|
||||
if err != nil {
|
||||
return current, SessionCommand{}, false, err
|
||||
}
|
||||
|
||||
apply = command.Kind == PlaylistSelect || next != current
|
||||
if !apply {
|
||||
return next, SessionCommand{}, false, nil
|
||||
}
|
||||
|
||||
entry, ok := next.Entry(playlist)
|
||||
if !ok {
|
||||
return current, SessionCommand{}, false, ErrPlaylistNoSelection
|
||||
}
|
||||
session := entry.SessionConfig(retry)
|
||||
if err := session.Validate(); err != nil {
|
||||
return current, SessionCommand{}, false, err
|
||||
}
|
||||
|
||||
return next, SessionCommand{
|
||||
Kind: CommandSetSession,
|
||||
Session: session,
|
||||
}, true, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user