Rail开发日志_3

发布于:2025-07-13 ⋅ 阅读:(18) ⋅ 点赞:(0)

上一篇笔记概括了实例化方法。

目标是root

某些人连栈都不知道什么意思,真的是边学边写,开发过程非常复杂,历时15个月。不做解释。

use std::mem;
const OMMISION: &str = "/";
const CATEGORY: &str = "+";
#[derive(Debug, Clone)]
enum Line {
    PlaceHolder,
    Comment { content: String },
    Category { name: String },
}
#[derive(Debug)]
enum Node {
    PlaceHolder,
    Comment { content: String },
    Domain { category: Line, rails: Vec<Node> },
}
struct Root {
    name: String,
    rails: Vec<Node>,
}


fn into_tokens(lines_input: &str) -> (Vec<Vec<String>>, Vec<usize>) {
    let mut word_current = String::new();
    let mut line_current = Vec::new();
    let mut lines_list_splited = Vec::new();
    let mut indent_current = 0;
    let mut indent_list = Vec::new();
    let mut indent_state_in = true;

    for char in lines_input.chars() {
        match char {
            '\n' => {
                if !word_current.is_empty() {
                    line_current.push(word_current.clone());
                    word_current.clear();
                }
                if !line_current.is_empty() {
                    lines_list_splited.push(line_current.clone());
                    indent_list.push(indent_current);
                }

                line_current.clear();
                indent_current = 0;
                indent_state_in = true;
            }
            ' ' => {
                if indent_state_in {
                    indent_current += 1;
                } else if !word_current.is_empty() {
                    line_current.push(word_current.clone());
                    word_current.clear();
                }
            }
            other => {
                indent_state_in = false;
                word_current.push(other);
            }
        }
    }

    if !word_current.is_empty() {
        line_current.push(word_current);
    }
    if !line_current.is_empty() {
        lines_list_splited.push(line_current);
        indent_list.push(indent_current);
    }

    let indent_list = indent_list.into_iter().map(|x| x / 2).collect();
    (lines_list_splited, indent_list)
}
fn into_lines(token_list: &Vec<Vec<String>>, range: usize) -> Vec<Line> {
    let mut local = Vec::new();

    for i in 0..range {
        let token = &token_list[i];
        let lenth = token.len();
        let data = match lenth {
            1 => match token[0].as_str() {
                OMMISION => Line::PlaceHolder,
                _ => continue,
            },
            2 => match token[0].as_str() {
                OMMISION => Line::Comment { content: token[1].clone() },
                CATEGORY => Line::Category { name: token[1].clone() },
                _ => continue,
            },
            _ => continue,
        };
        local.push(data);
    }

    local
}
fn into_nodes(domain_name: &str, indent_list: Vec<usize>, line_list: Vec<Line>, range: usize) -> Root {
    let mut root = Root { name: domain_name.to_string(), rails: Vec::new() };
    let mut stack: Vec<Vec<Node>> = vec![vec![]];
    let mut context = 0;
    for i in 0..range {
        let indent = indent_list[i];
        let indent_move = indent as isize - context as isize;
        let line = &line_list[i];
        if indent_move < 0 {
            for _ in 0..-indent_move {
                if let Some(child_layer) = stack.pop() {
                    if let Some(parent_layer) = stack.last_mut() {
                        if let Some(Node::Domain { rails, .. }) = parent_layer.last_mut() {
                            // 这是在干嘛?
                            *rails = child_layer;
                        }
                    }
                }
            }
        }
        let node = match line {
            Line::PlaceHolder => Node::PlaceHolder,
            Line::Comment { content } => Node::Comment { content: content.clone() },
            Line::Category { name } => Node::Domain {
                category: Line::Category { name: name.clone() },
                rails: Vec::new(),
            },
        };
        if indent_move > 0 {
            if let Some(current_layer) = stack.last_mut() {
                if let Some(Node::Domain { rails, .. }) = current_layer.last_mut() {
                    // 看不懂
                    let new_layer = mem::take(rails);
                    stack.push(new_layer);
                }
            }
        }
        if let Some(current_layer) = stack.last_mut() {
            current_layer.push(node);
        }

        context = indent;
    }
    while let Some(child_layer) = stack.pop() {
        if let Some(parent_layer) = stack.last_mut() {
            if let Some(Node::Domain { rails, .. }) = parent_layer.last_mut() {
                *rails = child_layer;
            }
        } else {
            root.rails = child_layer;
        }
    }
    root
}

fn print_node(node: &Node, indent: usize) {
    let indent_str = "_".repeat(indent);
    match node {
        Node::PlaceHolder => println!("{indent_str}/"),
        Node::Comment { content } => println!("{indent_str}/ {}", content),
        Node::Domain { category, rails } => {
            match category {
                Line::Category { name } => println!("{indent_str}+ {}", name),
                _ => {}
            }
            for child in rails {
                print_node(child, indent + 2);
            }
        }
    }
}
fn check_nodes(root: &Root) {
    println!("[{}]", root.name);
    for node in &root.rails {
        print_node(node, 0);
    }
}
fn main() {
    test(
        String::from("
/ ......
/ some_text
/
+ test1
  / ...
  / ...
  / ...
  + test2
    / ...
    + test3
  / ..
  + test4
/ ......
/
")
    );
}
fn test(input: String) {

    let (line_list, indent_list) = into_tokens(&input);
    let range = line_list.len();

    let lines = into_lines(&line_list, range);

    let root = into_nodes("local", indent_list, lines, range);
    check_nodes(&root);
}

测试结果如下。

[local]
/ ......
/ some_text
/
+ test1
__/ ...
__/ ...
__/ ...
__+ test2
____/ ...
____+ test3
__/ ..
__+ test4
/ ......
/


网站公告

今日签到

点亮在社区的每一天
去签到