use std::iter::Enumerate;
use std::path::{Path, PathBuf};
use std::str::from_utf8;
pub type FnBlockReturnType = (usize, isize, Option<Vec<AdvReturnValue>>);
pub type FnReadBlockType = Option<
Box<
dyn Fn(
Option<&[u8]>, &[u8], &mut Enumerate<std::slice::Iter<'_, u8>>,
usize, u8, &mut usize, u8, ) -> FnBlockReturnType
+ Send
+ 'static,
>,
>;
#[derive(PartialEq, Clone, Debug)]
pub enum ReaderState {
Default,
Number,
String,
LineComment,
Comment,
Block,
}
#[derive(Clone, Debug, PartialEq)]
pub enum AdvReturnValue {
Bytes(Vec<u8>),
String(Vec<u8>),
Comment(Vec<u8>),
LineComment(Vec<u8>),
StringUtf8(String),
CommentUtf8(String),
LineCommentUtf8(String),
Bool(bool),
Int(i64),
Float(f64),
Hex(i64),
Oct(i64),
Bin(i64),
Block(Vec<u8>),
}
impl AdvReturnValue {
pub fn as_string(&self) -> String {
match &self {
AdvReturnValue::Bytes(v) => format!("Bytes({:?})", from_utf8(v)),
AdvReturnValue::String(v) => format!("String({:?})", from_utf8(v)),
AdvReturnValue::Comment(v) => format!("Comment({:?})", from_utf8(v)),
AdvReturnValue::LineComment(v) => format!("LineComment({:?})", from_utf8(v)),
AdvReturnValue::StringUtf8(v) => format!("StringUtf8({v})"),
AdvReturnValue::CommentUtf8(v) => format!("CommentUtf8({v})"),
AdvReturnValue::LineCommentUtf8(v) => format!("LineCommentUtf8({v})"),
AdvReturnValue::Bool(v) => format!("Bool({v:?})"),
AdvReturnValue::Int(v) => format!("Int({v:?})"),
AdvReturnValue::Float(v) => format!("Float({v:?})"),
AdvReturnValue::Hex(v) => format!("Hex({v:?})"),
AdvReturnValue::Oct(v) => format!("Oct({v:?})"),
AdvReturnValue::Bin(v) => format!("Bin({v:?})"),
AdvReturnValue::Block(v) => format!("Block({:?})", from_utf8(v)),
}
}
}
#[derive(Debug)]
pub struct AdvReaderOptions {
pub path: PathBuf,
pub buffer_size: usize,
pub trim: bool,
pub line_end: u8,
pub skip_comments: bool,
pub encode_comments: bool,
pub encode_strings: bool,
pub encoder: Option<String>,
pub allow_invalid_utf8: bool,
pub extended_word_separation: bool,
pub double_quote_escape: bool,
pub convert2numbers: bool,
pub keep_base: bool,
pub bool_false: Option<Vec<u8>>,
pub bool_true: Option<Vec<u8>>,
}
impl AdvReaderOptions {
pub fn new(path: &Path, encoder: Option<String>) -> Self {
Self {
path: path.to_path_buf(),
buffer_size: 65536,
trim: false,
line_end: b'\n',
skip_comments: false,
encode_comments: true,
encode_strings: true,
encoder,
allow_invalid_utf8: true,
extended_word_separation: false,
double_quote_escape: false,
convert2numbers: true,
keep_base: false,
bool_false: None,
bool_true: None,
}
}
}