Skip to content

Commit

Permalink
refactor(toml): Switch to using accessors with ScriptSource
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 25, 2024
1 parent 3bdc3fb commit 51cb5cc
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub(super) fn expand_manifest(
gctx: &GlobalContext,
) -> CargoResult<String> {
let source = ScriptSource::parse(content)?;
if let Some(frontmatter) = source.frontmatter {
match source.info {
if let Some(frontmatter) = source.frontmatter() {
match source.info() {
Some("cargo") | None => {}
Some(other) => {
if let Some(remainder) = other.strip_prefix("cargo,") {
Expand All @@ -50,15 +50,15 @@ pub(super) fn expand_manifest(
)
.into_path_unlocked();
let mut hacked_source = String::new();
if let Some(shebang) = source.shebang {
if let Some(shebang) = source.shebang() {
writeln!(hacked_source, "{shebang}")?;
}
writeln!(hacked_source)?; // open
for _ in 0..frontmatter.lines().count() {
writeln!(hacked_source)?;
}
writeln!(hacked_source)?; // close
writeln!(hacked_source, "{}", source.content)?;
writeln!(hacked_source, "{}", source.content())?;
if let Some(parent) = hacked_path.parent() {
cargo_util::paths::create_dir_all(parent)?;
}
Expand Down Expand Up @@ -279,6 +279,22 @@ impl<'s> ScriptSource<'s> {

Ok(source)
}

fn shebang(&self) -> Option<&'s str> {
self.shebang
}

fn info(&self) -> Option<&'s str> {
self.info
}

fn frontmatter(&self) -> Option<&'s str> {
self.frontmatter
}

fn content(&self) -> &'s str {
self.content
}
}

#[cfg(test)]
Expand All @@ -299,10 +315,10 @@ mod test_expand {
};

let mut rendered = String::new();
write_optional_field(&mut rendered, "shebang", actual.shebang);
write_optional_field(&mut rendered, "info", actual.info);
write_optional_field(&mut rendered, "frontmatter", actual.frontmatter);
writeln!(&mut rendered, "content: {:?}", actual.content).unwrap();
write_optional_field(&mut rendered, "shebang", actual.shebang());
write_optional_field(&mut rendered, "info", actual.info());
write_optional_field(&mut rendered, "frontmatter", actual.frontmatter());
writeln!(&mut rendered, "content: {:?}", actual.content()).unwrap();
assert_data_eq!(rendered, expected.raw());
}

Expand Down

0 comments on commit 51cb5cc

Please sign in to comment.