2019-04-04 16:30:20 +00:00
|
|
|
//! This module contains utilities for rendering turning things into something
|
|
|
|
//! that may be used to render in UI.
|
2019-03-12 07:24:46 +00:00
|
|
|
use super::*;
|
|
|
|
use std::fmt::{self, Display};
|
2019-04-04 16:30:20 +00:00
|
|
|
use join_to_string::join;
|
|
|
|
|
|
|
|
/// Contains information about a function signature
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FunctionSignature {
|
|
|
|
/// Optional visibility
|
|
|
|
pub visibility: Option<String>,
|
|
|
|
/// Name of the function
|
|
|
|
pub name: Option<String>,
|
|
|
|
/// Documentation for the function
|
|
|
|
pub doc: Option<Documentation>,
|
|
|
|
/// Generic parameters
|
|
|
|
pub generic_parameters: Vec<String>,
|
|
|
|
/// Parameters of the function
|
|
|
|
pub parameters: Vec<String>,
|
|
|
|
/// Optional return type
|
|
|
|
pub ret_type: Option<String>,
|
|
|
|
/// Where predicates
|
|
|
|
pub where_predicates: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionSignature {
|
|
|
|
pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self {
|
|
|
|
self.doc = doc;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 07:24:46 +00:00
|
|
|
|
|
|
|
impl Display for FunctionSignature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
if let Some(t) = &self.visibility {
|
|
|
|
write!(f, "{} ", t)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(name) = &self.name {
|
|
|
|
write!(f, "fn {}", name)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.generic_parameters.is_empty() {
|
2019-04-04 16:30:20 +00:00
|
|
|
join(self.generic_parameters.iter())
|
|
|
|
.separator(", ")
|
|
|
|
.surround_with("<", ">")
|
|
|
|
.to_fmt(f)?;
|
2019-03-12 07:24:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 16:30:20 +00:00
|
|
|
join(self.parameters.iter()).separator(", ").surround_with("(", ")").to_fmt(f)?;
|
2019-03-12 07:24:46 +00:00
|
|
|
|
|
|
|
if let Some(t) = &self.ret_type {
|
|
|
|
write!(f, " -> {}", t)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.where_predicates.is_empty() {
|
|
|
|
write!(f, "\nwhere ")?;
|
2019-04-04 16:30:20 +00:00
|
|
|
join(self.where_predicates.iter()).separator(",\n ").to_fmt(f)?;
|
2019-03-12 07:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|