use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Source {
Frontmatter,
Inline,
Computed,
}
impl Source {
pub const ALL: [Source; 3] = [Source::Frontmatter, Source::Inline, Source::Computed];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Source::Frontmatter => "frontmatter",
Source::Inline => "inline",
Source::Computed => "computed",
}
}
#[must_use]
pub const fn rank(self) -> u8 {
match self {
Source::Frontmatter => 0,
Source::Inline => 1,
Source::Computed => 2,
}
}
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
Self::ALL.into_iter().find(|v| v.as_str() == s)
}
}
impl fmt::Display for Source {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Card {
Scalar,
List,
}
impl Card {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Card::Scalar => "scalar",
Card::List => "list",
}
}
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
match s {
"scalar" => Some(Card::Scalar),
"list" => Some(Card::List),
_ => None,
}
}
}
impl fmt::Display for Card {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ValueType {
String,
Number,
Bool,
Null,
Json,
}
impl ValueType {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
ValueType::String => "string",
ValueType::Number => "number",
ValueType::Bool => "bool",
ValueType::Null => "null",
ValueType::Json => "json",
}
}
#[must_use]
pub fn parse(s: &str) -> Option<Self> {
match s {
"string" => Some(ValueType::String),
"number" => Some(ValueType::Number),
"bool" => Some(ValueType::Bool),
"null" => Some(ValueType::Null),
"json" => Some(ValueType::Json),
_ => None,
}
}
}
impl fmt::Display for ValueType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Typed {
pub ty: ValueType,
pub val_text: Option<String>,
pub val_num: Option<f64>,
pub val_bool: Option<bool>,
pub val_json: Option<String>,
}
impl Typed {
#[must_use]
pub const fn null() -> Self {
Self {
ty: ValueType::Null,
val_text: None,
val_num: None,
val_bool: None,
val_json: None,
}
}
#[must_use]
pub fn bool(b: bool) -> Self {
Self {
ty: ValueType::Bool,
val_bool: Some(b),
..Self::null()
}
}
#[must_use]
pub fn number(n: f64) -> Self {
Self {
ty: ValueType::Number,
val_num: Some(n),
..Self::null()
}
}
#[must_use]
pub fn string(s: impl Into<String>) -> Self {
Self {
ty: ValueType::String,
val_text: Some(s.into()),
..Self::null()
}
}
#[must_use]
pub fn json(text: impl Into<String>) -> Self {
Self {
ty: ValueType::Json,
val_json: Some(text.into()),
..Self::null()
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct FlatRow {
pub key: String,
pub card: Card,
pub ord: u32,
pub typed: Typed,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyRow {
pub prop_id: String,
pub block_id: Option<String>,
pub source: Source,
pub key: String,
pub card: Card,
pub ord: u32,
pub ty: ValueType,
pub val_text: Option<String>,
pub val_num: Option<f64>,
pub val_bool: Option<bool>,
pub val_json: Option<String>,
}