report.go 747 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package goreport
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. // Code execution state.
  7. type Report struct {
  8. // Exit status.
  9. Code int
  10. // Status message.
  11. Err string
  12. // Evaluation and modification of data.
  13. EvalFunc func(r Report) Report
  14. // Saved call frames.
  15. frames []frame
  16. // User data.
  17. Tags map[string]any
  18. }
  19. // Evaluate r and return the result.
  20. func (r Report) Eval() Report {
  21. return r.EvalFunc(r)
  22. }
  23. // Exit the program with the r.Code.
  24. func (r Report) Exit() {
  25. os.Exit(r.Code)
  26. }
  27. // Return Report with the value of err in Err.
  28. func New[T ~string](err T) Report {
  29. return Report{Err: string(err)}
  30. }
  31. // Return Report with the formatted specifier in Err.
  32. func Newf(format string, errs ...any) Report {
  33. return Report{Err: fmt.Sprintf(format, errs...)}
  34. }