u2400_pathnode 0.1.6

path cluster base library
Documentation
use async_trait::async_trait;
use anyhow::{Ok, Result};
use super::path_node::{Context, PathNode, VoterType, CheckerType};
use super::util::{NodeTokens, self};

#[async_trait]
pub trait IndexNode {
    async fn new<T: IndexNode>(name: String) -> Result<T>;

    fn add_path(&mut self, path: &str) -> Result<()>;
    fn inner_add_path(root_node: &mut PathNode, path: &str, voter: VoterType, checker: CheckerType) -> Result<()> {
        // path转换为 node_name_list: Vec<&str>
        let node_vec = Self::path_to_node_list(path);
        let node_slice = &node_vec[..];
        let mut context = Context::new();
        root_node.add_by_node_list(node_slice, &mut context, voter, checker)?;
        Ok(())
    }

    fn add_success_match_path(&mut self, path: &str) -> Result<()>;
    fn inner_add_success_match_path(root_node: &mut PathNode, path: &str, voter: VoterType, checker: CheckerType) -> Result<()> {
        let node_vec = Self::path_to_node_list(path);
        let node_slice = &node_vec[..];

        let mut context = Context::new();
        context.is_need_cluster = false;

        root_node.add_by_node_list(node_slice, &mut context, voter, checker)?;
        Ok(())
    }

    fn set_token(&mut self, path: &str, token_type: NodeTokens, clear_node: bool) -> Result<()>;
    fn inner_set_token(root_node: &mut PathNode, path: &str, token_type: NodeTokens, clear_node: bool) -> Result<()> {
        let node_vec = Self::path_to_node_list(path);
        let node_slice = &node_vec[..];

        let mut context = Context::new();
        root_node.set_token_by_node_list(node_slice, token_type, clear_node, &mut context)
    }

    fn try_match_rule(root_node: &mut PathNode, path: &str) -> Result<String>;
    fn inner_try_match_rule(root_node: &mut PathNode, path: &str) -> Result<String> {
        let node_vec = Self::path_to_node_list(path);
        let node_slice = &node_vec[..];

        match root_node.try_match_rule(node_slice) {
            std::result::Result::Ok(mut path) => {
                path = path[..path.len() - 1].to_string();
                Ok(path)
            }
            Err(e) => Err(e),
        }
    }

    fn merge_node(&mut self, path: &str) -> Result<()>;
    fn inner_merge_node(root_node: &mut PathNode, path: &str) -> Result<()> {
        let node_vec = Self::path_to_node_list(path);
        let node_slice = &node_vec[..];
        let mut context = Context::new();
        root_node.merge_by_node_list(node_slice, &mut context)
    }

    fn path_to_node_list<'a>(path: &'a str) -> Vec<&'a str> {
        util::path_to_node_list(path)
    }
}