cuelang.org/go@v0.10.1/mod/module/dirfs.go (about)

     1  package module
     2  
     3  import (
     4  	"io/fs"
     5  	"os"
     6  
     7  	"cuelang.org/go/cue/ast"
     8  )
     9  
    10  // SourceLoc represents the location of some CUE source code.
    11  type SourceLoc struct {
    12  	// FS is the filesystem containing the source.
    13  	FS fs.FS
    14  	// Dir is the directory within the above filesystem.
    15  	Dir string
    16  }
    17  
    18  // ReadCUE can be implemented by an [fs.FS]
    19  // to provide an optimized (cached) way of
    20  // reading and parsing CUE syntax.
    21  type ReadCUEFS interface {
    22  	fs.FS
    23  
    24  	// ReadCUEFile reads CUE syntax from the given path.
    25  	//
    26  	// If this method is implemented, but the implementation
    27  	// does not support reading CUE files,
    28  	// it should return [errors.ErrUnsupported].
    29  	ReadCUEFile(path string) (*ast.File, error)
    30  }
    31  
    32  // OSRootFS can be implemented by an [fs.FS]
    33  // implementation to return its root directory as
    34  // an OS file path.
    35  type OSRootFS interface {
    36  	fs.FS
    37  
    38  	// OSRoot returns the root directory of the FS
    39  	// as an OS file path. If it wasn't possible to do that,
    40  	// it returns the empty string.
    41  	OSRoot() string
    42  }
    43  
    44  // OSDirFS is like [os.DirFS] but the returned value implements
    45  // [OSRootFS] by returning p.
    46  func OSDirFS(p string) fs.FS {
    47  	return dirFSImpl{
    48  		augmentedFS: os.DirFS(p).(augmentedFS),
    49  		osRoot:      p,
    50  	}
    51  }
    52  
    53  var _ interface {
    54  	augmentedFS
    55  	OSRootFS
    56  } = dirFSImpl{}
    57  
    58  type augmentedFS interface {
    59  	fs.FS
    60  	fs.StatFS
    61  	fs.ReadDirFS
    62  	fs.ReadFileFS
    63  }
    64  
    65  type dirFSImpl struct {
    66  	osRoot string
    67  	augmentedFS
    68  }
    69  
    70  func (fsys dirFSImpl) OSRoot() string {
    71  	return fsys.osRoot
    72  }