Page MenuHomePhorge

args.rs
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None
pub trait Validatable {
fn from_id(id: usize) -> Self;
fn from_string(s: String) -> Self;
fn get_string(&self) -> Option<&str>;
}
#[derive(Clone)]
pub enum Args {
Id(usize),
Script(String),
}
#[derive(Clone)]
pub enum Item {
Id(usize),
Name(String),
}
impl Validatable for Args {
fn from_id(id: usize) -> Self { Args::Id(id) }
fn from_string(s: String) -> Self { Args::Script(s) }
fn get_string(&self) -> Option<&str> {
match self {
Args::Id(_) => None,
Args::Script(s) => Some(s),
}
}
}
impl Validatable for Item {
fn from_id(id: usize) -> Self { Item::Id(id) }
fn from_string(s: String) -> Self { Item::Name(s) }
fn get_string(&self) -> Option<&str> {
match self {
Item::Id(_) => None,
Item::Name(s) => Some(s),
}
}
}
pub fn validate<T: Validatable>(s: &str) -> Result<T, String> {
if let Ok(id) = s.parse::<usize>() {
Ok(T::from_id(id))
} else {
Ok(T::from_string(s.to_owned()))
}
}

File Metadata

Mime Type
text/plain
Expires
Sun, Feb 1, 1:24 PM (1 d, 6 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
491150
Default Alt Text
args.rs (1 KB)

Event Timeline