role.go 868 B

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