cursive/src/view/view_path.rs

37 lines
858 B
Rust
Raw Normal View History

/// Represents a path to a single view in the layout.
pub struct ViewPath {
/// List of turns to make on decision nodes when descending the view tree.
/// Simple nodes (with one fixed child) are skipped.
pub path: Vec<usize>,
}
2016-06-28 05:40:11 +00:00
impl Default for ViewPath {
fn default() -> Self {
Self::new()
}
}
impl ViewPath {
/// Creates a new empty path.
pub fn new() -> Self {
2016-03-15 22:37:57 +00:00
ViewPath { path: Vec::new() }
}
2015-05-16 21:02:15 +00:00
2015-05-20 00:31:52 +00:00
/// Creates a path from the given item.
2015-05-16 21:02:15 +00:00
pub fn from<T: ToPath>(path: T) -> Self {
path.to_path()
}
}
2016-06-28 05:40:11 +00:00
/// Generic trait for elements that can be converted into a `ViewPath`.
2015-05-16 21:02:15 +00:00
pub trait ToPath {
2015-05-20 00:31:52 +00:00
/// Creates a path from the element.
2015-05-16 21:02:15 +00:00
fn to_path(self) -> ViewPath;
}
2016-06-25 23:36:22 +00:00
impl<'a> ToPath for &'a [usize] {
2015-05-16 21:02:15 +00:00
fn to_path(self) -> ViewPath {
2016-03-15 22:37:57 +00:00
ViewPath { path: self.to_owned() }
2015-05-16 21:02:15 +00:00
}
}