11use serde:: { Serialize , Deserialize } ;
2- use reqwest:: { Error , blocking:: Client } ;
2+ use reqwest:: blocking:: Client ;
33use anyhow:: { Context , Result } ;
44use unicode_normalization:: UnicodeNormalization ;
5+ use serde_variant:: to_variant_name;
56use std:: env;
67
78fn main ( ) -> Result < ( ) > {
@@ -47,11 +48,32 @@ struct Match {
4748 id : Option < String > ,
4849 name : String ,
4950 href : String ,
50- class_name : String ,
51+ class_name : MatchClassName ,
5152 space_name : Option < String > ,
5253 space_key : Option < String > ,
5354}
5455
56+ #[ derive( Deserialize , Serialize , Debug , PartialEq ) ]
57+ enum MatchClassName {
58+ #[ serde( rename = "content-type-page" ) ]
59+ Page ,
60+ #[ serde( rename = "content-type-blogpost" ) ]
61+ BlogPost ,
62+ #[ serde( rename = "search-for" ) ]
63+ SearchFor ,
64+ #[ serde( other) ]
65+ Unknown ,
66+ }
67+
68+ impl MatchClassName {
69+ fn to_string ( & self ) -> String {
70+ match self {
71+ MatchClassName :: Page | MatchClassName :: BlogPost | MatchClassName :: SearchFor => to_variant_name ( self ) . unwrap ( ) . into ( ) ,
72+ MatchClassName :: Unknown => panic ! ( "Unsupported match class name" ) ,
73+ }
74+ }
75+ }
76+
5577#[ derive( Serialize , Debug ) ]
5678struct AlfredResult {
5779 uid : String ,
@@ -86,13 +108,29 @@ impl AlfredResult {
86108 subtitle : confluence_match. space_name . unwrap ( ) ,
87109 arg : url. clone ( ) ,
88110 icon : AlfredResultIcon {
89- path : format ! ( "assets/{}.png" , confluence_match. class_name) ,
111+ path : format ! ( "assets/{}.png" , confluence_match. class_name. to_string ( ) ) ,
90112 } ,
91113 text : AlfredResultText {
92114 copy : url,
93115 } ,
94116 }
95117 }
118+
119+ fn from_search_in_confluence_match ( confluence_match : Match , base_url : & String ) -> AlfredResult {
120+ let url = format ! ( "{}{}" , base_url, confluence_match. href) ;
121+ AlfredResult {
122+ uid : "search-item" . to_string ( ) ,
123+ title : html_escape:: decode_html_entities ( & confluence_match. name ) . into_owned ( ) ,
124+ subtitle : "Use full Confluence Search" . to_string ( ) ,
125+ arg : url. clone ( ) ,
126+ icon : AlfredResultIcon {
127+ path : format ! ( "assets/{}.png" , confluence_match. class_name. to_string( ) ) ,
128+ } ,
129+ text : AlfredResultText {
130+ copy : confluence_match. name ,
131+ } ,
132+ }
133+ }
96134}
97135
98136impl AlfredResultList {
@@ -102,9 +140,14 @@ impl AlfredResultList {
102140 . content_name_matches
103141 . into_iter ( )
104142 . flatten ( )
105- . filter ( |m| m. id . is_some ( ) )
106- . filter ( |m| m. class_name == "content-type-page" || m. class_name == "content-type-blogpost" || m. class_name == "search-for" )
107- . map ( |m| AlfredResult :: from ( m, base_url) )
143+ . filter ( |m| m. class_name != MatchClassName :: Unknown )
144+ . map ( |m|
145+ match m. class_name {
146+ MatchClassName :: Page | MatchClassName :: BlogPost => AlfredResult :: from ( m, base_url) ,
147+ MatchClassName :: SearchFor => AlfredResult :: from_search_in_confluence_match ( m, base_url) ,
148+ _ => panic ! ( "Unsupported match class name" ) ,
149+ }
150+ )
108151 . collect ( ) ,
109152 }
110153 }
0 commit comments