1+ use crate :: types:: * ;
2+ use chrono:: { Duration , TimeZone , Utc } ;
13use rmcp:: {
24 ErrorData as McpError , ServerHandler ,
35 handler:: server:: { router:: tool:: ToolRouter , tool:: * } ,
46 model:: * ,
57 schemars, tool, tool_handler, tool_router,
68} ;
7- use crate :: types:: * ;
8- use chrono:: { Duration , TimeZone , Utc } ;
99
1010#[ derive( Debug , Clone ) ]
1111pub struct WeatherServer {
@@ -90,12 +90,15 @@ impl WeatherServer {
9090 )
9191 } ) ?;
9292
93- tracing:: info!( "weather_data:\n {}" , serde_json:: to_string_pretty( & weather_data) . unwrap( ) ) ;
93+ tracing:: info!(
94+ "weather_data:\n {}" ,
95+ serde_json:: to_string_pretty( & weather_data) . unwrap( )
96+ ) ;
9497
9598 let weather_response = {
9699 // convert weather_data to WeatherResponse
97- let weather_response: WeatherResponse =
98- serde_json :: from_value ( weather_data . clone ( ) ) . map_err ( |e| {
100+ let weather_response: WeatherResponse = serde_json :: from_value ( weather_data . clone ( ) )
101+ . map_err ( |e| {
99102 McpError :: new (
100103 ErrorCode :: INTERNAL_ERROR ,
101104 format ! ( "Failed to parse weather response: {e}" ) ,
@@ -113,7 +116,9 @@ impl WeatherServer {
113116
114117 let weather_info = format_weather_info ( & weather_response) ;
115118
116- let content = Content :: json ( GetWeatherResponse { weather : weather_info } ) ?;
119+ let content = Content :: json ( GetWeatherResponse {
120+ weather : weather_info,
121+ } ) ?;
117122
118123 let res = CallToolResult :: success ( vec ! [ content] ) ;
119124
@@ -168,15 +173,26 @@ pub struct GetWeatherResponse {
168173 pub weather : String ,
169174}
170175
171-
172176fn format_weather_info ( weather : & WeatherResponse ) -> String {
173- let weather_item = & weather. weather [ 0 ] ; // Assuming only one weather item
174- let rain_info = weather. rain . as_ref ( ) . map_or ( "(No rain information)" . to_string ( ) , |r| format ! ( "(Rain: {} mm/h)" , r. one_hour) ) ;
175- let snow_info = weather. snow . as_ref ( ) . map_or ( "(No snow information)" . to_string ( ) , |s| format ! ( "(Snow: {} mm/h)" , s. one_hour) ) ;
177+ let weather_item = & weather. weather [ 0 ] ; // Assuming only one weather item
178+ let rain_info = weather
179+ . rain
180+ . as_ref ( )
181+ . map_or ( "(No rain information)" . to_string ( ) , |r| {
182+ format ! ( "(Rain: {} mm/h)" , r. one_hour)
183+ } ) ;
184+ let snow_info = weather
185+ . snow
186+ . as_ref ( )
187+ . map_or ( "(No snow information)" . to_string ( ) , |s| {
188+ format ! ( "(Snow: {} mm/h)" , s. one_hour)
189+ } ) ;
176190
177191 // Convert sunrise and sunset times to local timezone
178- let sunrise_local = Utc . timestamp_opt ( weather. sys . sunrise as i64 , 0 ) . unwrap ( ) + Duration :: seconds ( weather. timezone as i64 ) ;
179- let sunset_local = Utc . timestamp_opt ( weather. sys . sunset as i64 , 0 ) . unwrap ( ) + Duration :: seconds ( weather. timezone as i64 ) ;
192+ let sunrise_local = Utc . timestamp_opt ( weather. sys . sunrise as i64 , 0 ) . unwrap ( )
193+ + Duration :: seconds ( weather. timezone as i64 ) ;
194+ let sunset_local = Utc . timestamp_opt ( weather. sys . sunset as i64 , 0 ) . unwrap ( )
195+ + Duration :: seconds ( weather. timezone as i64 ) ;
180196
181197 format ! (
182198 "Current Location: {} (Country: {}, Latitude: {}, Longitude: {}, Timezone Offset: {} seconds).\n \
0 commit comments