World

Trait World 

Source
pub trait World {
    // Required methods
    fn entry_point(&self) -> FileId;
    fn source(&self, file_id: FileId) -> Result<Source, FileError>;
    fn library(&self) -> &Library;
    fn write(
        &self,
        f: &mut dyn FnMut(&mut dyn Write) -> Result<(), Error>,
    ) -> Result<(), Error>;
    fn read(
        &self,
        f: &mut dyn FnMut(&mut dyn Read) -> Result<(), Error>,
    ) -> Result<(), Error>;

    // Provided methods
    fn with_io(
        &self,
        f: &mut dyn FnMut(&mut dyn Read, &mut dyn Write) -> Result<(), Error>,
    ) -> Result<(), Error> { ... }
    fn name(&self, id: FileId) -> String { ... }
    fn related_source(&self, span: Span) -> Option<Source> { ... }
}
Expand description

Defines how Compose interacts with the outside world.

World is an abstraction layer between the Compose runtime and its external environment.

It is responsible for:

  • Loading source files and defining the program entrypoint.
  • Providing access to a standard library.
  • Provide I/O primitives for stdout.

This trait allows Compose to be embedded in different environments (e.g. CLI tools, editors, tests, or sandboxes) without hard-coding filesystem or I/O behaviour.

Required Methods§

Source

fn entry_point(&self) -> FileId

Returns the FileId of the entrypoint of the program.

Source

fn source(&self, file_id: FileId) -> Result<Source, FileError>

Returns the Source identified by the given FileId

§Errors

Returns an error if the source cannot be located or loaded.

Source

fn library(&self) -> &Library

Returns a reference to the standard library available to the program.

Source

fn write( &self, f: &mut dyn FnMut(&mut dyn Write) -> Result<(), Error>, ) -> Result<(), Error>

Provides write access to the program’s output stream.

The provided closure is given exclusive access to a writer for the duration of the call.

Source

fn read( &self, f: &mut dyn FnMut(&mut dyn Read) -> Result<(), Error>, ) -> Result<(), Error>

Provides read access to the program’s input stream.

The provided closure is given exclusive access to the reader for the duration of the call.

Provided Methods§

Source

fn with_io( &self, f: &mut dyn FnMut(&mut dyn Read, &mut dyn Write) -> Result<(), Error>, ) -> Result<(), Error>

Provides read and write access to the programs input and output stream.

The provided closure is given exclusive access to the reader and writer for the duration of the call.

Source

fn name(&self, id: FileId) -> String

Returns a human-readable name for the given file identifier.

By default, this is derived from the file’s path.

Source

fn related_source(&self, span: Span) -> Option<Source>

Attempts to retrieve the source associated with the given span.

This is primarily used for diagnostics and error reporting.

Trait Implementations§

Source§

impl<'a> Files<'a> for &dyn World

Source§

type FileId = FileId

A unique identifier for files in the file provider. This will be used for rendering diagnostic::Labels in the corresponding source files.
Source§

type Name = String

The user-facing name of a file, to be displayed in diagnostics.
Source§

type Source = Source

The source code of a file.
Source§

fn name( &'a self, id: <&dyn World as Files<'a>>::FileId, ) -> Result<<&dyn World as Files<'a>>::Name, Error>

The user-facing name of a file.
Source§

fn source( &'a self, id: <&dyn World as Files<'a>>::FileId, ) -> Result<<&dyn World as Files<'a>>::Source, Error>

The source code of a file.
Source§

fn line_index( &'a self, id: <&dyn World as Files<'a>>::FileId, byte_index: usize, ) -> Result<usize, Error>

The index of the line at the given byte index. If the byte index is past the end of the file, returns the maximum line index in the file. This means that this function only fails if the file is not present. Read more
Source§

fn line_range( &'a self, id: <&dyn World as Files<'a>>::FileId, line_index: usize, ) -> Result<Range<usize>, Error>

The byte range of line in the source of the file.
Source§

fn line_number( &'a self, id: Self::FileId, line_index: usize, ) -> Result<usize, Error>

The user-facing line number at the given line index. It is not necessarily checked that the specified line index is actually in the file. Read more
Source§

fn column_number( &'a self, id: Self::FileId, line_index: usize, byte_index: usize, ) -> Result<usize, Error>

The user-facing column number at the given line index and byte index. Read more
Source§

fn location( &'a self, id: Self::FileId, byte_index: usize, ) -> Result<Location, Error>

Convenience method for returning line and column number at the given byte index in the file.

Implementors§