48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package proposals
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// ReplacementPolicy controls how original_text spans are replaced in a target segment.
|
|
type ReplacementPolicy string
|
|
|
|
const (
|
|
ReplacementPolicyRequireUnique ReplacementPolicy = "require_unique"
|
|
ReplacementPolicyReplaceAll ReplacementPolicy = "replace_all"
|
|
)
|
|
|
|
func (p ReplacementPolicy) IsValid() bool {
|
|
switch p {
|
|
case ReplacementPolicyRequireUnique, ReplacementPolicyReplaceAll:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func ParseReplacementPolicy(raw string) (ReplacementPolicy, error) {
|
|
parsed := ReplacementPolicy(strings.TrimSpace(raw))
|
|
if !parsed.IsValid() {
|
|
return "", fmt.Errorf("replacement policy must be one of: require_unique, replace_all")
|
|
}
|
|
return parsed, nil
|
|
}
|
|
|
|
func (p *ReplacementPolicy) UnmarshalJSON(data []byte) error {
|
|
var raw string
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
return fmt.Errorf("replacement policy must be a JSON string: %w", err)
|
|
}
|
|
|
|
parsed, err := ParseReplacementPolicy(raw)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*p = parsed
|
|
return nil
|
|
}
|