-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from xoen/removed-duplication
Simplified public interface and removed duplication
- Loading branch information
Showing
4 changed files
with
103 additions
and
134 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
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,37 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::Region; | ||
|
||
/// Carbon intensity target, e.g. a postcode or a region | ||
pub enum Target { | ||
// NATIONAL, | ||
Postcode(String), | ||
Region(Region), | ||
} | ||
|
||
impl FromStr for Target { | ||
type Err = (); | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
//"" => Ok(Target::NATIONAL) | ||
|
||
// Check if input can be parsed as a Region | ||
if let Ok(region) = s.parse::<Region>() { | ||
return Ok(Target::Region(region)); | ||
} | ||
|
||
// Assumes the string was a postcode | ||
Ok(Target::Postcode(s.to_string())) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for Target { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let target = match self { | ||
Target::Postcode(postcode) => format!("postcode {postcode}"), | ||
Target::Region(region) => region.to_string(), | ||
}; | ||
|
||
write!(f, "{target}") | ||
} | ||
} |