From 65eab4e4886004c91e07653420be5daa566b6770 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 17 May 2022 19:57:57 +0530 Subject: [PATCH] chore: rename data::Data to ctx::Ctx --- src/{data.rs => ctx.rs} | 4 ++-- src/main.rs | 2 +- src/spider.rs | 20 +++++++++++++------- src/tests.rs | 13 +++++-------- 4 files changed, 21 insertions(+), 18 deletions(-) rename src/{data.rs => ctx.rs} (98%) diff --git a/src/data.rs b/src/ctx.rs similarity index 98% rename from src/data.rs rename to src/ctx.rs index 4953349..0b7cc34 100644 --- a/src/data.rs +++ b/src/ctx.rs @@ -31,12 +31,12 @@ lazy_static! { const CLIENT_TIMEOUT: u64 = 60; #[derive(Clone)] -pub struct Data { +pub struct Ctx { pub client: Client, pub settings: Settings, } -impl Data { +impl Ctx { pub async fn new(settings: Settings) -> Arc { let timeout = Duration::new(CLIENT_TIMEOUT, 0); let client = ClientBuilder::new() diff --git a/src/main.rs b/src/main.rs index 1dd9950..af443d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -pub mod data; +pub mod ctx; pub mod db; pub mod forge; pub mod settings; diff --git a/src/spider.rs b/src/spider.rs index d4ff12b..9ac55dc 100644 --- a/src/spider.rs +++ b/src/spider.rs @@ -24,10 +24,10 @@ use db_core::prelude::*; use forge_core::prelude::*; use gitea::Gitea; -use crate::data::Data; +use crate::ctx::Ctx; use crate::db::BoxDB; -impl Data { +impl Ctx { pub async fn crawl(&self, instance_url: &str, db: &BoxDB) { let gitea = Gitea::new(Url::parse(instance_url).unwrap(), self.client.clone()); let mut page = 1; @@ -56,7 +56,7 @@ impl Data { for (username, u) in res.users.iter() { if !db - .user_exists(&username, Some(&gitea.get_hostname())) + .user_exists(username, Some(gitea.get_hostname())) .await .unwrap() { @@ -66,8 +66,14 @@ impl Data { } for r in res.repos.iter() { - let msg = r.into(); - db.create_repository(&msg).await.unwrap(); + if !db + .repository_exists(&r.name, &r.owner.username, r.hostname) + .await + .unwrap() + { + let msg = r.into(); + db.create_repository(&msg).await.unwrap(); + } } sleep_fut.await.unwrap(); @@ -88,8 +94,8 @@ mod tests { #[actix_rt::test] async fn crawl_gitea() { - let (db, data) = sqlx_sqlite::get_data().await; - let res = data.crawl(GITEA_HOST, &db).await; + let (db, ctx) = sqlx_sqlite::get_ctx().await; + ctx.crawl(GITEA_HOST, &db).await; let hostname = get_hostname(&Url::parse(GITEA_HOST).unwrap()); assert!(db.forge_exists(&hostname).await.unwrap()); assert!(db diff --git a/src/tests.rs b/src/tests.rs index 4d1d998..48db49c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -19,30 +19,27 @@ use std::env; pub use std::sync::Arc; -use serde::Serialize; - -use crate::data::Data; +use crate::ctx::Ctx; pub use crate::db::BoxDB; use crate::settings::{DBType, Settings}; //pub mod sqlx_postgres { // use super::*; // -// pub async fn get_data() -> (BoxDB, Arc) { +// pub async fn get_ctx() -> (BoxDB, Arc) { // let url = env::var("POSTGRES_DATABASE_URL").unwrap(); // let mut settings = Settings::new().unwrap(); // settings.database.url = url.clone(); // settings.database.database_type = DBType::Postgres; // let db = pg::get_data(Some(settings.clone())).await; -// (db, Data::new(Some(settings))) +// (db, Ctx::new(Some(settings))) // } -//} pub mod sqlx_sqlite { use super::*; use crate::db::sqlite; - pub async fn get_data() -> (BoxDB, Arc) { + pub async fn get_ctx() -> (BoxDB, Arc) { let url = env::var("SQLITE_DATABASE_URL").unwrap(); env::set_var("DATABASE_URL", &url); println!("found db url: {url}"); @@ -50,6 +47,6 @@ pub mod sqlx_sqlite { settings.database.url = url.clone(); settings.database.database_type = DBType::Sqlite; let db = sqlite::get_data(Some(settings.clone())).await; - (db, Data::new(settings).await) + (db, Ctx::new(settings).await) } }