Skip to content

Commit af169da

Browse files
Move common error code into an inner function
1 parent d07ce99 commit af169da

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

src/manifest.rs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ pub(crate) fn load_manifests(load_from: &Path) -> Result<(Vec<MirrorFile>, Vec<S
4646
let mut cache = LocationCache::default();
4747
let mut errors = Vec::new();
4848

49+
fn emit_error(
50+
error: String,
51+
mirror_file: &MirrorFile,
52+
file_source: &str,
53+
cache: &LocationCache,
54+
errors: &mut Vec<String>,
55+
) {
56+
let location = cache
57+
.seen_paths
58+
.get(&mirror_file.name)
59+
.unwrap()
60+
.first()
61+
.unwrap();
62+
let (src_line, snippet) = span_info(&file_source, location);
63+
errors.push(format!(
64+
"{error}:\n\
65+
# {} (line {src_line})\n{snippet}\n",
66+
location.file.display()
67+
));
68+
}
69+
4970
fn load_inner(
5071
load_from: &Path,
5172
result: &mut Vec<MirrorFile>,
@@ -85,48 +106,39 @@ pub(crate) fn load_manifests(load_from: &Path) -> Result<(Vec<MirrorFile>, Vec<S
85106
match mirror_file.rename_from {
86107
Some(ref rename_from) => {
87108
if path_name == file_name {
88-
let location = cache
89-
.seen_paths
90-
.get(&mirror_file.name)
91-
.unwrap()
92-
.first()
93-
.unwrap();
94-
let (src_line, snippet) = span_info(&file_source, location);
95-
errors.push(format!(
96-
"`rename-from` field isn't needed since `source` and `name` field have the same file name (`{file_name}`):\n\
97-
# {} (line {src_line})\n{snippet}\n",
98-
location.file.display()
99-
));
100-
} else if path_name != file_name && rename_from != file_name {
101-
let location = cache
102-
.seen_paths
103-
.get(&mirror_file.name)
104-
.unwrap()
105-
.first()
106-
.unwrap();
107-
let (src_line, snippet) = span_info(&file_source, location);
108-
errors.push(format!(
109-
"`rename-from` field value doesn't match name from the URL `{source}` (`{file_name}` != `{rename_from}`):\n\
110-
# {} (line {src_line})\n{snippet}\n",
111-
location.file.display()
112-
));
109+
emit_error(
110+
format!(
111+
"`rename-from` field isn't needed since `source` and `name` field have the same file name (`{file_name}`)"
112+
),
113+
&mirror_file,
114+
&file_source,
115+
cache,
116+
errors,
117+
);
118+
} else if rename_from != file_name {
119+
emit_error(
120+
format!(
121+
"`rename-from` field value doesn't match name from the URL `{source}` (`{file_name}` != `{rename_from}`)"
122+
),
123+
&mirror_file,
124+
&file_source,
125+
cache,
126+
errors,
127+
);
113128
}
114129
}
115130
None => {
116131
if path_name != file_name {
117-
let location = cache
118-
.seen_paths
119-
.get(&mirror_file.name)
120-
.unwrap()
121-
.first()
122-
.unwrap();
123-
let (src_line, snippet) = span_info(&file_source, location);
124-
errors.push(format!(
125-
"The name from the URL `{source}` doesn't match the `name` field (`{file_name}` != `{path_name}`). \
126-
Add `rename-from = {file_name:?}` to fix this error:\n\
127-
# {} (line {src_line})\n{snippet}\n",
128-
location.file.display()
129-
));
132+
emit_error(
133+
format!(
134+
"The name from the URL `{source}` doesn't match the `name` field (`{file_name}` != `{path_name}`). \
135+
Add `rename-from = {file_name:?}` to fix this error"
136+
),
137+
&mirror_file,
138+
&file_source,
139+
cache,
140+
errors,
141+
);
130142
}
131143
}
132144
}

0 commit comments

Comments
 (0)