Skip to content

Commit

Permalink
returning the serialized yaml even if formatting and reconciliation f…
Browse files Browse the repository at this point in the history
…ails
  • Loading branch information
robertohuertasm committed Jul 30, 2024
1 parent 7e5035f commit e68729c
Showing 1 changed file with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::comment_preserver::{prettify_yaml, reconcile_comments};
use super::comment_preserver::{prettify_yaml, reconcile_comments, ReconcileError};
use super::error::ConfigFileError;
use indexmap::IndexMap;
use kernel::config_file::config_file_to_yaml;
Expand Down Expand Up @@ -244,17 +244,35 @@ impl StaticAnalysisConfigFile {
}

/// Serializes the `StaticAnalysisConfigFile` into a YAML string.
///
/// # Returns
///
/// This function will try to prettify/format the yaml and preserve the existing comments.
/// If it fails to do so, it will return a raw yaml with the default format and without comments.
///
/// # Errors
///
/// Returns a `ConfigFileError` if something goes wrong.
#[instrument(skip(self))]
pub fn to_string(&self) -> Result<String, ConfigFileError> {
let str = config_file_to_yaml(self)?;
// fix null maps
let fixed = str.replace(": null", ":");
// preserve the comments if the original content is provided
if let Some(original_content) = &self.original_content {
reconcile_comments(original_content, &fixed, true).map_err(Into::into)
} else {
// prettify the content
prettify_yaml(&fixed).map_err(Into::into)
}

// reconcile and format
// NOTE: if this fails, we're going to return the content
// and swallow the error.
self.original_content
.as_ref()
.map_or_else(
|| prettify_yaml(&fixed),
|original_content| reconcile_comments(original_content, &fixed, true),
)
.or_else(|e| {
tracing::debug!(error = ?e, "Reconciliation error: {}", e.to_string());
Ok::<String, ReconcileError>(fixed)
})
.map_err(Into::into)
}
}

Expand Down

0 comments on commit e68729c

Please sign in to comment.