TemplatedControlEditor

Struct TemplatedControlEditor 

Source
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

Source

pub fn create<P: AsRef<Path>>(control_path: P) -> Result<Self, EditorError>

Create a new control file editor.

Source

pub fn template_type(&self) -> Option<TemplateType>

Return the type of the template used to generate the control file.

Source

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.

Source

pub fn open<P: AsRef<Path>>(control_path: P) -> Result<Self, EditorError>

Open an existing control file.

Source

pub fn new<P: AsRef<Path>>( control_path: P, allow_missing: bool, ) -> Result<Self, EditorError>

Create a new control file editor.

Source

pub fn commit(&self) -> Result<Vec<PathBuf>, EditorError>

Commit the changes to the control file and template.

Methods from Deref<Target = Control>§

Source

pub fn parse_mode(&self) -> ParseMode

Get the parse mode for this control file

Source

pub fn as_mut_deb822(&mut self) -> &mut Deb822

Return the underlying deb822 object, mutable

Source

pub fn as_deb822(&self) -> &Deb822

Return the underlying deb822 object

Source

pub fn source(&self) -> Option<Source>

Return the source package

Source

pub fn binaries(&self) -> impl Iterator<Item = Binary>

Iterate over all binary packages

Source

pub fn add_source(&mut self, name: &str) -> Source

Add a new source package

§Arguments
  • name - The name of the source package
§Returns

The newly created source package

§Example
use debian_control::lossless::control::Control;
let mut control = Control::new();
let source = control.add_source("foo");
assert_eq!(source.name(), Some("foo".to_owned()));
Source

pub fn add_binary(&mut self, name: &str) -> Binary

Add new binary package

§Arguments
  • name - The name of the binary package
§Returns

The newly created binary package

§Example
use debian_control::lossless::control::Control;
let mut control = Control::new();
let binary = control.add_binary("foo");
assert_eq!(binary.name(), Some("foo".to_owned()));
Source

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);
Source

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 use
  • immediate_empty_line - Whether to add an empty line at the start of multi-line fields
  • max_line_length_one_liner - The maximum line length for one-liner fields
Source

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()));
Source

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);
    }
}

Trait Implementations§

Source§

impl DerefMut for TemplatedControlEditor

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl Editor<Control> for TemplatedControlEditor

Source§

fn orig_content(&self) -> Option<&[u8]>

The original content, if any - without reformatting
Source§

fn updated_content(&self) -> Option<Vec<u8>>

The updated content, if any
Source§

fn rewritten_content(&self) -> Option<&[u8]>

The original content, but rewritten with our parser/serializer
Source§

fn is_generated(&self) -> bool

Check if the file is generated
Source§

fn commit(&self) -> Result<Vec<PathBuf>, EditorError>

Commit the changes Read more
Source§

fn has_changed(&self) -> bool

Whether the file has changed
Source§

impl Deref for TemplatedControlEditor

Source§

type Target = Control

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<E> AbstractControlEditor for E
where E: Editor<Control>,

Source§

fn source<'a>(&'a mut self) -> Option<Box<dyn AbstractSource<'a> + 'a>>

Get the source package.
Source§

fn binaries<'a>(&'a mut self) -> Vec<Box<dyn AbstractBinary + 'a>>

Get the binary packages.
Source§

fn commit(&self) -> bool

Commit the changes.
Source§

fn wrap_and_sort(&mut self)

Wrap and sort the control file.
§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more