role.go 766 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package ice
  2. import (
  3. "fmt"
  4. )
  5. // Role represents ICE agent role, which can be controlling or controlled.
  6. type Role byte
  7. // Possible ICE agent roles.
  8. const (
  9. Controlling Role = iota
  10. Controlled
  11. )
  12. // UnmarshalText implements TextUnmarshaler.
  13. func (r *Role) UnmarshalText(text []byte) error {
  14. switch string(text) {
  15. case "controlling":
  16. *r = Controlling
  17. case "controlled":
  18. *r = Controlled
  19. default:
  20. return fmt.Errorf("%w %q", errUnknownRole, text)
  21. }
  22. return nil
  23. }
  24. // MarshalText implements TextMarshaler.
  25. func (r Role) MarshalText() (text []byte, err error) {
  26. return []byte(r.String()), nil
  27. }
  28. func (r Role) String() string {
  29. switch r {
  30. case Controlling:
  31. return "controlling"
  32. case Controlled:
  33. return "controlled"
  34. default:
  35. return "unknown"
  36. }
  37. }