-
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.
variable: Add default behavior to VarIdx for str parsing
- Loading branch information
1 parent
1c02996
commit 1c82d9f
Showing
4 changed files
with
65 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod dummy_variables; | ||
mod dummy_variable; | ||
mod var_idx; | ||
|
||
pub use self::dummy_variables::DummyVariables; | ||
pub use self::dummy_variable::DummyVariable; | ||
pub use self::var_idx::VarIdx; |
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,34 @@ | ||
use std::convert::From; | ||
use std::str::FromStr; | ||
use convert_ref::TryFromRef; | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct VarIdx(usize); | ||
|
||
#[derive(Debug)] | ||
pub enum VarIdxErr<'a, E> { | ||
InvalidVariableName(&'a str), | ||
ConvertErr(E), | ||
} | ||
|
||
impl<'a> TryFromRef<&'a str> for VarIdx { | ||
type Err = VarIdxErr<'a, <usize as FromStr>::Err>; | ||
|
||
fn try_from_ref(s: &&'a str) -> Result<Self, Self::Err> { | ||
match s.chars().next() { | ||
Some('$') => { | ||
match FromStr::from_str(&s[1..]) { | ||
Ok(n) => Ok(VarIdx(n)), | ||
Err(err) => Err(VarIdxErr::ConvertErr(err)), | ||
} | ||
}, | ||
_ => Err(VarIdxErr::InvalidVariableName(s)), | ||
} | ||
} | ||
} | ||
|
||
impl From<VarIdx> for usize { | ||
fn from(var_idx: VarIdx) -> Self { | ||
var_idx.0 | ||
} | ||
} |