-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better separation of binary and library (#70)
* Create internal module for logic that isn't intended to be published as part of the library * Move all printing/formatting logic to the internal module * Move PublicHandle from main.rs to library * Make enum PuzzleType non-public * Completely refactor solution module * Move build command from solution to main.rs * Handle solution command failing to run * Add some docstrings for Clash * Change `.expect()` messages to comply with [the convention](https://doc.rust-lang.org/std/result/enum.Result.html#recommended-message-style) --------- Co-authored-by: ellnix <103502144+ellnix@users.noreply.github.com>
- Loading branch information
1 parent
0210067
commit ced9db8
Showing
13 changed files
with
476 additions
and
452 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::str::FromStr; | ||
|
||
use anyhow::anyhow; | ||
use serde::{Deserialize, Deserializer, Serialize}; | ||
|
||
/// `PublicHandle` is a hexadecimal string that uniquely identifies a clash | ||
/// or a puzzle. It is the last part of the URL when viewing a clash or a puzzle | ||
/// on the CodinGame contribution page. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use clashlib::clash::PublicHandle; | ||
/// use std::str::FromStr; | ||
/// | ||
/// let handle = PublicHandle::from_str("682102420fbce0fce95e0ee56095ea2b9924"); | ||
/// assert!(handle.is_ok()); | ||
/// let invalid_handle = PublicHandle::from_str("xyz"); | ||
/// assert!(invalid_handle.is_err()); | ||
/// ``` | ||
#[derive(Debug, Clone, Serialize)] | ||
pub struct PublicHandle(String); | ||
|
||
impl FromStr for PublicHandle { | ||
type Err = anyhow::Error; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
if s.chars().all(|ch| ch.is_ascii_hexdigit()) { | ||
Ok(PublicHandle(String::from(s))) | ||
} else { | ||
Err(anyhow!("valid handles only contain characters 0-9 and a-f")) | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for PublicHandle { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for PublicHandle { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
FromStr::from_str(&s).map_err(serde::de::Error::custom) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod formatter; | ||
mod lines_with_endings; | ||
mod outputstyle; | ||
|
||
pub use outputstyle::OutputStyle; |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// https://stackoverflow.com/a/40457615/5465108 | ||
pub struct LinesWithEndings<'a> { | ||
input: &'a str, | ||
} | ||
|
||
impl<'a> LinesWithEndings<'a> { | ||
pub fn from(input: &'a str) -> LinesWithEndings<'a> { | ||
LinesWithEndings { input } | ||
} | ||
} | ||
|
||
impl<'a> Iterator for LinesWithEndings<'a> { | ||
type Item = &'a str; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<&'a str> { | ||
if self.input.is_empty() { | ||
return None | ||
} | ||
let split = self.input.find('\n').map(|i| i + 1).unwrap_or(self.input.len()); | ||
let (line, rest) = self.input.split_at(split); | ||
self.input = rest; | ||
Some(line) | ||
} | ||
} |
Oops, something went wrong.