Skip to content

Commit

Permalink
refactor(pipeline): reuse types and constants for the data pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
rfprod committed Oct 28, 2023
1 parent 3db8682 commit 584e560
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/data_pipeline/artifact/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use std::{
use colored::Colorize;

/// Supported contexts.
type Contexts<'a> = [&'a str; 2];
pub type Contexts<'a> = [&'a str; 2];
/// Supported contexts.
pub const CONTEXTS: Contexts = ["Create artifact", "Restore artifact"];

/// The entry point of the program.
pub fn main(contexts: Contexts, context_arg: Option<String>) {
Expand Down
12 changes: 7 additions & 5 deletions src/data_pipeline/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ use std::{
process::Command,
};

/// Custom result type for fetch results.
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

/// Fetch repositories from GitHub.
pub async fn repos(
q: &str,
sort: SearchReposSort,
order: Order,
per_page: i64,
page: i64,
) -> Result<FetchResult> {
) -> Result<ReposFetchResult> {
let p = DataPipelineGitHub::new();
p.repos_request(q, sort, order, per_page, page).await
}

/// GitHub repos fetch result.
pub struct FetchResult {
pub struct ReposFetchResult {
pub items: Vec<RepoSearchResultItem>,
pub total: i64,
pub retry: bool,
Expand All @@ -48,7 +50,7 @@ impl DataPipelineGitHub {
order: Order,
per_page: i64,
page: i64,
) -> Result<FetchResult> {
) -> Result<ReposFetchResult> {
let token_env = env::var("GITHUB_TOKEN");
let token = match token_env.unwrap().trim().parse::<String>() {
Ok(value) => value,
Expand Down Expand Up @@ -110,7 +112,7 @@ impl DataPipelineGitHub {
};

if retry {
let result = FetchResult {
let result = ReposFetchResult {
items: Vec::<RepoSearchResultItem>::new(),
total: 0,
retry: true,
Expand All @@ -135,7 +137,7 @@ impl DataPipelineGitHub {
let items = res.body.items.to_owned();
let total = res.body.total_count.to_owned();

let result = FetchResult {
let result = ReposFetchResult {
items,
total,
retry: false,
Expand Down
18 changes: 6 additions & 12 deletions src/data_pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,16 @@ struct InuputArguments {
search_term: Option<String>,
}

/// Supported contexts.
type Contexts<'a> = [&'a str; 2];

/// Supported collections.
type Collections<'a> = [&'a str; 1];

struct DataPipeline<'a> {
contexts: Contexts<'a>,
collections: Collections<'a>,
contexts: artifact::Contexts<'a>,
collections: mongo::Collections<'a>,
}

impl<'a> DataPipeline<'a> {
/// Program constructor.
fn new() -> DataPipeline<'a> {
let contexts: Contexts = ["Create artifact", "Restore artifact"];
let collections: Collections = ["repos"];
let contexts: artifact::Contexts = artifact::CONTEXTS;
let collections: mongo::Collections = mongo::COLLECTIONS;
let mut program = DataPipeline {
contexts,
collections,
Expand Down Expand Up @@ -284,7 +278,7 @@ impl<'a> DataPipeline<'a> {
let per_page = 5;
page += 1;

let mut fetch_result = github::FetchResult {
let mut fetch_result = github::ReposFetchResult {
items: Vec::<RepoSearchResultItem>::new(),
total: 0,
retry: false,
Expand Down Expand Up @@ -319,7 +313,7 @@ impl<'a> DataPipeline<'a> {
println!("\n{}: {:?}", "There was an error".red(), error);
let items = Vec::<RepoSearchResultItem>::new();
let total: i64 = 0;
github::FetchResult {
github::ReposFetchResult {
items,
total,
retry: false,
Expand Down
4 changes: 3 additions & 1 deletion src/data_pipeline/mongo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub fn main(collections: Collections, collection_arg: Option<String>) {
}

/// Supported collections.
type Collections<'a> = [&'a str; 1];
pub type Collections<'a> = [&'a str; 1];
/// Supported collections.
pub const COLLECTIONS: Collections = ["repos"];

struct DataPipelineMongoDb<'a> {
collections: Collections<'a>,
Expand Down

0 comments on commit 584e560

Please sign in to comment.