pub struct TemplatedControlEditor { /* private fields */ }Expand description
An editor for a control file that may be generated from a template.
This editor will automatically expand the template if it does not exist. It will also automatically update the template if the control file is changed.
§Example
use std::path::Path;
use debian_analyzer::control::TemplatedControlEditor;
let td = tempfile::tempdir().unwrap();
let mut editor = TemplatedControlEditor::create(td.path().join("control")).unwrap();
editor.add_source("foo").set_architecture(Some("all"));
editor.commit().unwrap();Implementations§
Source§impl TemplatedControlEditor
impl TemplatedControlEditor
Sourcepub fn create<P: AsRef<Path>>(control_path: P) -> Result<Self, EditorError>
pub fn create<P: AsRef<Path>>(control_path: P) -> Result<Self, EditorError>
Create a new control file editor.
Sourcepub fn template_type(&self) -> Option<TemplateType>
pub fn template_type(&self) -> Option<TemplateType>
Return the type of the template used to generate the control file.
Sourcepub fn normalize_field_spacing(&mut self) -> Result<(), EditorError>
pub fn normalize_field_spacing(&mut self) -> Result<(), EditorError>
Normalize field spacing in the control file.
For template-based control files with deb822-style templates (CDBS, Directory), this will normalize both the template file and the primary control file. For non-deb822 templates (Rules, Gnome, Postgresql, etc.), this returns an error since those control files are generated and shouldn’t be normalized. For files without templates, it normalizes the control file directly.
§Returns
An error if a template exists but cannot be normalized, or if the template is not a deb822-style template.
Sourcepub fn open<P: AsRef<Path>>(control_path: P) -> Result<Self, EditorError>
pub fn open<P: AsRef<Path>>(control_path: P) -> Result<Self, EditorError>
Open an existing control file.
Methods from Deref<Target = Control>§
Sourcepub fn parse_mode(&self) -> ParseMode
pub fn parse_mode(&self) -> ParseMode
Get the parse mode for this control file
Sourcepub fn as_mut_deb822(&mut self) -> &mut Deb822
pub fn as_mut_deb822(&mut self) -> &mut Deb822
Return the underlying deb822 object, mutable
Sourcepub fn add_source(&mut self, name: &str) -> Source
pub fn add_source(&mut self, name: &str) -> Source
Sourcepub fn add_binary(&mut self, name: &str) -> Binary
pub fn add_binary(&mut self, name: &str) -> Binary
Sourcepub fn remove_binary(&mut self, name: &str) -> bool
pub fn remove_binary(&mut self, name: &str) -> bool
Remove a binary package paragraph by name
§Arguments
name- The name of the binary package to remove
§Returns
true if a binary paragraph with the given name was found and removed, false otherwise
§Example
use debian_control::lossless::control::Control;
let mut control = Control::new();
control.add_binary("foo");
assert_eq!(control.binaries().count(), 1);
assert!(control.remove_binary("foo"));
assert_eq!(control.binaries().count(), 0);Sourcepub fn wrap_and_sort(
&mut self,
indentation: Indentation,
immediate_empty_line: bool,
max_line_length_one_liner: Option<usize>,
)
pub fn wrap_and_sort( &mut self, indentation: Indentation, immediate_empty_line: bool, max_line_length_one_liner: Option<usize>, )
Wrap and sort the control file
§Arguments
indentation- The indentation to useimmediate_empty_line- Whether to add an empty line at the start of multi-line fieldsmax_line_length_one_liner- The maximum line length for one-liner fields
Sourcepub fn sort_binaries(&mut self, keep_first: bool)
pub fn sort_binaries(&mut self, keep_first: bool)
Sort binary package paragraphs alphabetically by package name.
This method reorders the binary package paragraphs in alphabetical order based on their Package field value. The source paragraph always remains first.
§Arguments
keep_first- If true, keeps the first binary package in place and only sorts the remaining binary packages. If false, sorts all binary packages.
§Example
use debian_control::lossless::Control;
let input = r#"Source: foo
Package: libfoo
Architecture: all
Package: libbar
Architecture: all
"#;
let mut control: Control = input.parse().unwrap();
control.sort_binaries(false);
// Binary packages are now sorted: libbar comes before libfoo
let binaries: Vec<_> = control.binaries().collect();
assert_eq!(binaries[0].name(), Some("libbar".to_string()));
assert_eq!(binaries[1].name(), Some("libfoo".to_string()));Sourcepub fn fields_in_range(&self, range: TextRange) -> impl Iterator<Item = Entry>
pub fn fields_in_range(&self, range: TextRange) -> impl Iterator<Item = Entry>
Iterate over fields that overlap with the given range
This method returns all fields (entries) from all paragraphs that have any overlap with the specified text range. This is useful for incremental parsing in LSP contexts where you only want to process fields that were affected by a text change.
§Arguments
range- The text range to check for overlaps
§Returns
An iterator over all Entry items that overlap with the given range
§Example
use debian_control::lossless::Control;
use deb822_lossless::TextRange;
let control_text = "Source: foo\nMaintainer: test@example.com\n\nPackage: bar\nArchitecture: all\n";
let control: Control = control_text.parse().unwrap();
// Get fields in a specific range (e.g., where a change occurred)
let change_range = TextRange::new(20.into(), 40.into());
for entry in control.fields_in_range(change_range) {
if let Some(key) = entry.key() {
println!("Field {} was in the changed range", key);
}
}