1- use std:: sync:: Arc ;
1+ use std:: { path :: PathBuf , sync:: Arc } ;
22
3- use axum :: {
4- extract :: { Path , Query } ,
5- Extension , Json ,
6- } ;
3+ use anyhow :: Context ;
4+ use axum :: { extract :: Query , Extension , Json } ;
5+
6+ use crate :: repo :: RepoRef ;
77
88use super :: prelude:: * ;
99
10- #[ derive( Debug , serde:: Deserialize , Default ) ]
10+ #[ derive( Debug , serde:: Deserialize ) ]
1111pub ( super ) struct Params {
12+ pub repo_ref : RepoRef ,
13+ pub path : PathBuf ,
14+
1215 /// 1-indexed line number at which to start the snippet
1316 pub line_start : Option < isize > ,
1417
@@ -19,33 +22,27 @@ pub(super) struct Params {
1922#[ derive( serde:: Serialize ) ]
2023pub ( super ) struct FileResponse {
2124 contents : String ,
25+ lang : Option < String > ,
2226}
2327
2428impl super :: ApiResponse for FileResponse { }
2529
2630pub ( super ) async fn handle < ' a > (
27- Path ( path) : Path < String > ,
2831 Query ( params) : Query < Params > ,
2932 Extension ( indexes) : Extension < Arc < Indexes > > ,
3033) -> Result < Json < super :: Response < ' a > > , Error > {
31- // Strip leading slash, always present.
32- let Some ( ( rr, file_path) ) = path. split_once ( ':' ) else {
33- return Err ( Error :: user ( "invalid path, use repo_ref:path" ) ) ;
34- } ;
35-
36- let Ok ( repo_ref) = rr. parse ( ) else {
37- println ! ( "{rr}" ) ;
38- return Err ( Error :: user ( "invalid repo_ref" ) ) ;
39- } ;
40-
4134 let doc = indexes
4235 . file
43- . by_path ( & repo_ref, file_path)
36+ . by_path (
37+ & params. repo_ref ,
38+ params. path . to_str ( ) . context ( "invalid file path" ) ?,
39+ )
4440 . await
4541 . map_err ( Error :: internal) ?;
4642
4743 Ok ( json ( FileResponse {
4844 contents : split_by_lines ( & doc. content , & doc. line_end_indices , & params) ?. to_string ( ) ,
45+ lang : doc. lang ,
4946 } ) )
5047}
5148
9390 text,
9491 & indices,
9592 & Params {
93+ repo_ref: "local//repo" . into( ) ,
94+ path: "file" . into( ) ,
9695 line_start: None ,
9796 line_end: None
9897 }
@@ -106,6 +105,8 @@ cccccc
106105 text,
107106 & indices,
108107 & Params {
108+ repo_ref: "local//repo" . into( ) ,
109+ path: "file" . into( ) ,
109110 line_start: Some ( 1 ) ,
110111 line_end: None
111112 }
@@ -119,6 +120,8 @@ cccccc
119120 text,
120121 & indices,
121122 & Params {
123+ repo_ref: "local//repo" . into( ) ,
124+ path: "file" . into( ) ,
122125 line_start: Some ( 2 ) ,
123126 line_end: None
124127 }
@@ -132,6 +135,8 @@ cccccc
132135 text,
133136 & indices,
134137 & Params {
138+ repo_ref: "local//repo" . into( ) ,
139+ path: "file" . into( ) ,
135140 line_start: Some ( 3 ) ,
136141 line_end: Some ( 3 ) ,
137142 }
@@ -145,6 +150,8 @@ cccccc
145150 text,
146151 & indices,
147152 & Params {
153+ repo_ref: "local//repo" . into( ) ,
154+ path: "file" . into( ) ,
148155 line_start: Some ( 2 ) ,
149156 line_end: Some ( 3 ) ,
150157 }
0 commit comments