Skip to content

Commit

Permalink
Delint.
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi committed Apr 25, 2022
1 parent dd487f3 commit 4c29d30
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 39 deletions.
58 changes: 39 additions & 19 deletions demo-hytradboi/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#![allow(dead_code)]

use std::{rc::Rc, sync::Arc, fs};
use std::{fs, rc::Rc, sync::Arc};

use git_url_parse::GitUrl;
use hn_api::{types::Item, HnClient};
use lazy_static::__Deref;
use octorust::types::{FullRepository, ContentFile};
use octorust::types::{ContentFile, FullRepository};
use tokio::runtime::Runtime;
use trustfall_core::{
interpreter::{Adapter, DataContext, InterpretedQuery},
ir::{EdgeParameters, Eid, FieldValue, Vid},
};

use crate::{
actions_parser::{get_env_for_run_step, get_jobs_in_workflow_file, get_steps_in_job},
pagers::{CratesPager, WorkflowsPager},
token::{Token, Repository},
util::{Pager, get_owner_and_repo}, actions_parser::{get_jobs_in_workflow_file, get_steps_in_job, get_env_for_run_step},
token::{Repository, Token},
util::{get_owner_and_repo, Pager},
};

const USER_AGENT: &str = "demo-hytradboi (github.com/obi1kenobi/trustfall)";
Expand All @@ -35,7 +36,8 @@ lazy_static! {
)),
)
.unwrap();
static ref REPOS_CLIENT: octorust::repos::Repos = octorust::repos::Repos::new(GITHUB_CLIENT.clone());
static ref REPOS_CLIENT: octorust::repos::Repos =
octorust::repos::Repos::new(GITHUB_CLIENT.clone());
static ref RUNTIME: Runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
Expand Down Expand Up @@ -329,27 +331,40 @@ impl Adapter<'static> for DemoAdapter {
}),

// properties on GitHubActionsJob
("GitHubActionsJob", "name") => impl_property!(data_contexts, as_github_actions_job, name),
("GitHubActionsJob", "runsOn") => impl_property!(data_contexts, as_github_actions_job, runs_on),
("GitHubActionsJob", "name") => {
impl_property!(data_contexts, as_github_actions_job, name)
}
("GitHubActionsJob", "runsOn") => {
impl_property!(data_contexts, as_github_actions_job, runs_on)
}

// properties on GitHubActionsStep and its implementers
("GitHubActionsStep" | "GitHubActionsImportedStep" | "GitHubActionsRunStep", "name") => impl_property!(data_contexts, as_github_actions_step, step_name, {
(
"GitHubActionsStep" | "GitHubActionsImportedStep" | "GitHubActionsRunStep",
"name",
) => impl_property!(data_contexts, as_github_actions_step, step_name, {
step_name.map(|x| x.to_string()).into()
}),

// properties on GitHubActionsImportedStep
("GitHubActionsImportedStep", "uses") => impl_property!(data_contexts, as_github_actions_imported_step, uses),
("GitHubActionsImportedStep", "uses") => {
impl_property!(data_contexts, as_github_actions_imported_step, uses)
}

// properties on GitHubActionsRunStep
("GitHubActionsRunStep", "run") => impl_property!(data_contexts, as_github_actions_run_step, run),
("GitHubActionsRunStep", "run") => {
impl_property!(data_contexts, as_github_actions_run_step, run)
}

// properties on NameValuePair
("NameValuePair", "name") => impl_property!(data_contexts, as_name_value_pair, pair, {
pair.0.clone().into()
}),
("NameValuePair", "value") => impl_property!(data_contexts, as_name_value_pair, pair, {
pair.1.clone().into()
}),
("NameValuePair", "value") => {
impl_property!(data_contexts, as_name_value_pair, pair, {
pair.1.clone().into()
})
}
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -531,13 +546,13 @@ impl Adapter<'static> for DemoAdapter {
Item::Comment(c) => {
comment_id = c.id;
parent_id = c.parent;
},
}
Item::Poll(..) | Item::Pollopt(..) => {
// Not supported, because HackerNews doesn't really
// run polls anymore, even though the API still supports them.
break Box::new(std::iter::empty());
}
}
},
Err(e) => {
eprintln!(
"API error while fetching comment {} parent {}: {}",
Expand Down Expand Up @@ -635,7 +650,8 @@ impl Adapter<'static> for DemoAdapter {
None => Box::new(std::iter::empty()),
Some(token) => Box::new(
WorkflowsPager::new(GITHUB_CLIENT.clone(), token, RUNTIME.deref())
.into_iter().map(|x| x.into())
.into_iter()
.map(|x| x.into()),
),
};

Expand Down Expand Up @@ -711,8 +727,12 @@ impl Adapter<'static> for DemoAdapter {
("Webpage", "Repository") => token.as_repository().is_some(),
("Webpage", "GitHubRepository") => token.as_github_repository().is_some(),
("Repository", "GitHubRepository") => token.as_github_repository().is_some(),
("GitHubActionsStep", "GitHubActionsImportedStep") => token.as_github_actions_imported_step().is_some(),
("GitHubActionsStep", "GitHubActionsRunStep") => token.as_github_actions_run_step().is_some(),
("GitHubActionsStep", "GitHubActionsImportedStep") => {
token.as_github_actions_imported_step().is_some()
}
("GitHubActionsStep", "GitHubActionsRunStep") => {
token.as_github_actions_run_step().is_some()
}
unhandled => unreachable!("{:?}", unhandled),
};

Expand Down Expand Up @@ -775,6 +795,6 @@ fn get_repo_file_content(repo: &FullRepository, path: &str) -> Option<ContentFil
owner, repo_name, main_branch, path, e
);
None
},
}
}
}
5 changes: 4 additions & 1 deletion demo-hytradboi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ fn execute_query(path: &str) {
.as_ref()
.clone()
.into_iter()
.map(|(k, v)| (k, serde_json::to_string_pretty(&TransparentValue::from(v)).unwrap()))
.map(|(k, v)| (
k,
serde_json::to_string_pretty(&TransparentValue::from(v)).unwrap()
))
.collect::<BTreeMap<_, _>>()
);

Expand Down
24 changes: 17 additions & 7 deletions demo-hytradboi/src/pagers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tokio::runtime::Runtime;

use crate::{
token::{RepoWorkflow, Token},
util::{Pager, PagerOutput, get_owner_and_repo},
util::{get_owner_and_repo, Pager, PagerOutput},
};

pub(crate) struct CratesPager<'a> {
Expand Down Expand Up @@ -86,13 +86,23 @@ impl<'a> Pager for WorkflowsPager<'a> {
if response.workflows.is_empty() {
PagerOutput::None
} else if response.workflows.len() == per_page as usize {
PagerOutput::Page(response.workflows.into_iter().map(|w| {
RepoWorkflow::new(repo_clone.repo.clone(), Rc::new(w))
}).collect::<Vec<_>>().into_iter())
PagerOutput::Page(
response
.workflows
.into_iter()
.map(|w| RepoWorkflow::new(repo_clone.repo.clone(), Rc::new(w)))
.collect::<Vec<_>>()
.into_iter(),
)
} else {
PagerOutput::KnownFinalPage(response.workflows.into_iter().map(|w| {
RepoWorkflow::new(repo_clone.repo.clone(), Rc::new(w))
}).collect::<Vec<_>>().into_iter())
PagerOutput::KnownFinalPage(
response
.workflows
.into_iter()
.map(|w| RepoWorkflow::new(repo_clone.repo.clone(), Rc::new(w)))
.collect::<Vec<_>>()
.into_iter(),
)
}
}
Err(e) => {
Expand Down
6 changes: 5 additions & 1 deletion demo-hytradboi/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ pub struct ActionsJob {

impl ActionsJob {
pub(crate) fn new(yaml: Yaml, name: String, runs_on: Option<String>) -> Self {
Self { yaml, name, runs_on }
Self {
yaml,
name,
runs_on,
}
}
}

Expand Down
19 changes: 10 additions & 9 deletions demo-hytradboi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ pub(crate) trait Pager {

fn get_page(&mut self, page: usize) -> PagerOutput<Self::Item>;

fn into_iter(self) -> PaginationIterator<Self::Item, Self> where Self : Sized {
fn into_iter(self) -> PaginationIterator<Self::Item, Self>
where
Self: Sized,
{
PaginationIterator::new(self)
}
}

pub(crate) struct PaginationIterator<T, P: Pager<Item=T>> {
pub(crate) struct PaginationIterator<T, P: Pager<Item = T>> {
pager: P,
next_page: usize,
batch: Option<std::vec::IntoIter<T>>,
final_page_seen: bool,
}

impl<T, P: Pager<Item=T>> PaginationIterator<T, P> {
impl<T, P: Pager<Item = T>> PaginationIterator<T, P> {
pub(crate) fn new(pager: P) -> Self {
Self {
pager,
Expand All @@ -34,7 +37,7 @@ impl<T, P: Pager<Item=T>> PaginationIterator<T, P> {
}
}

impl<T, P: Pager<Item=T>> Iterator for PaginationIterator<T, P> {
impl<T, P: Pager<Item = T>> Iterator for PaginationIterator<T, P> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -49,9 +52,7 @@ impl<T, P: Pager<Item=T>> Iterator for PaginationIterator<T, P> {
true
}
}
None => {
true
}
None => true,
};
if needs_next_page {
if self.final_page_seen {
Expand All @@ -65,10 +66,10 @@ impl<T, P: Pager<Item=T>> Iterator for PaginationIterator<T, P> {
PagerOutput::KnownFinalPage(batch) => {
self.final_page_seen = true;
self.batch = Some(batch);
},
}
PagerOutput::Page(batch) => {
self.batch = Some(batch);
},
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion trustfall_core/src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ impl From<FieldValue> for TransparentValue {
FieldValue::Boolean(x) => TransparentValue::Boolean(x),
FieldValue::DateTimeUtc(x) => TransparentValue::DateTimeUtc(x),
FieldValue::Enum(x) => TransparentValue::Enum(x),
FieldValue::List(x) => TransparentValue::List(x.into_iter().map(|v| v.into()).collect()),
FieldValue::List(x) => {
TransparentValue::List(x.into_iter().map(|v| v.into()).collect())
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion trustfall_core/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ fn check_property_and_edge_invariants(
// and this field is neither an edge nor a property.
unreachable!(
"field {} (type {}) appears to represent neither an edge nor a property",
field_defn.name.node.as_ref(), field_type.to_string(),
field_defn.name.node.as_ref(),
field_type.to_string(),
)
}
}
Expand Down

0 comments on commit 4c29d30

Please sign in to comment.