@@ -10,6 +10,10 @@ use walkdir::{DirEntry, WalkDir};
1010
1111use crate :: { Crate , LINTCHECK_DOWNLOADS , LINTCHECK_SOURCES } ;
1212
13+ const DEFAULT_DOCS_LINK : & str = "https://docs.rs/{krate}/{version}/src/{krate}/{file}.html#{line}" ;
14+ const DEFAULT_GITHUB_LINK : & str = "{url}/blob/{hash}/src/{file}#L{line}" ;
15+ const DEFAULT_PATH_LINK : & str = "{path}/src/{file}:{line}" ;
16+
1317/// List of sources to check, loaded from a .toml file
1418#[ derive( Debug , Deserialize ) ]
1519pub struct SourceList {
@@ -33,6 +37,39 @@ struct TomlCrate {
3337 git_hash : Option < String > ,
3438 path : Option < String > ,
3539 options : Option < Vec < String > > ,
40+ /// Magic values:
41+ /// * `{krate}` will be replaced by `self.name`
42+ /// * `{version}` will be replaced by `self.version`
43+ /// * `{url}` will be replaced with `self.git_url`
44+ /// * `{hash}` will be replaced with `self.git_hash`
45+ /// * `{path}` will be replaced with `self.path`
46+ /// * `{file}` will be replaced by the path after `src/`
47+ /// * `{line}` will be replaced by the line
48+ ///
49+ /// If unset, this will be filled by [`read_crates`] since it depends on
50+ /// the source.
51+ online_link : Option < String > ,
52+ }
53+
54+ impl TomlCrate {
55+ fn file_link ( & self , default : & str ) -> String {
56+ let mut link = self . online_link . clone ( ) . unwrap_or_else ( || default. to_string ( ) ) ;
57+ link = link. replace ( "{krate}" , & self . name ) ;
58+
59+ if let Some ( version) = & self . version {
60+ link = link. replace ( "{version}" , version) ;
61+ }
62+ if let Some ( url) = & self . git_url {
63+ link = link. replace ( "{url}" , url) ;
64+ }
65+ if let Some ( hash) = & self . git_hash {
66+ link = link. replace ( "{hash}" , hash) ;
67+ }
68+ if let Some ( path) = & self . path {
69+ link = link. replace ( "{path}" , path) ;
70+ }
71+ link
72+ }
3673}
3774
3875/// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
@@ -41,6 +78,7 @@ struct TomlCrate {
4178pub struct CrateWithSource {
4279 pub name : String ,
4380 pub source : CrateSource ,
81+ pub file_link : String ,
4482 pub options : Option < Vec < String > > ,
4583}
4684
@@ -70,6 +108,7 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateWithSource>, RecursiveOptions)
70108 source : CrateSource :: Path {
71109 path : PathBuf :: from ( path) ,
72110 } ,
111+ file_link : tk. file_link ( DEFAULT_PATH_LINK ) ,
73112 options : tk. options . clone ( ) ,
74113 } ) ;
75114 } else if let Some ( ref version) = tk. version {
@@ -78,6 +117,7 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateWithSource>, RecursiveOptions)
78117 source : CrateSource :: CratesIo {
79118 version : version. to_string ( ) ,
80119 } ,
120+ file_link : tk. file_link ( DEFAULT_DOCS_LINK ) ,
81121 options : tk. options . clone ( ) ,
82122 } ) ;
83123 } else if tk. git_url . is_some ( ) && tk. git_hash . is_some ( ) {
@@ -88,6 +128,7 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateWithSource>, RecursiveOptions)
88128 url : tk. git_url . clone ( ) . unwrap ( ) ,
89129 commit : tk. git_hash . clone ( ) . unwrap ( ) ,
90130 } ,
131+ file_link : tk. file_link ( DEFAULT_GITHUB_LINK ) ,
91132 options : tk. options . clone ( ) ,
92133 } ) ;
93134 } else {
@@ -141,6 +182,7 @@ impl CrateWithSource {
141182 }
142183 let name = & self . name ;
143184 let options = & self . options ;
185+ let file_link = & self . file_link ;
144186 match & self . source {
145187 CrateSource :: CratesIo { version } => {
146188 let extract_dir = PathBuf :: from ( LINTCHECK_SOURCES ) ;
@@ -173,6 +215,7 @@ impl CrateWithSource {
173215 name : name. clone ( ) ,
174216 path : extract_dir. join ( format ! ( "{name}-{version}/" ) ) ,
175217 options : options. clone ( ) ,
218+ base_url : file_link. clone ( ) ,
176219 }
177220 } ,
178221 CrateSource :: Git { url, commit } => {
@@ -214,6 +257,7 @@ impl CrateWithSource {
214257 name : name. clone ( ) ,
215258 path : repo_path,
216259 options : options. clone ( ) ,
260+ base_url : file_link. clone ( ) ,
217261 }
218262 } ,
219263 CrateSource :: Path { path } => {
@@ -253,6 +297,7 @@ impl CrateWithSource {
253297 name : name. clone ( ) ,
254298 path : dest_crate_root,
255299 options : options. clone ( ) ,
300+ base_url : file_link. clone ( ) ,
256301 }
257302 } ,
258303 }
0 commit comments