use serde::de::{Error as DeError, SeqAccess, Unexpected, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::BTreeMap;
use std::fmt;
#[derive(
Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum ReturnFormat {
#[default]
Raw,
Markdown,
Commonmark,
Text,
Xml,
Bytes,
Empty,
}
impl ReturnFormat {
pub const fn as_str(&self) -> &'static str {
match self {
ReturnFormat::Raw => "raw",
ReturnFormat::Markdown => "markdown",
ReturnFormat::Commonmark => "commonmark",
ReturnFormat::Text => "text",
ReturnFormat::Xml => "xml",
ReturnFormat::Bytes => "bytes",
ReturnFormat::Empty => "empty",
}
}
}
impl fmt::Display for ReturnFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ReturnFormatHandling {
Single(ReturnFormat),
Multi(Vec<ReturnFormat>),
}
impl Default for ReturnFormatHandling {
fn default() -> ReturnFormatHandling {
ReturnFormatHandling::Single(ReturnFormat::Raw)
}
}
impl From<ReturnFormat> for ReturnFormatHandling {
fn from(value: ReturnFormat) -> ReturnFormatHandling {
ReturnFormatHandling::Single(value)
}
}
impl From<Vec<ReturnFormat>> for ReturnFormatHandling {
fn from(value: Vec<ReturnFormat>) -> ReturnFormatHandling {
ReturnFormatHandling::Multi(value)
}
}
impl ReturnFormatHandling {
pub fn formats(&self) -> &[ReturnFormat] {
match self {
ReturnFormatHandling::Single(format) => std::slice::from_ref(format),
ReturnFormatHandling::Multi(formats) => formats,
}
}
pub fn contains(&self, format: ReturnFormat) -> bool {
self.formats().contains(&format)
}
}
impl Serialize for ReturnFormatHandling {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
ReturnFormatHandling::Single(format) => format.serialize(serializer),
ReturnFormatHandling::Multi(formats) => formats.serialize(serializer),
}
}
}
impl<'de> Deserialize<'de> for ReturnFormatHandling {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<ReturnFormatHandling, D::Error> {
struct HandlingVisitor;
impl<'de> Visitor<'de> for HandlingVisitor {
type Value = ReturnFormatHandling;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a return format, or an array of them")
}
fn visit_str<E: DeError>(self, value: &str) -> Result<ReturnFormatHandling, E> {
parse_format(value).map(ReturnFormatHandling::Single)
}
fn visit_seq<A: SeqAccess<'de>>(
self,
mut seq: A,
) -> Result<ReturnFormatHandling, A::Error> {
let mut formats = Vec::with_capacity(seq.size_hint().unwrap_or(2));
while let Some(format) = seq.next_element::<String>()? {
formats.push(parse_format::<A::Error>(&format)?);
}
Ok(ReturnFormatHandling::Multi(formats))
}
}
deserializer.deserialize_any(HandlingVisitor)
}
}
fn parse_format<E: DeError>(value: &str) -> Result<ReturnFormat, E> {
match value.trim().to_ascii_lowercase().as_str() {
"raw" => Ok(ReturnFormat::Raw),
"markdown" => Ok(ReturnFormat::Markdown),
"commonmark" => Ok(ReturnFormat::Commonmark),
"text" => Ok(ReturnFormat::Text),
"xml" => Ok(ReturnFormat::Xml),
"bytes" => Ok(ReturnFormat::Bytes),
"empty" => Ok(ReturnFormat::Empty),
_ => Err(E::invalid_value(
Unexpected::Str(value),
&"one of raw, markdown, commonmark, text, xml, bytes or empty",
)),
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum ExtractSelector {
Css(String),
XPath(String),
}
impl ExtractSelector {
pub fn css(expression: impl Into<String>) -> ExtractSelector {
ExtractSelector::Css(expression.into())
}
pub fn xpath(expression: impl Into<String>) -> ExtractSelector {
ExtractSelector::XPath(expression.into())
}
pub fn as_str(&self) -> &str {
match self {
ExtractSelector::Css(expression) | ExtractSelector::XPath(expression) => expression,
}
}
pub fn parse(expression: &str) -> ExtractSelector {
let head = expression.trim_start();
if head.starts_with('/') || head.starts_with("./") || head.starts_with("(/") {
ExtractSelector::XPath(expression.to_string())
} else {
ExtractSelector::Css(expression.to_string())
}
}
}
impl fmt::Display for ExtractSelector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl Serialize for ExtractSelector {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for ExtractSelector {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<ExtractSelector, D::Error> {
let expression = String::deserialize(deserializer)?;
Ok(ExtractSelector::parse(&expression))
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SelectorGroup {
pub name: String,
pub selectors: Vec<ExtractSelector>,
}
impl SelectorGroup {
pub fn new(
name: impl Into<String>,
selectors: impl IntoIterator<Item = ExtractSelector>,
) -> SelectorGroup {
SelectorGroup {
name: name.into(),
selectors: selectors.into_iter().collect(),
}
}
pub fn css<S: Into<String>>(
name: impl Into<String>,
selectors: impl IntoIterator<Item = S>,
) -> SelectorGroup {
SelectorGroup::new(name, selectors.into_iter().map(ExtractSelector::css))
}
pub fn xpath<S: Into<String>>(
name: impl Into<String>,
selectors: impl IntoIterator<Item = S>,
) -> SelectorGroup {
SelectorGroup::new(name, selectors.into_iter().map(ExtractSelector::xpath))
}
}
pub type CssExtractionMap = BTreeMap<String, Vec<SelectorGroup>>;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum ChunkingKind {
#[default]
ByWords,
ByLines,
ByCharacterLength,
BySentence,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChunkingAlg {
pub r#type: ChunkingKind,
pub value: i32,
}
impl ChunkingAlg {
pub const fn new(kind: ChunkingKind, value: i32) -> ChunkingAlg {
ChunkingAlg {
r#type: kind,
value,
}
}
}