Skip to content

Commit

Permalink
refactoring; clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelCoding committed May 19, 2024
1 parent f71e46a commit f9edeea
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 358 deletions.
40 changes: 20 additions & 20 deletions src/bird.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
use axum::async_trait;
use select::predicate::Name;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use select::predicate::Name;
use time::{Duration, OffsetDateTime};
use tokio::sync::{Mutex, RwLock};
use crate::cache::{Cache, Updater};

const MAX_AGE: Duration = Duration::minutes(10);
struct BirdUpdater {
path: PathBuf,
}

#[derive(Clone)]
pub(crate) struct Bird {
path: PathBuf,
next_update: Arc<Mutex<OffsetDateTime>>,
content: Arc<RwLock<Arc<String>>>,
content: Arc<Cache<BirdUpdater>>,
}

#[async_trait]
impl Updater for BirdUpdater {
type Output = String;
type Error = anyhow::Error;

async fn update(&self) -> Result<Self::Output, Self::Error> {
let content = load(&self.path).await?;
Ok(content)
}
}

impl Bird {
pub(crate) async fn new(path: PathBuf) -> anyhow::Result<Self> {
let content = load(&path).await?;
Ok(Self {
path,
next_update: Arc::new(Mutex::new(OffsetDateTime::now_utc())),
content: Arc::new(RwLock::new(Arc::new(content))),
content: Arc::new(Cache::new(BirdUpdater { path })),
})
}

pub(crate) async fn content(&self) -> anyhow::Result<Arc<String>> {
{
let mut lock = self.next_update.lock().await;
if OffsetDateTime::now_utc() > *lock {
*self.content.write().await = Arc::new(load(&self.path).await?);
*lock = OffsetDateTime::now_utc();
}
}

Ok(self.content.read().await.clone())
self.content.get().await
}
}

Expand Down
19 changes: 0 additions & 19 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ pub(crate) struct ListmonkUserCreateResponse {
data: ListmonkUser,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct ListmonkAddSubscribers {
ids: Vec<i32>,
action: String,
target_list_ids: Vec<i32>,
status: String,
}

#[derive(Debug, Clone)]
pub enum MailingListsError {
InvalidMailingListId,
Expand Down Expand Up @@ -100,17 +92,6 @@ impl ListmonkCreateSubscriber {
}
}

impl ListmonkAddSubscribers {
fn load(subscriber: i32, desired_list: i32) -> ListmonkAddSubscribers {
ListmonkAddSubscribers {
ids: vec![subscriber],
action: "add".to_string(),
target_list_ids: vec![desired_list],
status: "unconfirmed".to_string(),
}
}
}

impl MailingLists {
pub async fn load(
url: &Url,
Expand Down
30 changes: 5 additions & 25 deletions src/peers.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
use std::collections::HashMap;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::path::Path;
use std::sync::Arc;

use reqwest::Client;
use serde::{Deserialize, Serialize};
use time::{Duration, OffsetDateTime};
use tokio::sync::RwLock;
use tracing::{error, info};
use tracing::info;
use url::Url;

use crate::cache::Cache;
use crate::cache::Updater;

// https://github.com/euro-ix/json-schemas/wiki/Schema-Field-Entries-Members#schema-field-entries---members

const MAX_AGE: Duration = Duration::hours(1);

#[derive(Deserialize, PartialEq, Clone)]
enum EuroIXMemberType {
#[serde(rename = "peering")]
Peering,
#[serde(rename = "ixp")]
IXP,
Ixp,
#[serde(rename = "other")]
Other,
}

#[derive(Deserialize, Serialize, Clone)]
enum PeeringPolicy {
#[serde(rename = "open")]
Open,
#[serde(rename = "selective")]
Selective,
#[serde(rename = "case-by-case")]
CaseByCase,
#[serde(rename = "mandatory")]
Mandatory,
}

#[derive(Deserialize, Clone)]
struct EuroIXIfList {
if_speed: u64,
Expand All @@ -64,7 +45,6 @@ struct EuroIXMemberScheme {
member_type: EuroIXMemberType,
name: String,
url: Url,
peering_policy: PeeringPolicy,
connection_list: Vec<EuroIXConnection>,
}

Expand Down Expand Up @@ -132,7 +112,7 @@ impl Updater for PeersUpdater {
let mut peers: Vec<FoundationEntity> = api_result
.member_list
.into_iter()
.filter(|peer| peer.member_type != EuroIXMemberType::IXP)
.filter(|peer| peer.member_type != EuroIXMemberType::Ixp)
.map(|value| {
let is_supporter = self.yaml_file.supporting_peers.contains(&value.asnum);
let mut does_v4 = false;
Expand Down Expand Up @@ -197,7 +177,7 @@ impl Updater for PeersUpdater {
}

impl NetworkService {
pub(crate) async fn new(base_path: &PathBuf, ixp_manager_url: Url) -> anyhow::Result<Self> {
pub(crate) async fn new(base_path: &Path, ixp_manager_url: Url) -> anyhow::Result<Self> {
let serialized_supporter = tokio::fs::read_to_string(base_path.join("supporter.yaml")).await?;
let yaml_file = serde_yaml::from_str(&serialized_supporter)?;

Expand Down
3 changes: 0 additions & 3 deletions src/routes/bird.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::sync::Arc;

use axum::extract::State;
use axum::http::StatusCode;
use axum::response::Html;
use axum::Json;
use tracing::error;

use crate::state::FoundationState;
Expand Down
Loading

0 comments on commit f9edeea

Please sign in to comment.