use crate::log::{Level, Log};
use std::collections::BTreeSet;
use std::default::Default;
use std::path;
use crate::style::{self, Style};
#[derive(Copy, Clone)]
pub enum ColorConfig {
Yes,
No,
IfTty,
}
#[derive(Clone)]
pub struct Session {
pub log: Log,
pub force_build: bool,
pub in_dir: Option<path::PathBuf>,
pub out_dir: Option<path::PathBuf>,
pub emit_rerun_directives: bool,
pub emit_comments: bool,
pub emit_whitespace: bool,
pub emit_report: bool,
pub color_config: ColorConfig,
pub max_errors: usize,
pub heading: Style,
pub ambig_symbols: Style,
pub observed_symbols: Style,
pub cursor_symbol: Style,
pub unobserved_symbols: Style,
pub terminal_symbol: Style,
pub nonterminal_symbol: Style,
pub hint_text: Style,
pub unit_test: bool,
pub features: Option<BTreeSet<String>>,
}
impl Session {
pub fn new() -> Session {
Session {
log: Log::new(Level::Informative),
in_dir: None,
out_dir: None,
force_build: false,
emit_rerun_directives: false,
emit_comments: false,
emit_whitespace: true,
emit_report: false,
color_config: ColorConfig::default(),
max_errors: 1,
heading: style::FG_WHITE.with(style::BOLD),
ambig_symbols: style::FG_WHITE,
observed_symbols: style::FG_BRIGHT_GREEN,
cursor_symbol: style::FG_BRIGHT_WHITE,
unobserved_symbols: style::FG_BRIGHT_RED,
terminal_symbol: style::BOLD,
nonterminal_symbol: style::DEFAULT,
hint_text: style::FG_BRIGHT_MAGENTA.with(style::BOLD),
unit_test: false,
features: Default::default(),
}
}
#[cfg(test)]
pub fn test() -> Session {
Session {
log: Log::new(Level::Debug),
in_dir: None,
out_dir: None,
force_build: false,
emit_rerun_directives: false,
emit_comments: false,
emit_whitespace: true,
emit_report: false,
color_config: ColorConfig::IfTty,
max_errors: 1,
heading: Style::new(),
ambig_symbols: Style::new(),
observed_symbols: Style::new(),
cursor_symbol: Style::new(),
unobserved_symbols: Style::new(),
terminal_symbol: Style::new(),
nonterminal_symbol: Style::new(),
hint_text: Style::new(),
unit_test: true,
features: Default::default(),
}
}
pub fn stop_after(&self, actual_errors: usize) -> bool {
self.max_errors != 0 && actual_errors >= self.max_errors
}
pub fn log<M>(&self, level: Level, message: M)
where
M: FnOnce() -> String,
{
self.log.log(level, message)
}
pub fn emit_rerun_directive(&self, path: &path::Path) {
if self.emit_rerun_directives {
if let Some(display) = path.to_str() {
println!("cargo:rerun-if-changed={}", display)
} else {
println!("cargo:warning=LALRPOP is unable to inform Cargo that {} is a dependency because its filename cannot be represented in UTF-8. This is probably because it contains an unpaired surrogate character on Windows. As a result, your build script will not be rerun when it changes.", path.to_string_lossy());
}
}
}
}
impl Default for Session {
fn default() -> Self {
Session::new()
}
}
impl Default for ColorConfig {
fn default() -> Self {
ColorConfig::IfTty
}
}