~vpzom/trout

eac7d5ff9ed6c3f5c3655860f4654d27f747f14d — Colin Reeder 1 year, 4 months ago a748fa0
Add with_child_str
2 files changed, 34 insertions(+), 2 deletions(-)

M Cargo.toml
M src/node.rs
M Cargo.toml => Cargo.toml +1 -1
@@ 1,6 1,6 @@
[package]
name = "trout"
version = "0.1.1"
version = "0.2.0"
authors = ["Colin Reeder <colin@vpzom.click>"]
edition = "2018"
license = "MIT"

M src/node.rs => src/node.rs +33 -1
@@ 81,6 81,25 @@ impl<
    }
}

struct ChildPathExtractSegmentStr<R: Request, T, C, P2>(Node<P2, R, T, C>);
impl<P1: 'static, R: Request + 'static, T: 'static, C: 'static, P2: TupleAdd<P1, String> + 'static> ChildPath<P1, R, T, C> for ChildPathExtractSegmentStr<R, T, C, P2> {
    fn route(&self, path: &str, ret: &mut TakeReturn<(P1, R, C), Result<T, RoutingFailure>>) {
        let (seg, remaining) = match path.find('/') {
            Some(idx) => {
                let (seg, remaining) = path.split_at(idx);
                let remaining = &remaining[1..];
                (seg, remaining)
            }
            None => (path, ""),
        };

        ret.take_return(|(props, req, context)| {
            let props = P2::tuple_add(props, seg.to_owned());
            self.0.route_inner(remaining, props, req, context)
        });
    }
}

/// The building block of a routing tree.
///
/// Each node can contain handlers, which will be used when this is the node


@@ 135,7 154,20 @@ impl<P: 'static, R: Request + 'static, T: 'static, C: 'static> Node<P, R, T, C> 
        self
    }

    /// Add a child node with a dynamic path segment
    /// Add a child node with a dynamic path segment (string form)
    /// # Example
    /// ```rust
    /// # let other_node = trout::Node::new();
    /// trout::Node::new()
    ///     .with_child_str(other_node)
    /// ```
    pub fn with_child_str<P2: TupleAdd<P, String> + 'static>(mut self, child: Node<P2, R, T, C>) -> Self {
        self.children.push(Box::new(ChildPathExtractSegmentStr(child)));

        self
    }

    /// Add a child node with a dynamic path segment (parsing form)
    /// # Example
    /// ```rust
    /// # let other_node = trout::Node::new();