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