hlctl/src/hlwm/parser/traits.rs

68 lines
1.6 KiB
Rust

use super::ParseError;
pub trait FromStrings: Sized {
fn from_strings<I: Iterator<Item = String>>(s: I) -> Result<Self, ParseError>;
}
/// Same as [FromStrings] but can pass a hint to the function which can help
/// in some determinations (e.g. passing in the path of the attribute for
/// parsing a [crate::hlwm::Attribute] so it can use the path to determine the
/// type of the attribute)
pub trait FromStringsHint<Hint>: Sized {
fn from_strings_hint<I: Iterator<Item = String>>(s: I, hint: Hint) -> Result<Self, ParseError>;
}
pub trait FromCommandArgs: Sized {
fn from_command_args<S: Into<String>, I: Iterator<Item = S>>(
command: &str,
args: I,
) -> Result<Self, ParseError>;
}
pub trait ToOption: Sized {
fn to_option(self) -> Option<Self>;
}
impl<T> ToOption for Vec<T> {
fn to_option(self) -> Option<Self> {
match self.is_empty() {
false => Some(self),
true => None,
}
}
}
impl ToOption for &str {
fn to_option(self) -> Option<Self> {
match self.is_empty() {
false => Some(self),
true => None,
}
}
}
impl ToOption for String {
fn to_option(self) -> Option<Self> {
match self.is_empty() {
false => Some(self),
true => None,
}
}
}
pub trait Flip {
type Target;
fn flip(self) -> Self::Target;
}
impl<T, E> Flip for Option<Result<T, E>> {
type Target = Result<Option<T>, E>;
fn flip(self) -> Self::Target {
match self {
Some(r) => r.map(Some),
None => Self::Target::Ok(None),
}
}
}