github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/linebreak.go (about)

     1  // Copyright 2024 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yamlfmt
    16  
    17  import "fmt"
    18  
    19  type LineBreakStyle string
    20  
    21  const (
    22  	LineBreakStyleLF   LineBreakStyle = "lf"
    23  	LineBreakStyleCRLF LineBreakStyle = "crlf"
    24  )
    25  
    26  type UnsupportedLineBreakError struct {
    27  	style LineBreakStyle
    28  }
    29  
    30  func (e UnsupportedLineBreakError) Error() string {
    31  	return fmt.Sprintf("unsupported line break style %s, see package documentation for supported styles", e.style)
    32  }
    33  
    34  func (s LineBreakStyle) Separator() (string, error) {
    35  	switch s {
    36  	case LineBreakStyleLF:
    37  		return "\n", nil
    38  	case LineBreakStyleCRLF:
    39  		return "\r\n", nil
    40  	}
    41  	return "", UnsupportedLineBreakError{style: s}
    42  }