state.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package lz4
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "github.com/pierrec/lz4/v4/internal/lz4errors"
  7. )
  8. //go:generate go run golang.org/x/tools/cmd/stringer -type=aState -output state_gen.go
  9. const (
  10. noState aState = iota // uninitialized reader
  11. errorState // unrecoverable error encountered
  12. newState // instantiated object
  13. readState // reading data
  14. writeState // writing data
  15. closedState // all done
  16. )
  17. type (
  18. aState uint8
  19. _State struct {
  20. states []aState
  21. state aState
  22. err error
  23. }
  24. )
  25. func (s *_State) init(states []aState) {
  26. s.states = states
  27. s.state = states[0]
  28. }
  29. func (s *_State) reset() {
  30. s.state = s.states[0]
  31. s.err = nil
  32. }
  33. // next sets the state to the next one unless it is passed a non nil error.
  34. // It returns whether or not it is in error.
  35. func (s *_State) next(err error) bool {
  36. if err != nil {
  37. s.err = fmt.Errorf("%s: %w", s.state, err)
  38. s.state = errorState
  39. return true
  40. }
  41. s.state = s.states[s.state]
  42. return false
  43. }
  44. // nextd is like next but for defers.
  45. func (s *_State) nextd(errp *error) bool {
  46. return errp != nil && s.next(*errp)
  47. }
  48. // check sets s in error if not already in error and if the error is not nil or io.EOF,
  49. func (s *_State) check(errp *error) {
  50. if s.state == errorState || errp == nil {
  51. return
  52. }
  53. if err := *errp; err != nil {
  54. s.err = fmt.Errorf("%w[%s]", err, s.state)
  55. if !errors.Is(err, io.EOF) {
  56. s.state = errorState
  57. }
  58. }
  59. }
  60. func (s *_State) fail() error {
  61. s.state = errorState
  62. s.err = fmt.Errorf("%w[%s]", lz4errors.ErrInternalUnhandledState, s.state)
  63. return s.err
  64. }