1 //! FIXME: write short doc here
4 use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
6 pub(crate) trait ShortLabel {
7 fn short_label(&self) -> Option<String>;
10 impl ShortLabel for ast::Fn {
11 fn short_label(&self) -> Option<String> {
12 Some(crate::display::function_declaration(self))
16 impl ShortLabel for ast::Struct {
17 fn short_label(&self) -> Option<String> {
18 short_label_from_node(self, "struct ")
22 impl ShortLabel for ast::Union {
23 fn short_label(&self) -> Option<String> {
24 short_label_from_node(self, "union ")
28 impl ShortLabel for ast::Enum {
29 fn short_label(&self) -> Option<String> {
30 short_label_from_node(self, "enum ")
34 impl ShortLabel for ast::Trait {
35 fn short_label(&self) -> Option<String> {
36 if self.unsafe_token().is_some() {
37 short_label_from_node(self, "unsafe trait ")
39 short_label_from_node(self, "trait ")
44 impl ShortLabel for ast::Module {
45 fn short_label(&self) -> Option<String> {
46 short_label_from_node(self, "mod ")
50 impl ShortLabel for ast::SourceFile {
51 fn short_label(&self) -> Option<String> {
56 impl ShortLabel for ast::BlockExpr {
57 fn short_label(&self) -> Option<String> {
62 impl ShortLabel for ast::TypeAlias {
63 fn short_label(&self) -> Option<String> {
64 short_label_from_node(self, "type ")
68 impl ShortLabel for ast::Const {
69 fn short_label(&self) -> Option<String> {
70 let mut new_buf = short_label_from_ty(self, self.ty(), "const ")?;
71 if let Some(expr) = self.body() {
72 format_to!(new_buf, " = {}", expr.syntax());
78 impl ShortLabel for ast::Static {
79 fn short_label(&self) -> Option<String> {
80 short_label_from_ty(self, self.ty(), "static ")
84 impl ShortLabel for ast::RecordField {
85 fn short_label(&self) -> Option<String> {
86 short_label_from_ty(self, self.ty(), "")
90 impl ShortLabel for ast::Variant {
91 fn short_label(&self) -> Option<String> {
92 Some(self.name()?.text().to_string())
96 impl ShortLabel for ast::ConstParam {
97 fn short_label(&self) -> Option<String> {
98 let mut buf = "const ".to_owned();
99 buf.push_str(self.name()?.text());
100 if let Some(type_ref) = self.ty() {
101 format_to!(buf, ": {}", type_ref.syntax());
107 fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String>
109 T: NameOwner + VisibilityOwner,
111 let mut buf = short_label_from_node(node, prefix)?;
113 if let Some(type_ref) = ty {
114 format_to!(buf, ": {}", type_ref.syntax());
120 fn short_label_from_node<T>(node: &T, label: &str) -> Option<String>
122 T: NameOwner + VisibilityOwner,
124 let mut buf = node.visibility().map(|v| format!("{} ", v.syntax())).unwrap_or_default();
126 buf.push_str(node.name()?.text());