Skip to main content

sediment/
document.rs

1//! Content type for chunking
2//!
3//! This module is kept for the ContentType enum used by the chunker.
4
5use serde::{Deserialize, Serialize};
6use std::str::FromStr;
7
8use crate::error::{Result, SedimentError};
9
10/// Content type of content (used for smart chunking)
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
12#[serde(rename_all = "lowercase")]
13pub enum ContentType {
14    /// Plain text content
15    #[default]
16    Text,
17    /// Markdown formatted content
18    Markdown,
19    /// JSON data
20    Json,
21    /// YAML data
22    Yaml,
23    /// Source code
24    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}