Skip to content

Commit

Permalink
some nice refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-ardi committed Nov 25, 2023
1 parent 79dccb3 commit e9aec6b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion aoc-runner-derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn generator_impl(args: pm::TokenStream, input: pm::TokenStream) -> pm::Toke
panic!("cannot find output type for {}", fn_name)
};

let (special_type, out_t) = if let Some((ty, inner)) = utils::extract_result(&*out_t) {
let (special_type, out_t) = if let Some((ty, inner)) = utils::extract_result(&out_t) {
(Some(ty), Box::new(inner))
} else {
(None, out_t)
Expand Down
8 changes: 4 additions & 4 deletions aoc-runner-derive/src/out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ fn headers(map: &InnerMap, year: u32) -> pm2::TokenStream {
let traits_impl: pm2::TokenStream = map
.keys()
.map(|dp| {
let snake = to_snakecase(&dp);
let camel = to_camelcase(&dp);
let snake = to_snakecase(dp);
let camel = to_camelcase(dp);

quote! {
#[doc(hidden)]
Expand Down Expand Up @@ -220,7 +220,7 @@ fn read_infos() -> Result<DayParts, Box<dyn error::Error>> {
fn parse_lib_infos(infos: pm::TokenStream) -> Result<LibInfos, ()> {
let tokens: Vec<_> = infos.into_iter().collect();

if let pm::TokenTree::Ident(i) = tokens.get(0).ok_or(())? {
if let pm::TokenTree::Ident(i) = tokens.first().ok_or(())? {
if i.to_string() != "year" {
return Err(());
}
Expand Down Expand Up @@ -258,7 +258,7 @@ fn parse_main_infos(infos: pm::TokenStream) -> Result<MainInfos, ()> {
return Err(());
}

if let pm::TokenTree::Ident(i) = tokens.get(0).ok_or(())? {
if let pm::TokenTree::Ident(i) = tokens.first().ok_or(())? {
let ty = i.to_string();

Ok(match ty.as_ref() {
Expand Down
2 changes: 1 addition & 1 deletion aoc-runner-derive/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn runner_impl(args: pm::TokenStream, input: pm::TokenStream) -> pm::TokenSt
panic!()
};

let (special_type, out_t) = if let Some((ty, inner)) = extract_result(&*out_t) {
let (special_type, out_t) = if let Some((ty, inner)) = extract_result(&out_t) {
(Some(ty), Box::new(inner))
} else {
(None, out_t)
Expand Down
4 changes: 2 additions & 2 deletions aoc-runner-internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl FromStr for Day {

fn from_str(day: &str) -> Result<Self, Self::Err> {
let slice = if day.len() < 4 || &day[..3] != "day" {
&day[..]
day
} else {
&day[3..]
};
Expand Down Expand Up @@ -71,7 +71,7 @@ impl DayPart {

impl PartialOrd for DayPart {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(&other))
Some(self.cmp(other))
}
}

Expand Down
28 changes: 14 additions & 14 deletions cargo-aoc/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn execute_input(args: &Input) -> Result<(), Box<dyn Error>> {
let client = client.clone();
tasks.push(tokio::spawn(async move {
let date = AOCDate { day, year };
match download_input_async(date, &*client).await {
match download_input_async(date, &client).await {
Ok(_) => println!("Successfully downloaded day {day}"),
Err(e) => return eprintln!("{e}"),
};
Expand All @@ -78,7 +78,7 @@ pub fn execute_input(args: &Input) -> Result<(), Box<dyn Error>> {
}
match codegen(day) {
Ok(_) => println!("Successfully generated boilerplate for {day}"),
Err(e) => return eprintln!("{e}"),
Err(e) => eprintln!("{e}"),
}
}));
}
Expand Down Expand Up @@ -179,7 +179,7 @@ async fn download_input_async(
.text()
.await
.map_err(|e| format!("Can't convert response to text: {e:?}"))?;
let mut file = File::create(&filename)?;
let mut file = File::create(filename)?;
file.write_all(body.as_bytes())?;
Ok(())
}
Expand Down Expand Up @@ -209,7 +209,7 @@ fn download_input(date: AOCDate) -> Result<(), Box<dyn error::Error>> {
let formated_token = format!("session={}", token);

let response = client
.get(&date.request_url())
.get(date.request_url())
.header(USER_AGENT, CARGO_AOC_USER_AGENT)
.header(COOKIE, formated_token)
.send()?;
Expand All @@ -219,11 +219,11 @@ fn download_input(date: AOCDate) -> Result<(), Box<dyn error::Error>> {
let dir = date.directory();
// Creates the file-tree to store inputs
// TODO: Maybe use crate's infos to get its root in the filesystem ?
fs::create_dir_all(&dir)?;
fs::create_dir_all(dir)?;

// Gets the body from the response and outputs everything to a file
let body = response.text()?;
let mut file = File::create(&filename)?;
let mut file = File::create(filename)?;
file.write_all(body.as_bytes())?;
}
sc => return Err(format!(
Expand Down Expand Up @@ -312,13 +312,13 @@ pub fn execute_default(args: &Cli) -> Result<(), Box<dyn error::Error>> {

fs::create_dir_all("target/aoc/aoc-autobuild/src")
.expect("failed to create autobuild directory");
fs::write("target/aoc/aoc-autobuild/Cargo.toml", &cargo_content)
fs::write("target/aoc/aoc-autobuild/Cargo.toml", cargo_content)
.expect("failed to write Cargo.toml");
fs::write("target/aoc/aoc-autobuild/src/main.rs", &main_content)
fs::write("target/aoc/aoc-autobuild/src/main.rs", main_content)
.expect("failed to write src/main.rs");

let status = process::Command::new("cargo")
.args(&["run", "--release"])
.args(["run", "--release"])
.current_dir("target/aoc/aoc-autobuild")
.spawn()
.expect("Failed to run cargo")
Expand Down Expand Up @@ -431,7 +431,7 @@ pub fn execute_bench(args: &Bench) -> Result<(), Box<dyn error::Error>> {
.replace(
"{NAME}",
if let Some(n) = &dp.name {
&n
n
} else {
"(default)"
},
Expand Down Expand Up @@ -483,7 +483,7 @@ pub fn execute_bench(args: &Bench) -> Result<(), Box<dyn error::Error>> {
.replace(
"{NAME}",
if let Some(n) = &dp.name {
&n
n
} else {
"(default)"
},
Expand Down Expand Up @@ -523,16 +523,16 @@ pub fn execute_bench(args: &Bench) -> Result<(), Box<dyn error::Error>> {

fs::create_dir_all("target/aoc/aoc-autobench/benches")
.expect("failed to create autobench directory");
fs::write("target/aoc/aoc-autobench/Cargo.toml", &cargo_content)
fs::write("target/aoc/aoc-autobench/Cargo.toml", cargo_content)
.expect("failed to write Cargo.toml");
fs::write(
"target/aoc/aoc-autobench/benches/aoc_benchmark.rs",
&main_content,
main_content,
)
.expect("failed to write src/aoc_benchmark.rs");

let status = process::Command::new("cargo")
.args(&["bench"])
.args(["bench"])
.current_dir("target/aoc/aoc-autobench")
.spawn()
.expect("Failed to run cargo")
Expand Down
5 changes: 4 additions & 1 deletion cargo-aoc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ fn main() {

match subcommand {
SubCommands::Bench(arg) => execute_bench(&arg),
SubCommands::Credentials(arg) => Ok(execute_credentials(&arg)),
SubCommands::Credentials(arg) => {
execute_credentials(&arg);
Ok(())
},
SubCommands::Input(arg) => execute_input(&arg),
}
.unwrap()
Expand Down

0 comments on commit e9aec6b

Please sign in to comment.