use typst_syntax::SyntaxNode;
use crate::core::typst2latex::utils::FuncArgs;
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum HLineStyle {
#[default]
Normal,
TopRule,
MidRule,
BottomRule,
}
#[derive(Debug, Clone)]
pub struct LatexHLine {
pub start: Option<usize>,
pub end: Option<usize>,
pub style: HLineStyle,
}
#[allow(dead_code)]
impl LatexHLine {
pub fn full() -> Self {
LatexHLine {
start: None,
end: None,
style: HLineStyle::Normal,
}
}
pub fn partial(start: usize, end: usize) -> Self {
LatexHLine {
start: Some(start),
end: Some(end),
style: HLineStyle::Normal,
}
}
pub fn top_rule() -> Self {
LatexHLine {
start: None,
end: None,
style: HLineStyle::TopRule,
}
}
pub fn mid_rule() -> Self {
LatexHLine {
start: None,
end: None,
style: HLineStyle::MidRule,
}
}
pub fn bottom_rule() -> Self {
LatexHLine {
start: None,
end: None,
style: HLineStyle::BottomRule,
}
}
pub fn from_typst_ast(node: &SyntaxNode) -> Self {
let children: Vec<_> = node.children().collect();
let args = FuncArgs::from_func_call(&children);
let start = args.named_usize("start");
let end = args.named_usize("end");
LatexHLine {
start,
end,
style: HLineStyle::Normal,
}
}
pub fn to_latex(&self) -> String {
match self.style {
HLineStyle::TopRule => "\\toprule".to_string(),
HLineStyle::MidRule => "\\midrule".to_string(),
HLineStyle::BottomRule => "\\bottomrule".to_string(),
HLineStyle::Normal => {
match (self.start, self.end) {
(Some(s), Some(e)) => {
format!("\\cline{{{}-{}}}", s + 1, e)
}
(Some(s), None) => {
format!("\\cline{{{}-}}", s + 1)
}
_ => "\\hline".to_string(),
}
}
}
}
pub fn to_latex_with_cols(&self, col_count: usize) -> String {
match self.style {
HLineStyle::TopRule => "\\toprule".to_string(),
HLineStyle::MidRule => "\\midrule".to_string(),
HLineStyle::BottomRule => "\\bottomrule".to_string(),
HLineStyle::Normal => match (self.start, self.end) {
(Some(s), Some(e)) => {
format!("\\cline{{{}-{}}}", s + 1, e)
}
(Some(s), None) => {
format!("\\cline{{{}-{}}}", s + 1, col_count)
}
(None, Some(e)) => {
format!("\\cline{{1-{}}}", e)
}
(None, None) => "\\hline".to_string(),
},
}
}
}