use std::path::{PathBuf,Path};
use std::collections::HashMap;
use core::error::Error;

mod lexer;

pub struct Tx {
    fragments: HashMap<String, Fragment>, 
}

enum FragmentSource {
    File {
        path: PathBuf,
        loaded: usize,
    }
}

struct Fragment {
    name: String,
    source: FragmentSource,
    sections: HashMap<String, Section>,
}

struct Section {
    name: String,
    ast_root: Token,
}

type Properties = HashMap<String, String>;

enum Token {
    Root(Vec<Token>),
    Fragment(String,Properties,Vec<Token>),
    Section(String,Properties,Vec<Token>),
    Text(String),

}

struct State {
}

impl Tx {
    pub fn new() -> Self {
        Self {
            fragments: HashMap::new(),
        }
    }

    pub fn load(path: &Path) -> Result<(),Box::<dyn Error>> {
        Ok(())
    }

    pub fn parse(data: String) -> Result<(),Box::<dyn Error>> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lexing() {
        let data = r#"Leading comment: [|fragment "basic"|>Hello there <|fragment|] Trailing text"#;
        let ll = lexer::Lexer::new( &data );
        for sym in ll {
            if let lexer::Symbol::Error{line,row,what} = sym {
                println!("Error {}:{}: {}", line, row, what );
                break;
            } else {
                println!("Symbol: {:?}", sym );
            }
        }
    }
}
