11use axum:: {
2- body:: Bytes ,
3- extract:: Path ,
4- http:: StatusCode ,
5- response:: IntoResponse ,
6- routing:: get,
7- Json , Router ,
2+ body:: Bytes , extract:: Path , http:: StatusCode , response:: IntoResponse , routing:: get, Json ,
3+ Router ,
84} ;
95use chrono:: { NaiveDate , Utc } ;
106use serde:: { Deserialize , Serialize } ;
117use std:: fs;
128
13- use crate :: services:: filesystem;
149use crate :: config;
10+ use crate :: services:: filesystem;
1511use crate :: services:: frontmatter;
1612
1713#[ derive( Debug , Serialize ) ]
@@ -35,7 +31,12 @@ pub fn router() -> Router {
3531 Router :: new ( )
3632 . route ( "/" , get ( list_daily_notes) )
3733 . route ( "/today" , get ( get_or_create_today) )
38- . route ( "/{date}" , get ( get_daily_note) . post ( create_daily_note) . put ( update_daily_note) )
34+ . route (
35+ "/{date}" ,
36+ get ( get_daily_note)
37+ . post ( create_daily_note)
38+ . put ( update_daily_note) ,
39+ )
3940}
4041
4142/// List all daily notes
@@ -52,7 +53,7 @@ async fn list_daily_notes() -> impl IntoResponse {
5253
5354fn list_daily_notes_impl ( ) -> Result < Vec < DailyNoteSummary > , String > {
5455 let daily_dir = config:: data_dir ( ) . join ( "daily" ) ;
55-
56+
5657 // Create directory if it doesn't exist
5758 if !daily_dir. exists ( ) {
5859 fs:: create_dir_all ( & daily_dir) . map_err ( |e| e. to_string ( ) ) ?;
@@ -70,7 +71,7 @@ fn list_daily_notes_impl() -> Result<Vec<DailyNoteSummary>, String> {
7071 }
7172
7273 let filename = path. file_stem ( ) . and_then ( |s| s. to_str ( ) ) . unwrap_or ( "" ) ;
73-
74+
7475 // Validate date format
7576 if NaiveDate :: parse_from_str ( filename, "%Y-%m-%d" ) . is_err ( ) {
7677 continue ;
@@ -102,7 +103,7 @@ fn list_daily_notes_impl() -> Result<Vec<DailyNoteSummary>, String> {
102103/// Get or create today's daily note
103104async fn get_or_create_today ( ) -> impl IntoResponse {
104105 let today = Utc :: now ( ) . format ( "%Y-%m-%d" ) . to_string ( ) ;
105-
106+
106107 match get_daily_note_impl ( & today) {
107108 Ok ( note) => Json ( note) . into_response ( ) ,
108109 Err ( _) => {
@@ -123,14 +124,16 @@ async fn get_or_create_today() -> impl IntoResponse {
123124async fn get_daily_note ( Path ( date) : Path < String > ) -> impl IntoResponse {
124125 // Validate date format
125126 if NaiveDate :: parse_from_str ( & date, "%Y-%m-%d" ) . is_err ( ) {
126- return ( StatusCode :: BAD_REQUEST , "Invalid date format. Use YYYY-MM-DD" ) . into_response ( ) ;
127+ return (
128+ StatusCode :: BAD_REQUEST ,
129+ "Invalid date format. Use YYYY-MM-DD" ,
130+ )
131+ . into_response ( ) ;
127132 }
128133
129134 match get_daily_note_impl ( & date) {
130135 Ok ( note) => Json ( note) . into_response ( ) ,
131- Err ( err) if err. contains ( "not found" ) => {
132- ( StatusCode :: NOT_FOUND , err) . into_response ( )
133- }
136+ Err ( err) if err. contains ( "not found" ) => ( StatusCode :: NOT_FOUND , err) . into_response ( ) ,
134137 Err ( err) => (
135138 StatusCode :: INTERNAL_SERVER_ERROR ,
136139 format ! ( "Failed to get daily note: {}" , err) ,
@@ -171,16 +174,18 @@ async fn create_daily_note(
171174) -> impl IntoResponse {
172175 // Validate date format
173176 if NaiveDate :: parse_from_str ( & date, "%Y-%m-%d" ) . is_err ( ) {
174- return ( StatusCode :: BAD_REQUEST , "Invalid date format. Use YYYY-MM-DD" ) . into_response ( ) ;
177+ return (
178+ StatusCode :: BAD_REQUEST ,
179+ "Invalid date format. Use YYYY-MM-DD" ,
180+ )
181+ . into_response ( ) ;
175182 }
176183
177184 let content = body. and_then ( |b| b. content . clone ( ) ) ;
178185
179186 match create_daily_note_impl ( & date, content. as_deref ( ) ) {
180187 Ok ( note) => ( StatusCode :: CREATED , Json ( note) ) . into_response ( ) ,
181- Err ( err) if err. contains ( "already exists" ) => {
182- ( StatusCode :: CONFLICT , err) . into_response ( )
183- }
188+ Err ( err) if err. contains ( "already exists" ) => ( StatusCode :: CONFLICT , err) . into_response ( ) ,
184189 Err ( err) => (
185190 StatusCode :: INTERNAL_SERVER_ERROR ,
186191 format ! ( "Failed to create daily note: {}" , err) ,
@@ -191,7 +196,7 @@ async fn create_daily_note(
191196
192197fn create_daily_note_impl ( date : & str , initial_content : Option < & str > ) -> Result < DailyNote , String > {
193198 let daily_dir = config:: data_dir ( ) . join ( "daily" ) ;
194-
199+
195200 // Create directory if it doesn't exist
196201 if !daily_dir. exists ( ) {
197202 fs:: create_dir_all ( & daily_dir) . map_err ( |e| e. to_string ( ) ) ?;
@@ -204,10 +209,9 @@ fn create_daily_note_impl(date: &str, initial_content: Option<&str>) -> Result<D
204209 }
205210
206211 let now = Utc :: now ( ) . to_rfc3339 ( ) ;
207-
212+
208213 // Parse date for display
209- let parsed_date = NaiveDate :: parse_from_str ( date, "%Y-%m-%d" )
210- . map_err ( |e| e. to_string ( ) ) ?;
214+ let parsed_date = NaiveDate :: parse_from_str ( date, "%Y-%m-%d" ) . map_err ( |e| e. to_string ( ) ) ?;
211215 let display_date = parsed_date. format ( "%A, %B %d, %Y" ) . to_string ( ) ;
212216
213217 // Create frontmatter
@@ -238,14 +242,12 @@ fn create_daily_note_impl(date: &str, initial_content: Option<&str>) -> Result<D
238242 ) ;
239243
240244 // Use provided content or default template
241- let body = initial_content
242- . map ( |c| c. to_string ( ) )
243- . unwrap_or_else ( || {
244- format ! (
245- "# {}\n \n ## Today's Focus\n \n - \n \n ## Notes\n \n \n \n ## Tasks\n \n - [ ] \n " ,
246- display_date
247- )
248- } ) ;
245+ let body = initial_content. map ( |c| c. to_string ( ) ) . unwrap_or_else ( || {
246+ format ! (
247+ "# {}\n \n ## Today's Focus\n \n - \n \n ## Notes\n \n \n \n ## Tasks\n \n - [ ] \n " ,
248+ display_date
249+ )
250+ } ) ;
249251
250252 let content = frontmatter:: serialize_frontmatter ( & fm, & body) ?;
251253
@@ -261,22 +263,21 @@ fn create_daily_note_impl(date: &str, initial_content: Option<&str>) -> Result<D
261263}
262264
263265/// Update a daily note's content
264- async fn update_daily_note (
265- Path ( date) : Path < String > ,
266- body : Bytes ,
267- ) -> impl IntoResponse {
266+ async fn update_daily_note ( Path ( date) : Path < String > , body : Bytes ) -> impl IntoResponse {
268267 // Validate date format
269268 if NaiveDate :: parse_from_str ( & date, "%Y-%m-%d" ) . is_err ( ) {
270- return ( StatusCode :: BAD_REQUEST , "Invalid date format. Use YYYY-MM-DD" ) . into_response ( ) ;
269+ return (
270+ StatusCode :: BAD_REQUEST ,
271+ "Invalid date format. Use YYYY-MM-DD" ,
272+ )
273+ . into_response ( ) ;
271274 }
272275
273276 let content = String :: from_utf8_lossy ( & body) . to_string ( ) ;
274277
275278 match update_daily_note_impl ( & date, & content) {
276279 Ok ( note) => Json ( note) . into_response ( ) ,
277- Err ( err) if err. contains ( "not found" ) => {
278- ( StatusCode :: NOT_FOUND , err) . into_response ( )
279- }
280+ Err ( err) if err. contains ( "not found" ) => ( StatusCode :: NOT_FOUND , err) . into_response ( ) ,
280281 Err ( err) => (
281282 StatusCode :: INTERNAL_SERVER_ERROR ,
282283 format ! ( "Failed to update daily note: {}" , err) ,
0 commit comments