@@ -10,6 +10,10 @@ use walkdir::{DirEntry, WalkDir};
10
10
11
11
use crate :: { Crate , LINTCHECK_DOWNLOADS , LINTCHECK_SOURCES } ;
12
12
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
+
13
17
/// List of sources to check, loaded from a .toml file
14
18
#[ derive( Debug , Deserialize ) ]
15
19
pub struct SourceList {
@@ -33,6 +37,39 @@ struct TomlCrate {
33
37
git_hash : Option < String > ,
34
38
path : Option < String > ,
35
39
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
+ }
36
73
}
37
74
38
75
/// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
@@ -41,6 +78,7 @@ struct TomlCrate {
41
78
pub struct CrateWithSource {
42
79
pub name : String ,
43
80
pub source : CrateSource ,
81
+ pub file_link : String ,
44
82
pub options : Option < Vec < String > > ,
45
83
}
46
84
@@ -70,6 +108,7 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateWithSource>, RecursiveOptions)
70
108
source : CrateSource :: Path {
71
109
path : PathBuf :: from ( path) ,
72
110
} ,
111
+ file_link : tk. file_link ( DEFAULT_PATH_LINK ) ,
73
112
options : tk. options . clone ( ) ,
74
113
} ) ;
75
114
} else if let Some ( ref version) = tk. version {
@@ -78,6 +117,7 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateWithSource>, RecursiveOptions)
78
117
source : CrateSource :: CratesIo {
79
118
version : version. to_string ( ) ,
80
119
} ,
120
+ file_link : tk. file_link ( DEFAULT_DOCS_LINK ) ,
81
121
options : tk. options . clone ( ) ,
82
122
} ) ;
83
123
} 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)
88
128
url : tk. git_url . clone ( ) . unwrap ( ) ,
89
129
commit : tk. git_hash . clone ( ) . unwrap ( ) ,
90
130
} ,
131
+ file_link : tk. file_link ( DEFAULT_GITHUB_LINK ) ,
91
132
options : tk. options . clone ( ) ,
92
133
} ) ;
93
134
} else {
@@ -141,6 +182,7 @@ impl CrateWithSource {
141
182
}
142
183
let name = & self . name ;
143
184
let options = & self . options ;
185
+ let file_link = & self . file_link ;
144
186
match & self . source {
145
187
CrateSource :: CratesIo { version } => {
146
188
let extract_dir = PathBuf :: from ( LINTCHECK_SOURCES ) ;
@@ -173,6 +215,7 @@ impl CrateWithSource {
173
215
name : name. clone ( ) ,
174
216
path : extract_dir. join ( format ! ( "{name}-{version}/" ) ) ,
175
217
options : options. clone ( ) ,
218
+ base_url : file_link. clone ( ) ,
176
219
}
177
220
} ,
178
221
CrateSource :: Git { url, commit } => {
@@ -214,6 +257,7 @@ impl CrateWithSource {
214
257
name : name. clone ( ) ,
215
258
path : repo_path,
216
259
options : options. clone ( ) ,
260
+ base_url : file_link. clone ( ) ,
217
261
}
218
262
} ,
219
263
CrateSource :: Path { path } => {
@@ -253,6 +297,7 @@ impl CrateWithSource {
253
297
name : name. clone ( ) ,
254
298
path : dest_crate_root,
255
299
options : options. clone ( ) ,
300
+ base_url : file_link. clone ( ) ,
256
301
}
257
302
} ,
258
303
}
0 commit comments