Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ra0x3 committed Oct 17, 2023
1 parent 5ace7bd commit 8a36af1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
61 changes: 36 additions & 25 deletions packages/fuel-indexer-lib/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ pub struct RawManifestLine(pub usize, pub String);

/// Represents the raw manifest file.
///
/// This includes comments and other tokens that `serde_yaml` will not
/// serialize/deserialize.
/// This includes comments and other tokens that `serde_yaml` will not serialize/deserialize.
///
/// While the primary `Manifest` is used within the project for ease-of-use (due to the ability
/// to `#[derive(Serialize, Deserialize)]` using `serde_yaml`), this `RawManifest` is used as a
/// thin layer for caching comments on file read, and writing comments on file write.
///
/// This `RawManifest` serves no other purpose outside of `plugins::forc_index::ops::forc_index_build`.
pub struct RawManifest {
/// Comments in the manifest file.
comments: Vec<RawManifestLine>,
Expand Down Expand Up @@ -112,7 +117,7 @@ impl RawManifest {
.enumerate()
.filter_map(|(i, line)| {
if line.starts_with('#') {
Some(RawManifestLine(i, format!("{}", line)))
Some(RawManifestLine(i, line.to_string()))
} else {
None
}
Expand All @@ -124,7 +129,7 @@ impl RawManifest {
.enumerate()
.filter_map(|(i, line)| {
if !line.starts_with('#') {
Some(RawManifestLine(i, format!("{}", line)))
Some(RawManifestLine(i, line.to_string()))
} else {
None
}
Expand All @@ -139,9 +144,8 @@ impl RawManifest {
let serde_yaml_content = serde_yaml::to_string(&manifest)?
.lines()
.filter_map(|line| {
if line.is_empty() {
None
} else if line.starts_with("---") {
if line.is_empty() || line.starts_with("---") {
// NOTE: `serde_yaml` adds a new context when it serializes the manifest.
None
} else {
Some(line.to_string())
Expand All @@ -152,26 +156,29 @@ impl RawManifest {
let raw_yaml_content = self
.yaml
.iter()
.filter_map(|line| {
let item = line.1.as_str();
if item.is_empty() {
return None;
} else {
return Some(line);
}
})
.filter(|line| line.1.as_str().is_empty())
.collect::<Vec<_>>();

// FIXME: Need to sort the keys in both YAMls so they're in order when we zip

// `forc_index::ops::forc_index_build` will never add or remove any lines from the manifest.
// `forc_index::ops::forc_index_build` will never add or remove any lines from the manifest, so
// we can assume the amount of raw lines should always equal the amount of lines serialized by
// `serde_yaml`.
assert_eq!(raw_yaml_content.len(), serde_yaml_content.len());

let new_yaml = serde_yaml_content
let new_yaml = raw_yaml_content
.iter()
.zip(raw_yaml_content.iter())
.map(|(updated_line, raw_line)| {
RawManifestLine(raw_line.0, format!("{}\n", updated_line))
.map(|line| {
let updated_line = serde_yaml_content.iter().find(|value| {
let value = value.as_str().split(':').next().unwrap().trim();
let item = line.1.as_str().split(':').next().unwrap().trim();
item == value
});

match updated_line {
Some(updated_line) => {
RawManifestLine(line.0, format!("{}\n", updated_line))
}
None => RawManifestLine(line.0, format!("{}\n", line.1)),
}
})
.collect::<Vec<_>>();

Expand All @@ -188,7 +195,11 @@ impl RawManifest {
.collect::<Vec<_>>();
content.sort_by(|a, b| a.0.cmp(&b.0));

let content = content.iter().map(|line| line.1.to_string()).collect::<Vec<_>>().join("\n");
let content = content
.iter()
.map(|line| line.1.to_string())
.collect::<Vec<_>>()
.join("\n");
write_content(path, content.into())
}
}
Expand All @@ -203,9 +214,9 @@ impl TryFrom<&RawManifest> for Manifest {
.filter_map(|line| {
let line = line.1.as_str();
if line.is_empty() {
return None;
None
} else {
return Some(line);
Some(line)
}
})
.collect::<Vec<_>>()
Expand Down
4 changes: 4 additions & 0 deletions plugins/forc-index/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ pub fn default_indexer_manifest(

let module = if is_native {
r#"
# Native execution should not include any paths.
native: ~"#
} else {
r#"
# Path to web assembly module.
wasm: ~"#
};

Expand Down

0 comments on commit 8a36af1

Please sign in to comment.