Skip to main content

InputResolver

Trait InputResolver 

Source
pub trait InputResolver: Send + Sync {
    // Required method
    fn resolve(
        &self,
        reference: &str,
        including: Option<&str>,
        parsing: &FlmParsing,
    ) -> Result<IncludedFile, InputError>;
}
Expand description

An input resolver finds and reads the files that \input commands refer to. A program that embeds FLM implements this trait, because the library performs no input or output itself.

You set the input resolver in the inputs of a registry, with RegistryInputs::input_resolver, or for a single parse, with ParseOptions::input_resolver. If no input resolver is set, the library reports each \input command as an error (InputUnavailable) and includes nothing. See the module documentation for how included files are parsed.

The library calls resolve once for each \input command that it parses. The method receives three arguments:

  • reference: the reference to the file, as written in the document. The input resolver decides how to find the file from the reference, usually relative to the including file.
  • including: the name of the file that contains the \input command, or None if the content does not come from a file. For the primary file, the name is the canonical path of its origin.
  • parsing: the parsing settings in effect at the \input command. In an included file, these are the settings under which the library parses the file.

If parsing settings are written in the file itself, the input resolver applies them over parsing and returns the result in IncludedFile::parsing.

The library keeps track of which file includes which, and of the parsing settings of each file. An input resolver therefore does not need to remember earlier calls.

An input resolver must be Send and Sync, because the library can call it from several threads. If your input resolver records the files that it reads, store the record behind a lock, for instance a Mutex.

§Examples

The following input resolver provides files stored in memory. It sets the name, the content and the first line of each file. The other fields of IncludedFile keep their defaults, so each file is a content input without parsing settings of its own.

use std::collections::HashMap;

use flm_core::lang::{FlmParsing, IncludedFile, InputError, InputResolver};

struct InMemory {
    files: HashMap<String, String>,
}

impl InputResolver for InMemory {
    fn resolve(
        &self,
        reference: &str,
        _including: Option<&str>,
        _parsing: &FlmParsing,
    ) -> Result<IncludedFile, InputError> {
        let text = self
            .files
            .get(reference)
            .ok_or_else(|| InputError::new(format!("no file named ‘{reference}’")))?;
        Ok(IncludedFile {
            name: reference.to_string(),
            text: text.clone(),
            first_line: 1,
            ..Default::default()
        })
    }
}

let resolver = InMemory {
    files: HashMap::from([(String::from("intro.flm"), String::from("Hello."))]),
};
let file = resolver.resolve("intro.flm", None, &FlmParsing::default()).unwrap();
assert_eq!(file.text, "Hello.");
assert!(resolver.resolve("missing.flm", None, &FlmParsing::default()).is_err());

Required Methods§

Source

fn resolve( &self, reference: &str, including: Option<&str>, parsing: &FlmParsing, ) -> Result<IncludedFile, InputError>

Finds and reads the file that reference refers to, and returns its content and the information from its front matter. The trait documentation describes the arguments.

§Errors

Returns an InputError if the file cannot be found or read. The library reports the error at the \input command (InputUnreadable) and includes nothing.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§