github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/lsp/util.go (about)

     1  package lsp
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"io"
     7  	"os"
     8  
     9  	"github.com/sourcegraph/jsonrpc2"
    10  )
    11  
    12  const (
    13  	codeUninitialized int64 = 32002
    14  )
    15  
    16  func unmarshalParams[T any](r *jsonrpc2.Request) (T, error) {
    17  	var params T
    18  	if r.Params == nil {
    19  		return params, invalidParams(errors.New("params not provided"))
    20  	}
    21  	if err := json.Unmarshal(*r.Params, &params); err != nil {
    22  		return params, invalidParams(err)
    23  	}
    24  	return params, nil
    25  }
    26  
    27  func invalidParams(err error) *jsonrpc2.Error {
    28  	return &jsonrpc2.Error{
    29  		Code:    jsonrpc2.CodeInvalidParams,
    30  		Message: err.Error(),
    31  	}
    32  }
    33  
    34  func invalidRequest(err error) *jsonrpc2.Error {
    35  	return &jsonrpc2.Error{
    36  		Code:    jsonrpc2.CodeInvalidRequest,
    37  		Message: err.Error(),
    38  	}
    39  }
    40  
    41  type stdrwc struct{}
    42  
    43  var _ io.ReadWriteCloser = (*stdrwc)(nil)
    44  
    45  func (stdrwc) Read(p []byte) (int, error)  { return os.Stdin.Read(p) }
    46  func (stdrwc) Write(p []byte) (int, error) { return os.Stdout.Write(p) }
    47  func (stdrwc) Close() error {
    48  	if err := os.Stdin.Close(); err != nil {
    49  		return err
    50  	}
    51  	return os.Stdout.Close()
    52  }