-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scaffold: Custom formatter and start on init
- Loading branch information
1 parent
963a59f
commit 03deb5d
Showing
8 changed files
with
335 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,51 @@ | ||
use std::{ | ||
fs::{self, File}, | ||
io::Write, | ||
path::Path, | ||
}; | ||
|
||
use anyhow::Result; | ||
use url::Url; | ||
|
||
use crate::{ | ||
args::{Args, InitArgs}, | ||
formatter::Formatter, | ||
misc::current_year, | ||
session::{Authenticated, Session}, | ||
}; | ||
|
||
pub fn init(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> { | ||
write_input(session, cmd, args)?; | ||
Ok(()) | ||
} | ||
|
||
fn write_input(session: &Session, cmd: &InitArgs, args: &Args) -> Result<()> { | ||
let file_location = Formatter::new(&cmd.input_location)? | ||
.format::<&[_]>(&[("year", cmd.year), ("day", cmd.day as u16)])?; | ||
|
||
let path = Path::new(&file_location); | ||
if let Some(parent) = path.parent() { | ||
if !parent.exists() { | ||
fs::create_dir_all(parent)?; | ||
} | ||
} | ||
|
||
let mut file = File::create(path)?; | ||
let input = fetch_input(session, &args.address, cmd.day, Some(cmd.year))?; | ||
file.write_all(input.as_bytes())?; | ||
println!("[*] Wrote input to {file_location}"); | ||
Ok(()) | ||
} | ||
|
||
fn fetch_input(session: &Session, base: &Url, day: u8, year: Option<u16>) -> Result<String> { | ||
let year = year.unwrap_or_else(current_year); | ||
println!("[*] Fetching input for {day}/{year}"); | ||
|
||
use crate::session::Session; | ||
let url = base.join(&format!("{year}/day/{day}/input"))?; | ||
let body = ureq::get(url.as_str()) | ||
.authenticated(session) | ||
.call()? | ||
.into_string()?; | ||
|
||
pub fn init(session: &Session, day: u8, year: Option<u16>) -> Result<()> { | ||
todo!() | ||
Ok(body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.