resource

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package resource provides abstractions for accessing resources.

Index

Constants

View Source
const Extension = ".corgi"

Extension is the file extension for corgi files.

Variables

This section is empty.

Functions

This section is empty.

Types

type FSSource

type FSSource struct {
	// contains filtered or unexported fields
}

FSSource is a Source that reads files from a fs.FS.

func NewFSSource

func NewFSSource(name string, filesys fs.FS) *FSSource

func (*FSSource) Name

func (s *FSSource) Name() string

func (*FSSource) ReadCorgiFile

func (s *FSSource) ReadCorgiFile(name string) (*File, error)

func (*FSSource) ReadCorgiLib

func (s *FSSource) ReadCorgiLib(name string) ([]File, error)

func (*FSSource) ReadFile

func (s *FSSource) ReadFile(name string) (*File, error)

type File

type File struct {
	// Name is the path to the file, relative to the resource directory it
	// is located in.
	//
	// It includes the file's file extension.
	Name string
	// Source is the file's source.
	Source Source
	// Contents are the contents of the file.
	Contents string
}

File represents a read resource file.

func ReadCorgiFile

func ReadCorgiFile(name string, sources ...Source) (*File, error)

ReadCorgiFile attempts to read the corgi file with the given name, as described in Source.ReadCorgiFile.

If none of the sources return a file, ReadCorgiFile returns a *NotFoundError.

func ReadCorgiLib

func ReadCorgiLib(name string, sources ...Source) ([]File, error)

ReadCorgiLib attempts to read the corgi library with the given name, as described in Source.ReadCorgiLib.

It returns the first non-nil list of Files. It does not combine files from different sources.

If none of the sources return any files, ReadCorgiLib returns a *NotFoundError.

func ReadFile

func ReadFile(name string, sources ...Source) (*File, error)

ReadFile attempts to read the file with the given name from the given sources, as described in Source.ReadFile.

If none of the sources return a file, ReadFile returns a *NotFoundError.

type NotFoundError

type NotFoundError struct {
	Name string
}

NotFoundError is returned when a resource file could not be found.

It is never returned by the Source directly.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type Source

type Source interface {
	// ReadCorgiFile returns the requested Corgi file.
	//
	// Name's file extension is optional and might not be present.
	//
	// If no file is found, ReadCorgiFile should return nil, nil.
	ReadCorgiFile(name string) (*File, error)
	// ReadCorgiLib returns the library files that are available under the
	// given name.
	// That name might either point to a directory of corgi files, or to a
	// single file.
	//
	// When accessing a file, the file extension is optional and might not
	// be present.
	//
	// If both a file and a directory with the same name exists,
	// directories should take precedence, e.g. if name is foo and a
	// directory foo and a file foo.corgi exists, the dir should be chosen.
	//
	// If no file is found, ReadCorgiLib should return nil, nil.
	ReadCorgiLib(name string) ([]File, error)
	// ReadFile reads the given file from the resource source.
	//
	// The file extension is optional and might not be present.
	// If name has no extension and a file with the given name and a corgi
	// file with the name + extension exists, then the file without
	// extension should be read.
	//
	// If no file is found, ReadFile should return nil, nil.
	ReadFile(name string) (*File, error)

	// Name returns the name of the source.
	// It should be unique to the source.
	// Ideally, it should be the path to the source root, without a
	// trailing slash.
	//
	// It must be the same for all files returned by the source.
	Name() string
}

Source is an interface that allows access to resources providing extended, included, and used files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL