Skip to content

Commit a0ccb13

Browse files
committed
refactor(toml): Switch to using accessors with ScriptSource
1 parent e82a4be commit a0ccb13

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/cargo/util/toml/embedded.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub(super) fn expand_manifest(
2222
gctx: &GlobalContext,
2323
) -> CargoResult<String> {
2424
let source = ScriptSource::parse(content)?;
25-
if let Some(frontmatter) = source.frontmatter {
26-
match source.info {
25+
if let Some(frontmatter) = source.frontmatter() {
26+
match source.info() {
2727
Some("cargo") | None => {}
2828
Some(other) => {
2929
if let Some(remainder) = other.strip_prefix("cargo,") {
@@ -50,15 +50,15 @@ pub(super) fn expand_manifest(
5050
)
5151
.into_path_unlocked();
5252
let mut hacked_source = String::new();
53-
if let Some(shebang) = source.shebang {
53+
if let Some(shebang) = source.shebang() {
5454
writeln!(hacked_source, "{shebang}")?;
5555
}
5656
writeln!(hacked_source)?; // open
5757
for _ in 0..frontmatter.lines().count() {
5858
writeln!(hacked_source)?;
5959
}
6060
writeln!(hacked_source)?; // close
61-
writeln!(hacked_source, "{}", source.content)?;
61+
writeln!(hacked_source, "{}", source.content())?;
6262
if let Some(parent) = hacked_path.parent() {
6363
cargo_util::paths::create_dir_all(parent)?;
6464
}
@@ -279,6 +279,22 @@ impl<'s> ScriptSource<'s> {
279279

280280
Ok(source)
281281
}
282+
283+
fn shebang(&self) -> Option<&'s str> {
284+
self.shebang
285+
}
286+
287+
fn info(&self) -> Option<&'s str> {
288+
self.info
289+
}
290+
291+
fn frontmatter(&self) -> Option<&'s str> {
292+
self.frontmatter
293+
}
294+
295+
fn content(&self) -> &'s str {
296+
self.content
297+
}
282298
}
283299

284300
#[cfg(test)]
@@ -299,10 +315,10 @@ mod test_expand {
299315
};
300316

301317
let mut rendered = String::new();
302-
write_optional_field(&mut rendered, "shebang", actual.shebang);
303-
write_optional_field(&mut rendered, "info", actual.info);
304-
write_optional_field(&mut rendered, "frontmatter", actual.frontmatter);
305-
writeln!(&mut rendered, "content: {:?}", actual.content).unwrap();
318+
write_optional_field(&mut rendered, "shebang", actual.shebang());
319+
write_optional_field(&mut rendered, "info", actual.info());
320+
write_optional_field(&mut rendered, "frontmatter", actual.frontmatter());
321+
writeln!(&mut rendered, "content: {:?}", actual.content()).unwrap();
306322
assert_data_eq!(rendered, expected.raw());
307323
}
308324

0 commit comments

Comments
 (0)