-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added functions that can be used in Rust code without the execu…
…table (#57) * Added a function that downloads input and a function that submits answers. Closes #12
- Loading branch information
Showing
20 changed files
with
220 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
[package] | ||
name = "elv" | ||
description = "A little CLI helper for Advent of Code. 🎄" | ||
version = "0.13.1" | ||
version = "0.13.2" | ||
authors = ["Konrad Pagacz <[email protected]>"] | ||
edition = "2021" | ||
readme = "README.md" | ||
|
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 |
---|---|---|
|
@@ -33,6 +33,8 @@ instead of the webpage. So far `elv` supports: | |
- guessing the year and day of a riddle based on the current date | ||
- caching `AoC` responses whenever possible, so you minimize your | ||
footprint on `AoC`'s servers | ||
- two functions that let you use `elv` as a library in your own | ||
`Rust`-based application or code | ||
|
||
## Installation | ||
|
||
|
@@ -158,6 +160,29 @@ brew uninstall kpagacz/elv/elv | |
brew autoremove | ||
``` | ||
|
||
## Library | ||
|
||
`elv` exposes a supremely small library that you can use in your scripts or | ||
applications. These include: | ||
* `elv::get_input` - a function that downloads the input for a given year and day | ||
* `elv::submit` - a function that submits the solution to a given year and day | ||
|
||
These functions have decent documentation that you can browse | ||
[here](https://docs.rs/elv/latest/elv/). Here is a small example from the docs: | ||
|
||
```rust | ||
// Will succeed if your token is set using another way | ||
get_input(1, 2023, None).unwrap() | ||
submit(20, 2019, "something", 2, Some("Mytoken")).unwrap(); | ||
``` | ||
|
||
You can also use the `Driver` object to perform even more actions, but | ||
this is not recommended as the API is not stable and may change in the | ||
future. The `Driver` struct is also poorly documented. | ||
|
||
Let me know at `[email protected]` or file an issue | ||
if you want to get more functions exposed in the library. | ||
|
||
## Examples | ||
|
||
You need an Advent of Code session token to interact with its API. `elv` | ||
|
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::{domain::riddle_part::RiddlePart, Configuration, Driver}; | ||
use anyhow::Result; | ||
|
||
/// Downloads the input from Advent of Code servers | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `day` - the day of the challenge. [1 - 25] | ||
/// * `year` - the year of the challenge. E.g. 2023 | ||
/// * `token` - optionally, the token used to authenticate you against AOC servers | ||
/// | ||
/// # Token | ||
/// | ||
/// You need the token to authenticate against the AOC servers. This function will not work | ||
/// without it. You can pass it directly to this function or set it via one of the other methods. | ||
/// See [the README](https://github.com/kpagacz/elv#faq) for more information. | ||
/// | ||
/// If you set the token using the CLI or in the configuration file, this function will reuse | ||
/// it and you will not need to additionally pass the token to the function. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use elv::get_input; | ||
/// fn download_input() -> String { | ||
/// // Will succeed if your token is set using another way | ||
/// get_input(1, 2023, None).unwrap() | ||
/// } | ||
/// fn download_input_with_token() -> String { | ||
/// // No need to set the token in any other way. | ||
/// get_input(1, 2023, Some("123456yourtoken")).unwrap() | ||
/// } | ||
/// ``` | ||
pub fn get_input(day: usize, year: usize, token: Option<&str>) -> Result<String> { | ||
let mut config = Configuration::new(); | ||
if let Some(token) = token { | ||
config.aoc.token = token.to_owned(); | ||
} | ||
|
||
let driver = Driver::new(config); | ||
driver.input(year, day) | ||
} | ||
|
||
/// Submits an answer to Advent of Code servers | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `day` - the day of the challenge. [1 - 25] | ||
/// * `year` - the year of the challenge. E.g. 2023 | ||
/// * `answer` - the submitted answer | ||
/// * `riddle_part` - either 1 or 2 indicating, respectively, part one and two of the riddle | ||
/// * `token` - optionally, the token used to authenticate you against AOC servers | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use elv::submit; | ||
/// fn submit_answer(answer: &str) { | ||
/// // Submits answer `12344` to the first part of thefirst day of the 2023 AOC. | ||
/// // This invocation will not work if you do not supply the token | ||
/// // some other way. | ||
/// submit(1, 2023, "12344", 1, None).unwrap(); | ||
/// // Submits answer `something` to the second part of the 20th day of the 2019 challenge. | ||
/// // This invocation does not need the token set any other way. | ||
/// submit(20, 2019, "something", 2, Some("Mytoken")).unwrap(); | ||
/// } | ||
/// ``` | ||
pub fn submit( | ||
day: usize, | ||
year: usize, | ||
answer: &str, | ||
riddle_part: u8, | ||
token: Option<&str>, | ||
) -> Result<()> { | ||
let mut config = Configuration::new(); | ||
if let Some(token) = token { | ||
config.aoc.token = token.to_owned(); | ||
} | ||
|
||
let driver = Driver::new(config); | ||
let part = match riddle_part { | ||
1 => RiddlePart::One, | ||
2 => RiddlePart::Two, | ||
_ => RiddlePart::One, | ||
}; | ||
driver.submit_answer(year, day, part, answer.to_owned())?; | ||
Ok(()) | ||
} |
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
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,16 +1,12 @@ | ||
use crate::{ | ||
domain::{ | ||
description::Description, submission::Submission, submission_result::SubmissionResult, | ||
}, | ||
infrastructure::aoc_api::aoc_client_impl::InputResponse, | ||
use crate::domain::{ | ||
description::Description, submission::Submission, submission_result::SubmissionResult, | ||
}; | ||
|
||
use super::errors::AocClientError; | ||
|
||
pub trait AocClient { | ||
fn submit_answer(&self, submission: Submission) -> Result<SubmissionResult, AocClientError>; | ||
fn get_description<Desc>(&self, year: i32, day: i32) -> Result<Desc, AocClientError> | ||
fn get_description<Desc>(&self, year: usize, day: usize) -> Result<Desc, AocClientError> | ||
where | ||
Desc: Description + TryFrom<reqwest::blocking::Response>; | ||
fn get_input(&self, year: i32, day: i32) -> InputResponse; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use anyhow::Result; | ||
|
||
pub trait GetInput { | ||
fn get_input(&self, day: usize, year: usize) -> Result<String>; | ||
} |
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
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
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.