types.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package setting
  4. import (
  5. "encoding"
  6. )
  7. // PreferenceOption is a policy that governs whether a boolean variable
  8. // is forcibly assigned an administrator-defined value, or allowed to receive
  9. // a user-defined value.
  10. type PreferenceOption byte
  11. const (
  12. ShowChoiceByPolicy PreferenceOption = iota
  13. NeverByPolicy
  14. AlwaysByPolicy
  15. )
  16. // Show returns if the UI option that controls the choice administered by this
  17. // policy should be shown. Currently this is true if and only if the policy is
  18. // [ShowChoiceByPolicy].
  19. func (p PreferenceOption) Show() bool {
  20. return p == ShowChoiceByPolicy
  21. }
  22. // ShouldEnable checks if the choice administered by this policy should be
  23. // enabled. If the administrator has chosen a setting, the administrator's
  24. // setting is returned, otherwise userChoice is returned.
  25. func (p PreferenceOption) ShouldEnable(userChoice bool) bool {
  26. switch p {
  27. case NeverByPolicy:
  28. return false
  29. case AlwaysByPolicy:
  30. return true
  31. default:
  32. return userChoice
  33. }
  34. }
  35. // IsAlways reports whether the preference should always be enabled.
  36. func (p PreferenceOption) IsAlways() bool {
  37. return p == AlwaysByPolicy
  38. }
  39. // IsNever reports whether the preference should always be disabled.
  40. func (p PreferenceOption) IsNever() bool {
  41. return p == NeverByPolicy
  42. }
  43. // WillOverride checks if the choice administered by the policy is different
  44. // from the user's choice.
  45. func (p PreferenceOption) WillOverride(userChoice bool) bool {
  46. return p.ShouldEnable(userChoice) != userChoice
  47. }
  48. // String returns a string representation of p.
  49. func (p PreferenceOption) String() string {
  50. switch p {
  51. case AlwaysByPolicy:
  52. return "always"
  53. case NeverByPolicy:
  54. return "never"
  55. default:
  56. return "user-decides"
  57. }
  58. }
  59. // MarshalText implements [encoding.TextMarshaler].
  60. func (p *PreferenceOption) MarshalText() (text []byte, err error) {
  61. return []byte(p.String()), nil
  62. }
  63. // UnmarshalText implements [encoding.TextUnmarshaler].
  64. // It never fails and sets p to [ShowChoiceByPolicy] if the specified text
  65. // does not represent a valid [PreferenceOption].
  66. func (p *PreferenceOption) UnmarshalText(text []byte) error {
  67. switch string(text) {
  68. case "always":
  69. *p = AlwaysByPolicy
  70. case "never":
  71. *p = NeverByPolicy
  72. default:
  73. *p = ShowChoiceByPolicy
  74. }
  75. return nil
  76. }
  77. // Visibility is a policy that controls whether or not a particular
  78. // component of a user interface is to be shown.
  79. type Visibility byte
  80. var (
  81. _ encoding.TextMarshaler = (*Visibility)(nil)
  82. _ encoding.TextUnmarshaler = (*Visibility)(nil)
  83. )
  84. const (
  85. VisibleByPolicy Visibility = 'v'
  86. HiddenByPolicy Visibility = 'h'
  87. )
  88. // Show reports whether the UI option administered by this policy should be shown.
  89. // Currently this is true if the policy is not [hiddenByPolicy].
  90. func (v Visibility) Show() bool {
  91. return v != HiddenByPolicy
  92. }
  93. // String returns a string representation of v.
  94. func (v Visibility) String() string {
  95. switch v {
  96. case 'h':
  97. return "hide"
  98. default:
  99. return "show"
  100. }
  101. }
  102. // MarshalText implements [encoding.TextMarshaler].
  103. func (v Visibility) MarshalText() (text []byte, err error) {
  104. return []byte(v.String()), nil
  105. }
  106. // UnmarshalText implements [encoding.TextUnmarshaler].
  107. // It never fails and sets v to [VisibleByPolicy] if the specified text
  108. // does not represent a valid [Visibility].
  109. func (v *Visibility) UnmarshalText(text []byte) error {
  110. switch string(text) {
  111. case "hide":
  112. *v = HiddenByPolicy
  113. default:
  114. *v = VisibleByPolicy
  115. }
  116. return nil
  117. }