forked from prefix-dev/pixi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor manifest into more generic approach (prefix-dev#2015)
Co-authored-by: Hofer-Julian <[email protected]>
- Loading branch information
1 parent
78850aa
commit c8b4849
Showing
24 changed files
with
350 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use toml_edit::{self, Array, Item, Table, Value}; | ||
|
||
pub mod project; | ||
|
||
pub mod manifest; | ||
|
||
pub use project::ManifestSource; | ||
|
||
use crate::error::TomlError; | ||
|
||
/// Represents a wrapper around a TOML document. | ||
/// This struct is exposed to other crates | ||
/// to allow for easy manipulation of the TOML document. | ||
#[derive(Debug, Clone, Default)] | ||
pub struct TomlManifest(toml_edit::DocumentMut); | ||
|
||
impl TomlManifest { | ||
/// Create a new `TomlManifest` from a `toml_edit::DocumentMut` document. | ||
pub fn new(document: toml_edit::DocumentMut) -> Self { | ||
Self(document) | ||
} | ||
|
||
/// Retrieve a mutable reference to a target table `table_name` | ||
/// in dotted form (e.g. `table1.table2`) from the root of the document. | ||
/// If the table is not found, it is inserted into the document. | ||
fn get_or_insert_nested_table<'a>( | ||
&'a mut self, | ||
table_name: &str, | ||
) -> Result<&'a mut Table, TomlError> { | ||
let parts: Vec<&str> = table_name.split('.').collect(); | ||
|
||
let mut current_table = self.0.as_table_mut(); | ||
|
||
for part in parts { | ||
let entry = current_table.entry(part); | ||
let item = entry.or_insert(Item::Table(Table::new())); | ||
current_table = item | ||
.as_table_mut() | ||
.ok_or_else(|| TomlError::table_error(part, table_name))?; | ||
// Avoid creating empty tables | ||
current_table.set_implicit(true); | ||
} | ||
Ok(current_table) | ||
} | ||
|
||
/// Retrieves a mutable reference to a target array `array_name` | ||
/// in table `table_name` in dotted form (e.g. `table1.table2.array`). | ||
/// | ||
/// If the array is not found, it is inserted into the document. | ||
pub fn get_or_insert_toml_array<'a>( | ||
&'a mut self, | ||
table_name: &str, | ||
array_name: &str, | ||
) -> Result<&'a mut Array, TomlError> { | ||
self.get_or_insert_nested_table(table_name)? | ||
.entry(array_name) | ||
.or_insert(Item::Value(Value::Array(Array::new()))) | ||
.as_array_mut() | ||
.ok_or_else(|| TomlError::array_error(array_name, table_name.to_string().as_str())) | ||
} | ||
|
||
/// Retrieves a mutable reference to a target array `array_name` | ||
/// in table `table_name` in dotted form (e.g. `table1.table2.array`). | ||
/// | ||
/// If the array is not found, returns None. | ||
pub fn get_toml_array<'a>( | ||
&'a mut self, | ||
table_name: &str, | ||
array_name: &str, | ||
) -> Result<Option<&'a mut Array>, TomlError> { | ||
let array = self | ||
.get_or_insert_nested_table(table_name)? | ||
.get_mut(array_name) | ||
.and_then(|a| a.as_array_mut()); | ||
Ok(array) | ||
} | ||
} |
Oops, something went wrong.