Home
Author Manual
Builder Manual
Development
Overview
fpm::Config
fpm::Document
fpm::Error
fpm::File
fpm::Font
fpm::Package
fpm::render
fpm::Result
fpm::StaticFile

fpm::Config

fpm::Config struct keeps track of configuration for a FPM package that is shared with the entire program. It is constructed from the content of FPM.ftd file for the package. In order to perform any operation on a package, this struct should be first created.

Config is created using Config::read() method, and should be constructed only once in the main() and passed everywhere.

Source Code

#[derive(Debug, Clone)]
pub struct Config {
    pub package: fpm::Package,
    pub root: camino::Utf8PathBuf,
    pub packages_root: camino::Utf8PathBuf,
    pub original_directory: camino::Utf8PathBuf,
    pub extra_data: serde_json::Map<String, serde_json::Value>,
    pub current_document: Option<String>,
    pub all_packages: std::cell::RefCell<std::collections::BTreeMap<String, fpm::Package>>,
    pub downloaded_assets: std::collections::BTreeMap<String, String>,
}

Config Properties

root

root is the package root folder, this is the folder where FPM.ftd file is stored.

Technically the rest of the program can simply call std::env::current_dir() and that is guaranteed to be same as Config.root, but Config.root is camino path, instead of std::path::Path, so we can treat root as a handy helper.

If root is passed as None, fpm::Config::read() looks for FPM.ftd file in the current directory, and if not found keeps searching for it in it’s ancestors till it finds it.

If root is passed as Some(path), it is assumed to be the root of the fpm package, and FPM.ftd file must be present in this folder.

packages_root
Keeps a track of the package root for a particular config. For a dep2 of a dep1, this could point to the <original_root>/.packages/ whereas the project root could be at <original_root>/.packages/<dep1_root>

original_directory

original_directory is the directory from which the fpm command was invoked

During the execution of fpm, we change the directory to the package root so the program can be written with the assumption that they are running from package root.

When printing filenames for users consumption we want to print the paths relative to the original_directory, so we keep track of the original directory.

extra_data
The extra_data stores the data passed for variables in ftd files as context. This data is processed by get-data processor.

current_document
current_document stores the document id (Eg: foo.ftd or bar/foo.ftd) which is currently in building process. It’s value is injected by fpm::build() function according to the currently processing document. It is consumed by the sitemap processor.

all_packages
all_packages stores the package data for all the packages that are dependencies of current package directly or indirectly. It also includes current package, translation packages, original package (of which the current package is translation) The key store the name of the package and value stores corresponding package data.

Config Functions

fpm::Config::read()
fpm::Config::read() finds the FPM.ftd and creates fpm::Config struct.

impl fpm::Config {
    pub async fn read(root: Option<String>, resolve_sitemap: bool)
        -> fpm::Result<fpm::Config>
}
fpm::Config::read() returns a fpm::Result of fpm::Config.

fpm::Config.attach_data_string()
This function can be used to attach json data to Config. This JSON data is available to every ftd file that uses get-data processor.

impl fpm::Config {
    pub fn attach_data_string(&mut self, json: &str) -> fpm::Result<()>
}

This should be called before fpm::render().

Implementation note: This function internally calls fpm::Config::attach_data().

fpm::Config.attach_data()
This function can be used to attach json data to Config. This JSON data is available to every ftd file that uses get-data processor.

impl fpm::Config {
    pub fn attach_data(&mut self, data: &serde_json::Value) -> fpm::Result<()>
}
This should be called before fpm::render().

fpm::Config.get_file_by_id(&fpm::Package)
Given the id of a document, this function returns the FTD source of the document.

impl fpm::Config {
    pub fn get_file_by_id(&self, id: &str) -> fpm::Result<fpm::File>
}
Note this implements fallback feature in case this is a translated package.

fpm::Config.get_files()
fpm::Config.get_files() returns all the “files” in a package.

pub(crate) async fn get_files(&self, package: &fpm::Package)
    -> fpm::Result<Vec<fpm::File>>

fpm::Config.get_assets()
fpm::Config.get_assets() returns the “assets” automagic module for each package.

pub(crate) async fn get_assets(
    &self,
    base_url: &str,
) -> fpm::Result<std::collections::HashMap<String, String>>
Key is the package name, and value is FTD source of generated assets module.