pub fn from_markdown(doc: &str) -> anyhow::Result<Self> {
        #[derive(Debug, PartialEq)]
        enum ParseState {
            Initial,
            DateFound,
            TaskSectionStart,
            TaskSectionEnd,
            ScheduleSectionStart,
            ScheduleSectionEnd,
        }

        let mut parse_state = ParseState::Initial;
        let arena = Arena::new();
        let root = parse_document(&arena, doc, &ComrakOptions::default());
        let nodes = root.children();

        let mut tasks = None;
        let mut schedule = None;

        let mut date_str = "".to_string();

        for node in nodes {
            match parse_state {
                ParseState::Initial => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    let header = match node.data.to_owned().into_inner().value {
                        NodeValue::Heading(c) => c,
                        _ => continue,
                    };

                    if header.level != 1 {
                        continue;
                    }

                    date_str = get_node_text(node);
                    parse_state = ParseState::DateFound;
                    continue;
                }
                ParseState::DateFound => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    let header = match node.data.to_owned().into_inner().value {
                        NodeValue::Heading(c) => c,
                        _ => continue,
                    };

                    if header.level != 2 {
                        continue;
                    }

                    let header_text = get_node_text(node);

                    if header_text.trim().to_lowercase() == "tasks" {
                        parse_state = ParseState::TaskSectionStart
                    }
                    continue;
                }
                ParseState::TaskSectionStart => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    match node.data.to_owned().into_inner().value {
                        NodeValue::List(_) => {
                            tasks = Some(parse_task_list(node)?);
                            parse_state = ParseState::TaskSectionEnd;
                        }
                        _ => continue,
                    };
                }
                ParseState::TaskSectionEnd => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    let header = match node.data.to_owned().into_inner().value {
                        NodeValue::Heading(c) => c,
                        _ => continue,
                    };

                    if header.level != 2 {
                        continue;
                    }

                    let header_text = get_node_text(node);

                    if header_text.trim().to_lowercase() == "schedule" {
                        parse_state = ParseState::ScheduleSectionStart
                    }
                    continue;
                }
                ParseState::ScheduleSectionStart => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    match node.data.to_owned().into_inner().value {
                        NodeValue::List(_) => {
                            schedule = Some(parse_schedule(node)?);
                            parse_state = ParseState::ScheduleSectionEnd;
                        }
                        _ => continue,
                    };
                }
                ParseState::ScheduleSectionEnd => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    break;
                }
            }
        }

        let date = chrono::NaiveDate::parse_from_str(date_str.trim(), "%m/%d/%Y")?;

        Ok(Plan {
            date,
            tasks,
            schedule,
        })
    }
/// Creates a [Plan] from a markdown document.
Creates a [Plan] from a markdown document.
Creates a [ Plan ] from a markdown document .
pub fn from_markdown(doc: &str) -> anyhow::Result<Self> {
        #[derive(Debug, PartialEq)]
        enum ParseState {
            Initial,
            DateFound,
            TaskSectionStart,
            TaskSectionEnd,
            ScheduleSectionStart,
            ScheduleSectionEnd,
        }
        let mut parse_state = ParseState::Initial;
        let arena = Arena::new();
        let root = parse_document(&arena, doc, &ComrakOptions::default());
        let nodes = root.children();
        let mut tasks = None;
        let mut schedule = None;
        let mut date_str = "".to_string();
        for node in nodes {
            match parse_state {
                ParseState::Initial => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    let header = match node.data.to_owned().into_inner().value {
                        NodeValue::Heading(c) => c,
                        _ => continue,
                    };
                    if header.level != 1 {
                        continue;
                    }
                    date_str = get_node_text(node);
                    parse_state = ParseState::DateFound;
                    continue;
                }
                ParseState::DateFound => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    let header = match node.data.to_owned().into_inner().value {
                        NodeValue::Heading(c) => c,
                        _ => continue,
                    };
                    if header.level != 2 {
                        continue;
                    }
                    let header_text = get_node_text(node);
                    if header_text.trim().to_lowercase() == "tasks" {
                        parse_state = ParseState::TaskSectionStart
                    }
                    continue;
                }
                ParseState::TaskSectionStart => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    match node.data.to_owned().into_inner().value {
                        NodeValue::List(_) => {
                            tasks = Some(parse_task_list(node)?);
                            parse_state = ParseState::TaskSectionEnd;
                        }
                        _ => continue,
                    };
                }
                ParseState::TaskSectionEnd => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    let header = match node.data.to_owned().into_inner().value {
                        NodeValue::Heading(c) => c,
                        _ => continue,
                    };
                    if header.level != 2 {
                        continue;
                    }
                    let header_text = get_node_text(node);
                    if header_text.trim().to_lowercase() == "schedule" {
                        parse_state = ParseState::ScheduleSectionStart
                    }
                    continue;
                }
                ParseState::ScheduleSectionStart => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    match node.data.to_owned().into_inner().value {
                        NodeValue::List(_) => {
                            schedule = Some(parse_schedule(node)?);
                            parse_state = ParseState::ScheduleSectionEnd;
                        }
                        _ => continue,
                    };
                }
                ParseState::ScheduleSectionEnd => {
                    log::trace!("Parsing state: {:#?}", parse_state);
                    break;
                }
            }
        }
        let date = chrono::NaiveDate::parse_from_str(date_str.trim(), "%m/%d/%Y")?;
        Ok(Plan {
            date,
            tasks,
            schedule,
        })
    }
pub fn from_markdown ( doc : & str ) -> anyhow :: Result < Self > { # [ derive ( Debug PartialEq ) ] enum ParseState { Initial , DateFound , TaskSectionStart , TaskSectionEnd , ScheduleSectionStart , ScheduleSectionEnd , } let mut parse_state = ParseState :: Initial ; let arena = Arena :: new ( ) ; let root = parse_document ( & arena , doc , & ComrakOptions :: default ( ) ) ; let nodes = root . children ( ) ; let mut tasks = None ; let mut schedule = None ; let mut date_str = " " . to_string ( ) ; for node in nodes { match parse_state { ParseState :: Initial => { log :: trace ! ( " " parse_state ) ; let header = match node . data . to_owned ( ) . into_inner ( ) . value { NodeValue :: Heading ( c ) => c , _ => continue , } ; if header . level != 1 { continue ; } date_str = get_node_text ( node ) ; parse_state = ParseState :: DateFound ; continue ; } ParseState :: DateFound => { log :: trace ! ( " " parse_state ) ; let header = match node . data . to_owned ( ) . into_inner ( ) . value { NodeValue :: Heading ( c ) => c , _ => continue , } ; if header . level != 2 { continue ; } let header_text = get_node_text ( node ) ; if header_text . trim ( ) . to_lowercase ( ) == " " { parse_state = ParseState :: TaskSectionStart } continue ; } ParseState :: TaskSectionStart => { log :: trace ! ( " " parse_state ) ; match node . data . to_owned ( ) . into_inner ( ) . value { NodeValue :: List ( _ ) => { tasks = Some ( parse_task_list ( node ) ? ) ; parse_state = ParseState :: TaskSectionEnd ; } _ => continue , } ; } ParseState :: TaskSectionEnd => { log :: trace ! ( " " parse_state ) ; let header = match node . data . to_owned ( ) . into_inner ( ) . value { NodeValue :: Heading ( c ) => c , _ => continue , } ; if header . level != 2 { continue ; } let header_text = get_node_text ( node ) ; if header_text . trim ( ) . to_lowercase ( ) == " " { parse_state = ParseState :: ScheduleSectionStart } continue ; } ParseState :: ScheduleSectionStart => { log :: trace ! ( " " parse_state ) ; match node . data . to_owned ( ) . into_inner ( ) . value { NodeValue :: List ( _ ) => { schedule = Some ( parse_schedule ( node ) ? ) ; parse_state = ParseState :: ScheduleSectionEnd ; } _ => continue , } ; } ParseState :: ScheduleSectionEnd => { log :: trace ! ( " " parse_state ) ; break ; } } } let date = chrono :: NaiveDate :: parse_from_str ( date_str . trim ( ) , " " ) ? ; Ok ( Plan { date , tasks , schedule , } ) }
Creates a [Plan] from a markdown document.
Creates a [ Plan ] from a markdown document .
doc
&str
doc
&str