1use serde::{Deserialize, Serialize};
6use std::str::FromStr;
7
8use crate::error::{Result, SedimentError};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
12#[serde(rename_all = "lowercase")]
13pub enum ContentType {
14 #[default]
16 Text,
17 Markdown,
19 Json,
21 Yaml,
23 Code,
25}
26
27impl ContentType {
28 pub fn as_str(&self) -> &'static str {
29 match self {
30 ContentType::Text => "text",
31 ContentType::Markdown => "markdown",
32 ContentType::Json => "json",
33 ContentType::Yaml => "yaml",
34 ContentType::Code => "code",
35 }
36 }
37}
38
39impl FromStr for ContentType {
40 type Err = SedimentError;
41
42 fn from_str(s: &str) -> Result<Self> {
43 match s.to_lowercase().as_str() {
44 "text" => Ok(ContentType::Text),
45 "markdown" | "md" => Ok(ContentType::Markdown),
46 "json" => Ok(ContentType::Json),
47 "yaml" | "yml" => Ok(ContentType::Yaml),
48 "code" => Ok(ContentType::Code),
49 _ => Err(SedimentError::InvalidContentType(s.to_string())),
50 }
51 }
52}
53
54impl std::fmt::Display for ContentType {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 write!(f, "{}", self.as_str())
57 }
58}