1use std::fmt;
8use std::fmt::{Display, Formatter};
9
10use colored::Colorize as _;
11use indent::indent_by;
12
13use crate::format::Config;
14use crate::{Context, Function};
15
16pub fn format_function<'cx>(cx: &'cx Context, cfg: Config, function: &'cx Function<'cx>) -> FunctionDisplay<'cx> {
25 FunctionDisplay { cx, function, cfg }
26}
27
28#[must_use]
29pub struct FunctionDisplay<'cx> {
30 cx: &'cx Context,
31 function: &'cx Function<'cx>,
32 cfg: Config,
33}
34
35impl Display for FunctionDisplay<'_> {
36 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
37 if let Some(func) = self.function.to_string(self.cx) {
38 let spaces = 2 * (self.cfg.indentation + self.cfg.depth);
39 write!(
40 f,
41 "{}",
42 indent_by(spaces as usize, func).color(self.cfg.colours.function)
43 )
44 } else {
45 Ok(())
46 }
47 }
48}