Skip to content

Commit

Permalink
Cleanup: 代码整理
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Mar 19, 2022
1 parent e31fb81 commit aeedbf8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 34 deletions.
21 changes: 3 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::{
fs::File,
path::{Path, PathBuf},
};
use std::fs::File;

#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -44,9 +41,7 @@ impl Author {
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Database {
pub table_name: String,
pub base_dir: String,
pub source: String,
pub path: String,
pub files: Option<Vec<String>>,
pub repos: Option<Vec<Repository>>,
}
Expand All @@ -69,16 +64,6 @@ impl Database {
}
}

impl Database {
pub fn location(&self, ext: String) -> PathBuf {
Path::new(self.base_dir.as_str()).join(format!(
"{}.{}",
self.table_name.clone(),
ext.as_str()
))
}
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct FetchAction {
pub github: Option<Vec<Github>>,
Expand All @@ -87,7 +72,7 @@ pub struct FetchAction {
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Github {
pub base_dir: String,
pub clone_dir: String,
pub destination: String,
pub token: String,
pub exclude_orgs: Option<Vec<String>>,
Expand Down
2 changes: 1 addition & 1 deletion src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl GithubRepoFetcher {
name: name.clone(),
branch: Some(repo.default_branch),
remote: Some(repo.clone_url),
path: Path::new(&config.base_dir.clone())
path: Path::new(&config.clone_dir)
.join(Path::new(&name))
.to_str()
.unwrap()
Expand Down
18 changes: 3 additions & 15 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ fn datetime_rfc339(datetime: &str) -> String {
/// 定义 Record 序列化接口
#[async_trait]
pub trait RecordSerializer {
fn extension(&self) -> String;
async fn serialize(&self, config: CreateAction) -> Result<()>;
}

Expand All @@ -50,7 +49,6 @@ static FLUSH_SIZE: usize = 500;

async fn persist_records<T: 'static + Gitter + Clone>(
gitter: T,
ext: String,
database: Database,
author_mappings: Vec<AuthorMapping>,
) -> Result<()> {
Expand Down Expand Up @@ -140,7 +138,7 @@ async fn persist_records<T: 'static + Gitter + Clone>(
}

let rev = tokio::spawn(async move {
let mut wtr = csv::Writer::from_path(database.location(ext)).unwrap();
let mut wtr = csv::Writer::from_path(database.path).unwrap();
let mut n: usize = 0;

while let Some(record) = rx.recv().await {
Expand Down Expand Up @@ -187,26 +185,16 @@ impl<T> CsvSerializer<T> {

#[async_trait]
impl<T: 'static + Gitter + Clone> RecordSerializer for CsvSerializer<T> {
fn extension(&self) -> String {
String::from("csv")
}

async fn serialize(&self, config: CreateAction) -> Result<()> {
let mut handles = vec![];
for database in config.databases {
let gitter = self.gitter.clone();
let extension = self.extension();
let database = database.clone();
let author_mappings = config.author_mappings.clone();

let handle = tokio::spawn(async move {
let r = persist_records(
gitter,
extension,
database,
author_mappings.unwrap_or_default(),
)
.await;
let r =
persist_records(gitter, database, author_mappings.unwrap_or_default()).await;
if let Err(e) = r {
error!("Failed to persist records, error: {}", e);
exit(1)
Expand Down

0 comments on commit aeedbf8

Please sign in to comment.