| 104 | } |
| 105 | |
| 106 | func PlanUpdatePlacement(options UpdatePlacementOptions) (Placement, error) { |
| 107 | replaceRange := strings.TrimSpace(options.ReplaceRange) |
| 108 | |
| 109 | minimum := int64(1) |
| 110 | if options.AllowZero { |
| 111 | minimum = 0 |
| 112 | } |
| 113 | |
| 114 | if options.IndexProvided && options.Index < minimum { |
| 115 | return Placement{}, invalid(fmt.Sprintf("invalid --index (must be >= %d)", minimum)) |
| 116 | } |
| 117 | |
| 118 | if options.IndexProvided && replaceRange != "" { |
| 119 | return Placement{}, invalid("--index cannot be combined with --replace-range") |
| 120 | } |
| 121 | |
| 122 | if err := ValidateAnchor(options.Anchor); err != nil { |
| 123 | return Placement{}, err |
| 124 | } |
| 125 | |
| 126 | if options.Anchor.Text != "" && (options.IndexProvided || replaceRange != "") { |
| 127 | return Placement{}, invalid("--at cannot be combined with --index or --replace-range") |
| 128 | } |
| 129 | |
| 130 | target, replacing, err := parseRangeWithMinimum(replaceRange, minimum) |
| 131 | if err != nil { |
| 132 | return Placement{}, err |
| 133 | } |
| 134 | |
| 135 | switch { |
| 136 | case options.Anchor.Text != "": |
| 137 | return Placement{Kind: PlacementAnchor, Anchor: options.Anchor}, nil |
| 138 | case replacing: |
| 139 | return Placement{Kind: PlacementRange, Range: target}, nil |
| 140 | case options.IndexProvided: |
| 141 | return Placement{Kind: PlacementIndex, Index: options.Index}, nil |
| 142 | default: |
| 143 | return Placement{Kind: PlacementEnd}, nil |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func PlanInsertPlacement(options InsertPlacementOptions) (Placement, error) { |
| 148 | minimum := int64(1) |