use serde::Deserialize;
use super::openapi_types::OpenApiSpec;
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedDoc {
pub frontmatter: DocFrontmatter,
pub content: Vec<DocNode>,
pub raw_markdown: String,
}
#[derive(Debug, Clone, Default, Deserialize, PartialEq)]
pub struct DocFrontmatter {
#[serde(default)]
pub title: String,
#[serde(default)]
pub description: Option<String>,
#[serde(rename = "sidebarTitle")]
#[serde(default)]
pub sidebar_title: Option<String>,
#[serde(default)]
pub icon: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DocNode {
Markdown(String),
Callout(CalloutNode),
Card(CardNode),
CardGroup(CardGroupNode),
Tabs(TabsNode),
Steps(StepsNode),
AccordionGroup(AccordionGroupNode),
CodeBlock(CodeBlockNode),
CodeGroup(CodeGroupNode),
ParamField(ParamFieldNode),
ResponseField(ResponseFieldNode),
Expandable(ExpandableNode),
RequestExample(RequestExampleNode),
ResponseExample(ResponseExampleNode),
Update(UpdateNode),
OpenApi(OpenApiNode),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CalloutType {
Tip,
Note,
Warning,
Info,
}
impl CalloutType {
pub fn parse(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"tip" => Some(Self::Tip),
"note" => Some(Self::Note),
"warning" => Some(Self::Warning),
"info" => Some(Self::Info),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match self {
Self::Tip => "Tip",
Self::Note => "Note",
Self::Warning => "Warning",
Self::Info => "Info",
}
}
pub fn alert_class(&self) -> &'static str {
match self {
Self::Tip => "alert-success",
Self::Note => "alert-info",
Self::Warning => "alert-warning",
Self::Info => "alert-info",
}
}
pub fn icon_name(&self) -> &'static str {
match self {
Self::Tip => "lightbulb",
Self::Note => "info",
Self::Warning => "alert-triangle",
Self::Info => "info",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CalloutNode {
pub callout_type: CalloutType,
pub content: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CardNode {
pub title: String,
pub icon: Option<String>,
pub href: Option<String>,
pub content: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CardGroupNode {
pub cols: u8,
pub cards: Vec<CardNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TabNode {
pub title: String,
pub content: Vec<DocNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TabsNode {
pub tabs: Vec<TabNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct StepNode {
pub title: String,
pub content: Vec<DocNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct StepsNode {
pub steps: Vec<StepNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AccordionNode {
pub title: String,
pub icon: Option<String>,
pub content: Vec<DocNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AccordionGroupNode {
pub items: Vec<AccordionNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CodeBlockNode {
pub language: Option<String>,
pub code: String,
pub filename: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CodeGroupNode {
pub blocks: Vec<CodeBlockNode>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParamLocation {
Header,
Path,
Query,
Body,
}
impl ParamLocation {
pub fn parse(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"header" => Some(Self::Header),
"path" => Some(Self::Path),
"query" => Some(Self::Query),
"body" => Some(Self::Body),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match self {
Self::Header => "header",
Self::Path => "path",
Self::Query => "query",
Self::Body => "body",
}
}
pub fn badge_class(&self) -> &'static str {
match self {
Self::Header => "badge-warning",
Self::Path => "badge-primary",
Self::Query => "badge-info",
Self::Body => "badge-secondary",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ParamFieldNode {
pub name: String,
pub location: ParamLocation,
pub param_type: String,
pub required: bool,
pub default: Option<String>,
pub content: Vec<DocNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ResponseFieldNode {
pub name: String,
pub field_type: String,
pub required: bool,
pub content: String,
pub expandable: Option<ExpandableNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExpandableNode {
pub title: String,
pub fields: Vec<ResponseFieldNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RequestExampleNode {
pub blocks: Vec<CodeBlockNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ResponseExampleNode {
pub blocks: Vec<CodeBlockNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UpdateNode {
pub label: String,
pub description: String,
pub content: Vec<DocNode>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct OpenApiNode {
pub spec: OpenApiSpec,
pub tags: Option<Vec<String>>,
pub show_schemas: bool,
}