use crate::error::FoundationError;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[non_exhaustive]
#[repr(u8)]
pub enum BookmarkType {
#[default]
Point = 0,
SpanStart = 1,
SpanEnd = 2,
}
impl fmt::Display for BookmarkType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Point => f.write_str("Point"),
Self::SpanStart => f.write_str("SpanStart"),
Self::SpanEnd => f.write_str("SpanEnd"),
}
}
}
impl std::str::FromStr for BookmarkType {
type Err = FoundationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Point" | "point" => Ok(Self::Point),
"SpanStart" | "span_start" => Ok(Self::SpanStart),
"SpanEnd" | "span_end" => Ok(Self::SpanEnd),
_ => Err(FoundationError::ParseError {
type_name: "BookmarkType".to_string(),
value: s.to_string(),
valid_values: "Point, SpanStart, SpanEnd".to_string(),
}),
}
}
}
impl TryFrom<u8> for BookmarkType {
type Error = FoundationError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Point),
1 => Ok(Self::SpanStart),
2 => Ok(Self::SpanEnd),
_ => Err(FoundationError::ParseError {
type_name: "BookmarkType".to_string(),
value: value.to_string(),
valid_values: "0 (Point), 1 (SpanStart), 2 (SpanEnd)".to_string(),
}),
}
}
}
impl schemars::JsonSchema for BookmarkType {
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("BookmarkType")
}
fn json_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
gen.subschema_for::<String>()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[non_exhaustive]
#[repr(u8)]
pub enum FieldType {
#[default]
ClickHere = 0,
Author = 1,
LastSavedBy = 2,
CreatedTime = 3,
ModifiedTime = 4,
Title = 5,
}
impl FieldType {
#[must_use]
pub fn summary_token(self) -> Option<&'static str> {
match self {
Self::ClickHere => None,
Self::Author => Some("$author"),
Self::LastSavedBy => Some("$lastsaveby"),
Self::CreatedTime => Some("$createtime"),
Self::ModifiedTime => Some("$modifiedtime"),
Self::Title => Some("$title"),
}
}
#[must_use]
pub fn from_summary_token(token: &str) -> Option<Self> {
match token {
"$author" => Some(Self::Author),
"$lastsaveby" => Some(Self::LastSavedBy),
"$createtime" => Some(Self::CreatedTime),
"$modifiedtime" => Some(Self::ModifiedTime),
"$title" => Some(Self::Title),
_ => None,
}
}
#[must_use]
pub fn hwpx_editable(self) -> bool {
match self {
Self::ClickHere => true,
Self::Author | Self::Title => false,
Self::LastSavedBy | Self::CreatedTime | Self::ModifiedTime => true,
}
}
}
impl fmt::Display for FieldType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ClickHere => f.write_str("CLICK_HERE"),
Self::Author => f.write_str("AUTHOR"),
Self::LastSavedBy => f.write_str("LAST_SAVED_BY"),
Self::CreatedTime => f.write_str("CREATED_TIME"),
Self::ModifiedTime => f.write_str("MODIFIED_TIME"),
Self::Title => f.write_str("TITLE"),
}
}
}
impl std::str::FromStr for FieldType {
type Err = FoundationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"CLICK_HERE" | "ClickHere" | "click_here" => Ok(Self::ClickHere),
"AUTHOR" | "Author" | "author"
| "DOC_SUMMARY" | "DocSummary" | "doc_summary" => Ok(Self::Author),
"LAST_SAVED_BY" | "LastSavedBy" | "last_saved_by"
| "USER_INFO" | "UserInfo" | "user_info" => Ok(Self::LastSavedBy),
"CREATED_TIME" | "CreatedTime" | "created_time"
| "TIME" | "Time" | "time" => Ok(Self::CreatedTime),
"MODIFIED_TIME" | "ModifiedTime" | "modified_time"
| "DATE" | "Date" | "date" => Ok(Self::ModifiedTime),
"TITLE" | "Title" | "title" => Ok(Self::Title),
_ => Err(FoundationError::ParseError {
type_name: "FieldType".to_string(),
value: s.to_string(),
valid_values:
"CLICK_HERE, AUTHOR, LAST_SAVED_BY, CREATED_TIME, MODIFIED_TIME, TITLE"
.to_string(),
}),
}
}
}
impl TryFrom<u8> for FieldType {
type Error = FoundationError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::ClickHere),
1 => Ok(Self::Author),
2 => Ok(Self::LastSavedBy),
3 => Ok(Self::CreatedTime),
4 => Ok(Self::ModifiedTime),
5 => Ok(Self::Title),
_ => Err(FoundationError::ParseError {
type_name: "FieldType".to_string(),
value: value.to_string(),
valid_values: "0..5 (ClickHere..Title)".to_string(),
}),
}
}
}
impl schemars::JsonSchema for FieldType {
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("FieldType")
}
fn json_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
gen.subschema_for::<String>()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[non_exhaustive]
pub enum RefType {
#[default]
Bookmark,
Table,
Figure,
Equation,
Footnote,
Endnote,
Outline,
Unknown(u8),
}
impl fmt::Display for RefType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bookmark => f.write_str("TARGET_BOOKMARK"),
Self::Table => f.write_str("TARGET_TABLE"),
Self::Figure => f.write_str("TARGET_FIGURE"),
Self::Equation => f.write_str("TARGET_EQUATION"),
Self::Footnote => f.write_str("TARGET_FOOTNOTE"),
Self::Endnote => f.write_str("TARGET_ENDNOTE"),
Self::Outline => f.write_str("TARGET_OUTLINE"),
Self::Unknown(code) => write!(f, "TARGET_UNKNOWN({code})"),
}
}
}
impl std::str::FromStr for RefType {
type Err = FoundationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"TARGET_BOOKMARK" | "Bookmark" | "bookmark" => Ok(Self::Bookmark),
"TARGET_TABLE" | "Table" | "table" => Ok(Self::Table),
"TARGET_FIGURE" | "Figure" | "figure" => Ok(Self::Figure),
"TARGET_EQUATION" | "Equation" | "equation" => Ok(Self::Equation),
"TARGET_FOOTNOTE" | "Footnote" | "footnote" => Ok(Self::Footnote),
"TARGET_ENDNOTE" | "Endnote" | "endnote" => Ok(Self::Endnote),
"TARGET_OUTLINE" | "Outline" | "outline" => Ok(Self::Outline),
_ => Err(FoundationError::ParseError {
type_name: "RefType".to_string(),
value: s.to_string(),
valid_values: "TARGET_BOOKMARK, TARGET_TABLE, TARGET_FIGURE, TARGET_EQUATION, \
TARGET_FOOTNOTE, TARGET_ENDNOTE, TARGET_OUTLINE"
.to_string(),
}),
}
}
}
impl schemars::JsonSchema for RefType {
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("RefType")
}
fn json_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
gen.subschema_for::<String>()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[non_exhaustive]
pub enum RefContentType {
#[default]
Page,
Number,
Contents,
UpDownPos,
Unknown(u8),
}
impl fmt::Display for RefContentType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Page => f.write_str("OBJECT_TYPE_PAGE"),
Self::Number => f.write_str("OBJECT_TYPE_NUMBER"),
Self::Contents => f.write_str("OBJECT_TYPE_CONTENTS"),
Self::UpDownPos => f.write_str("OBJECT_TYPE_UPDOWNPOS"),
Self::Unknown(code) => write!(f, "OBJECT_TYPE_UNKNOWN({code})"),
}
}
}
impl std::str::FromStr for RefContentType {
type Err = FoundationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"OBJECT_TYPE_PAGE" | "Page" | "page" => Ok(Self::Page),
"OBJECT_TYPE_NUMBER" | "Number" | "number" => Ok(Self::Number),
"OBJECT_TYPE_CONTENTS" | "Contents" | "contents" => Ok(Self::Contents),
"OBJECT_TYPE_UPDOWNPOS" | "UpDownPos" | "updownpos" => Ok(Self::UpDownPos),
_ => Err(FoundationError::ParseError {
type_name: "RefContentType".to_string(),
value: s.to_string(),
valid_values: "OBJECT_TYPE_PAGE, OBJECT_TYPE_NUMBER, OBJECT_TYPE_CONTENTS, \
OBJECT_TYPE_UPDOWNPOS"
.to_string(),
}),
}
}
}
impl schemars::JsonSchema for RefContentType {
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("RefContentType")
}
fn json_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
gen.subschema_for::<String>()
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DropCapStyle {
#[default]
None = 0,
DoubleLine = 1,
TripleLine = 2,
Margin = 3,
}
impl fmt::Display for DropCapStyle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::None => f.write_str("None"),
Self::DoubleLine => f.write_str("DoubleLine"),
Self::TripleLine => f.write_str("TripleLine"),
Self::Margin => f.write_str("Margin"),
}
}
}
impl DropCapStyle {
pub fn from_hwpx_str(s: &str) -> Self {
match s {
"DoubleLine" => Self::DoubleLine,
"TripleLine" => Self::TripleLine,
"Margin" => Self::Margin,
_ => Self::None,
}
}
}
impl std::str::FromStr for DropCapStyle {
type Err = FoundationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"None" | "NONE" | "none" => Ok(Self::None),
"DoubleLine" | "DOUBLE_LINE" => Ok(Self::DoubleLine),
"TripleLine" | "TRIPLE_LINE" => Ok(Self::TripleLine),
"Margin" | "MARGIN" => Ok(Self::Margin),
_ => Err(FoundationError::ParseError {
type_name: "DropCapStyle".to_string(),
value: s.to_string(),
valid_values: "None, DoubleLine, TripleLine, Margin".to_string(),
}),
}
}
}
impl schemars::JsonSchema for DropCapStyle {
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("DropCapStyle")
}
fn json_schema(gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
gen.subschema_for::<String>()
}
}
impl serde::Serialize for DropCapStyle {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.to_string())
}
}
impl<'de> serde::Deserialize<'de> for DropCapStyle {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
}
}