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.
#[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>, }
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
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
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.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.attach_data_string()
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()
get-data
processor.impl fpm::Config { pub fn attach_data(&mut self, data: &serde_json::Value) -> fpm::Result<()> }
fpm::render()
.fpm::Config.get_file_by_id(&fpm::Package)
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> }
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>>