Relations

Struct Relations 

Source
pub struct Relations(/* private fields */);
Expand description

A node in the syntax tree representing a $ast

Implementations§

Source§

impl Relations

Source

pub fn new() -> Self

Create a new relations field

Source

pub fn wrap_and_sort(self) -> Self

Wrap and sort this relations field

Source

pub fn entries(&self) -> impl Iterator<Item = Entry> + '_

Iterate over the entries in this relations field

Source

pub fn iter(&self) -> impl Iterator<Item = Entry> + '_

Iterate over the entries in this relations field

Source

pub fn get_entry(&self, idx: usize) -> Option<Entry>

Remove the entry at the given index

Source

pub fn remove_entry(&mut self, idx: usize) -> Entry

Remove the entry at the given index

Source

pub fn insert_with_separator( &mut self, idx: usize, entry: Entry, default_sep: Option<&str>, )

Insert a new entry at the given index

§Arguments
  • idx - The index to insert at
  • entry - The entry to insert
  • default_sep - Optional default separator to use if no pattern is detected (defaults to “ “)
Source

pub fn insert(&mut self, idx: usize, entry: Entry)

Insert a new entry at the given index with default separator

Source

pub fn replace(&mut self, idx: usize, entry: Entry)

Replace the entry at the given index

Source

pub fn push(&mut self, entry: Entry)

Push a new entry to the relations field

Source

pub fn substvars(&self) -> impl Iterator<Item = String> + '_

Return the names of substvars in this relations field

Source

pub fn parse_relaxed(s: &str, allow_substvar: bool) -> (Relations, Vec<String>)

Parse a relations field from a string, allowing syntax errors

Source

pub fn satisfied_by(&self, package_version: impl VersionLookup + Copy) -> bool

Check if this relations field is satisfied by the given package versions.

Source

pub fn is_empty(&self) -> bool

Check if this relations field is empty

Source

pub fn len(&self) -> usize

Get the number of entries in this relations field

Source

pub fn ensure_minimum_version( &mut self, package: &str, minimum_version: &Version, )

Ensure that a package has at least a minimum version constraint.

If the package already exists with a version constraint that satisfies the minimum version, it is left unchanged. Otherwise, the constraint is updated or added.

§Arguments
  • package - The package name
  • minimum_version - The minimum version required
§Example
use debian_control::lossless::relations::Relations;

let mut relations: Relations = "debhelper (>= 9)".parse().unwrap();
relations.ensure_minimum_version("debhelper", &"12".parse().unwrap());
assert_eq!(relations.to_string(), "debhelper (>= 12)");

let mut relations: Relations = "python3".parse().unwrap();
relations.ensure_minimum_version("debhelper", &"12".parse().unwrap());
assert!(relations.to_string().contains("debhelper (>= 12)"));
Source

pub fn ensure_exact_version(&mut self, package: &str, version: &Version)

Ensure that a package has an exact version constraint.

§Arguments
  • package - The package name
  • version - The exact version required
§Example
use debian_control::lossless::relations::Relations;

let mut relations: Relations = "debhelper (>= 9)".parse().unwrap();
relations.ensure_exact_version("debhelper", &"12".parse().unwrap());
assert_eq!(relations.to_string(), "debhelper (= 12)");
Source

pub fn ensure_some_version(&mut self, package: &str)

Ensure that a package dependency exists, without specifying a version.

If the package already exists (with or without a version constraint), the relations field is left unchanged. Otherwise, the package is added without a version constraint.

§Arguments
  • package - The package name
§Example
use debian_control::lossless::relations::Relations;

let mut relations: Relations = "python3".parse().unwrap();
relations.ensure_some_version("debhelper");
assert!(relations.to_string().contains("debhelper"));
Source

pub fn ensure_relation(&mut self, new_entry: Entry) -> bool

Ensure that a relation exists in the dependencies.

This function checks if the provided entry is already satisfied by an existing entry. If it is, no changes are made. If an existing entry is weaker than the new entry (i.e., the new entry implies the existing one), the existing entry is replaced with the new one. Otherwise, the new entry is added.

§Arguments
  • new_entry - The entry to ensure exists
§Returns

true if the entry was added or replaced, false if it was already satisfied

§Example
use debian_control::lossless::relations::{Relations, Entry};

let mut relations: Relations = "python3".parse().unwrap();
let new_entry: Entry = "debhelper (>= 12)".parse().unwrap();
let added = relations.ensure_relation(new_entry);
assert!(added);
assert!(relations.to_string().contains("debhelper (>= 12)"));
Source

pub fn ensure_substvar(&mut self, substvar: &str) -> Result<(), String>

Ensure that a substitution variable is present in the relations.

If the substvar already exists, it is left unchanged. Otherwise, it is added at the end of the relations list.

§Arguments
  • substvar - The substitution variable (e.g., “${misc:Depends}”)
§Returns

Ok(()) on success, or Err with an error message if parsing fails

§Example
use debian_control::lossless::relations::Relations;

let mut relations: Relations = "python3".parse().unwrap();
relations.ensure_substvar("${misc:Depends}").unwrap();
assert_eq!(relations.to_string(), "python3, ${misc:Depends}");
Source

pub fn drop_substvar(&mut self, substvar: &str)

Remove a substitution variable from the relations.

If the substvar exists, it is removed along with its surrounding separators. If the substvar does not exist, this is a no-op.

§Arguments
  • substvar - The substitution variable to remove (e.g., “${misc:Depends}”)
§Example
use debian_control::lossless::relations::Relations;

let (mut relations, _) = Relations::parse_relaxed("python3, ${misc:Depends}", true);
relations.drop_substvar("${misc:Depends}");
assert_eq!(relations.to_string(), "python3");
Source

pub fn filter_entries<F>(&mut self, keep: F)
where F: Fn(&Entry) -> bool,

Filter entries based on a predicate function.

§Arguments
  • keep - A function that returns true for entries to keep
§Example
use debian_control::lossless::relations::Relations;

let mut relations: Relations = "python3, debhelper, rustc".parse().unwrap();
relations.filter_entries(|entry| {
    entry.relations().any(|r| r.name().starts_with("python"))
});
assert_eq!(relations.to_string(), "python3");
Source

pub fn is_sorted(&self, sorting_order: &impl SortingOrder) -> bool

Check whether the relations are sorted according to a given sorting order.

§Arguments
  • sorting_order - The sorting order to check against
§Example
use debian_control::lossless::relations::{Relations, WrapAndSortOrder};

let relations: Relations = "debhelper, python3, rustc".parse().unwrap();
assert!(relations.is_sorted(&WrapAndSortOrder));

let relations: Relations = "rustc, debhelper, python3".parse().unwrap();
assert!(!relations.is_sorted(&WrapAndSortOrder));
Source

pub fn drop_dependency(&mut self, package: &str) -> bool

Drop a dependency from the relations by package name.

§Arguments
  • package - The package name to remove
§Returns

true if the package was found and removed, false otherwise

§Example
use debian_control::lossless::relations::Relations;

let mut relations: Relations = "python3, debhelper, rustc".parse().unwrap();
assert!(relations.drop_dependency("debhelper"));
assert_eq!(relations.to_string(), "python3, rustc");
assert!(!relations.drop_dependency("nonexistent"));
Source

pub fn add_dependency(&mut self, entry: Entry, position: Option<usize>)

Add a dependency at a specific position or auto-detect the position.

If position is None, the position is automatically determined based on the detected sorting order. If a sorting order is detected, the entry is inserted in the appropriate position to maintain that order. Otherwise, it is appended at the end.

§Arguments
  • entry - The entry to add
  • position - Optional position to insert at
§Example
use debian_control::lossless::relations::{Relations, Relation, Entry};

let mut relations: Relations = "python3, rustc".parse().unwrap();
let entry = Entry::from(Relation::simple("debhelper"));
relations.add_dependency(entry, None);
// debhelper is inserted in sorted order (if order is detected)
Source

pub fn get_relation(&self, package: &str) -> Result<(usize, Entry), String>

Get the entry containing a specific package.

This returns the first entry that contains exactly one relation with the specified package name (no alternatives).

§Arguments
  • package - The package name to search for
§Returns

A tuple of (index, Entry) if found

§Errors

Returns Err with a message if the package is found in a complex rule (with alternatives)

§Example
use debian_control::lossless::relations::Relations;

let relations: Relations = "python3, debhelper (>= 12), rustc".parse().unwrap();
let (idx, entry) = relations.get_relation("debhelper").unwrap();
assert_eq!(idx, 1);
assert_eq!(entry.to_string(), "debhelper (>= 12)");
Source

pub fn iter_relations_for( &self, package: &str, ) -> impl Iterator<Item = (usize, Entry)> + '_

Iterate over all entries containing a specific package.

§Arguments
  • package - The package name to search for
§Returns

An iterator over tuples of (index, Entry)

§Example
use debian_control::lossless::relations::Relations;

let relations: Relations = "python3 | python3-minimal, python3-dev".parse().unwrap();
let entries: Vec<_> = relations.iter_relations_for("python3").collect();
assert_eq!(entries.len(), 1);
Source

pub fn has_relation(&self, package: &str) -> bool

Check whether a package exists in the relations.

§Arguments
  • package - The package name to search for
§Returns

true if the package is found, false otherwise

§Example
use debian_control::lossless::relations::Relations;

let relations: Relations = "python3, debhelper, rustc".parse().unwrap();
assert!(relations.has_relation("debhelper"));
assert!(!relations.has_relation("nonexistent"));

Trait Implementations§

Source§

impl Debug for Relations

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Relations

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Relations

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Entry> for Relations

Source§

fn from(entry: Entry) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Entry>> for Relations

Source§

fn from(entries: Vec<Entry>) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Relations

Source§

type Err = String

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Relations

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

§

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.

§

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.

§

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

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.