use crate::layouter_error;
use crate::{Drawer, Embedder, LayouterError, SvgDrawer, Visualize};
use id_tree::Tree;
pub type Result = layouter_error::Result<()>;
pub struct Layouter<'a, 'b, 'c, T>
where
T: Visualize,
{
tree: &'a Tree<T>,
drawer: Option<&'b dyn Drawer>,
file_name: Option<&'c std::path::Path>,
}
impl<'a, 'b, 'c, T> Layouter<'a, 'b, 'c, T>
where
T: Visualize,
{
pub fn new(tree: &'a Tree<T>) -> Self {
Self {
tree,
drawer: None,
file_name: None,
}
}
pub fn with_file_path(self, path: &'c std::path::Path) -> Self {
Self {
tree: self.tree,
file_name: Some(path),
drawer: self.drawer,
}
}
pub fn with_drawer(self, drawer: &'b dyn Drawer) -> Self {
Self {
tree: self.tree,
file_name: self.file_name,
drawer: Some(drawer),
}
}
pub fn write(&self) -> Result {
if self.file_name.is_none() {
Err(LayouterError::from_description(
"No output file name given - use Layouter::with_file_path.".to_string(),
))
} else {
let embedding = Embedder::embed(self.tree);
let default_drawer = SvgDrawer::new();
let drawer = self.drawer.unwrap_or(&default_drawer);
drawer
.draw(self.file_name.unwrap(), &embedding)
.map_err(LayouterError::from_io_error)
}
}
}