Skip to content

Commit

Permalink
Fix TODO
Browse files Browse the repository at this point in the history
Add documentation to `parsed_relays.rs`
  • Loading branch information
MarkusPettersson98 committed Mar 13, 2024
1 parent 38ab1e4 commit 141e2b9
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion mullvad-relay-selector/src/relay_selector/parsed_relays.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
//! TODO(markus): Document this!
//! This module provides functionality for managing and updating the local relay list,
//! including support for loading these lists from disk & applying [overrides][`RelayOverride`].
//!
//! ## Overview
//!
//! The primary structure in this module, [`ParsedRelays`], holds information about the currently
//! available relays, including any overrides that have been applied to the original list fetched
//! from the Mullvad API or loaded from a local cache.

use std::{
collections::HashMap,
Expand All @@ -16,8 +23,11 @@ use mullvad_types::{
use crate::{constants::UDP2TCP_PORTS, error::Error};

pub(crate) struct ParsedRelays {
/// Tracks when the relay list was last updated.
last_updated: SystemTime,
/// The current list of relays, after applying [overrides][`RelayOverride`].
parsed_list: RelayList,
/// The original list of relays, as returned by the Mullvad relays API.
original_list: RelayList,
overrides: Vec<RelayOverride>,
}
Expand All @@ -28,6 +38,8 @@ impl ParsedRelays {
self.parsed_list.relays()
}

/// Replace `self` with a new [`ParsedRelays`] based on [new_relays][`ParsedRelays`],
/// bumping `self.last_updated` to the current system time.
pub fn update(&mut self, new_relays: RelayList) {
*self = Self::from_relay_list(new_relays, SystemTime::now(), &self.overrides);

Expand All @@ -37,6 +49,9 @@ impl ParsedRelays {
);
}

/// Tracks when the relay list was last updated.
///
/// The relay list can be updated by calling [`ParsedRelays::update`].
pub const fn last_updated(&self) -> SystemTime {
self.last_updated
}
Expand All @@ -45,14 +60,18 @@ impl ParsedRelays {
self.parsed_list.etag.clone()
}

/// The original list of relays, as returned by the Mullvad relays API.
pub const fn original_list(&self) -> &RelayList {
&self.original_list
}

/// The current list of relays, after applying [overrides][`RelayOverride`].
pub const fn parsed_list(&self) -> &RelayList {
&self.parsed_list
}

/// Replace the previous set of [overrides][`RelayOverride`] with `new_overrides`.
/// This will update `self.parsed_list` as a side-effect.
pub(crate) fn set_overrides(&mut self, new_overrides: &[RelayOverride]) {
self.parsed_list = Self::parse_relay_list(&self.original_list, new_overrides);
self.overrides = new_overrides.to_vec();
Expand Down Expand Up @@ -110,6 +129,8 @@ impl ParsedRelays {
Ok((last_modified, file))
}

/// Create a new [`ParsedRelays`] from [relay_list][`RelayList`] and [overrides][`RelayOverride`].
/// This will apply `overrides` to `relay_list` and store the result in `self.parsed_list`.
pub(crate) fn from_relay_list(
relay_list: RelayList,
last_updated: SystemTime,
Expand All @@ -123,6 +144,8 @@ impl ParsedRelays {
}
}

/// Apply [overrides][`RelayOverride`] to [relay_list][`RelayList`], yielding an updated relay
/// list.
fn parse_relay_list(relay_list: &RelayList, overrides: &[RelayOverride]) -> RelayList {
let mut remaining_overrides = HashMap::new();
for relay_override in overrides {
Expand Down

0 comments on commit 141e2b9

Please sign in to comment.