ini_merge/lib.rs
1//! # Library to merge INI files subject to configuration
2//!
3//! This library forms the backend to
4//! <https://github.com/VorpalBlade/chezmoi_modify_manager>.
5//! You probably want that tool instead.
6//!
7//! This library provides processing of INI files. In particular:
8//!
9//! * Merging of a source INI file with a target INI file. The merging is
10//! asymmetric: The values of the source are preferred unless specific rules
11//! have been provided for those sections and/or keys. Formatting is
12//! preserved. See [`merge::merge_ini`].
13//! * Filtering of an INI file based on a rule set
14
15/// Re-export keyring
16#[cfg(feature = "keyring")]
17pub use keyring;
18// Re-export sub-module
19pub use merge::mutations;
20
21pub mod actions;
22pub mod filter;
23mod loader;
24pub mod merge;
25mod source_loader;
26
27/// Describes a property
28///
29/// This is the type that is passed to mutators.
30#[derive(Debug)]
31#[non_exhaustive]
32pub struct Property<'a> {
33 /// Trimmed section
34 pub section: &'a str,
35 /// Trimmed key
36 pub key: &'a str,
37 /// Trimmed value (if any)
38 pub val: Option<&'a str>,
39 /// Raw line
40 pub raw: &'a str,
41}
42
43impl<'a> Property<'a> {
44 /// Convert from `SourceValue` to `Property`
45 pub(crate) fn from_src(
46 section: &'a str,
47 key: &'a str,
48 value: &'a source_loader::SourceValue,
49 ) -> Self {
50 Self {
51 section,
52 key,
53 val: value.value(),
54 raw: value.raw(),
55 }
56 }
57
58 /// Convert from INI parser value to Property
59 pub(crate) const fn try_from_ini(
60 section: &'a str,
61 value: ini_roundtrip::Item<'a>,
62 ) -> Option<Self> {
63 if let ini_roundtrip::Item::Property { key, val, raw } = value {
64 Some(Property {
65 section,
66 key,
67 val,
68 raw,
69 })
70 } else {
71 None
72 }
73 }
74}
75
76/// Input type to transformers
77pub type InputData<'a> = Option<Property<'a>>;
78
79/// Identifier for things outside sections. We could use None, but that
80/// wouldn't allow easily ignoring by regex.
81pub const OUTSIDE_SECTION: &str = "<NO_SECTION>";