teo_runtime/response/body/
mod.rs1use std::path::PathBuf;
2use std::sync::Arc;
3
4use crate::value::Value;
5
6#[derive(Clone)]
7pub struct Body {
8 pub inner: Arc<BodyInner>
9}
10
11impl Body {
12
13 pub fn empty() -> Self {
14 Self {
15 inner: Arc::new(BodyInner::Empty)
16 }
17 }
18
19 pub fn string(content: String) -> Self {
20 Self {
21 inner: Arc::new(BodyInner::String(content))
22 }
23 }
24
25 pub fn file(content: PathBuf) -> Self {
26 Self {
27 inner: Arc::new(BodyInner::File(content))
28 }
29 }
30
31 pub fn teon(content: Value) -> Self {
32 Self {
33 inner: Arc::new(BodyInner::Teon(content))
34 }
35 }
36
37 pub fn is_empty(&self) -> bool {
38 match self.inner.as_ref() {
39 BodyInner::Empty => true,
40 _ => false,
41 }
42 }
43
44 pub fn is_file(&self) -> bool {
45 match self.inner.as_ref() {
46 BodyInner::File(_) => true,
47 _ => false,
48 }
49 }
50
51 pub fn as_file(&self) -> Option<&PathBuf> {
52 match self.inner.as_ref() {
53 BodyInner::File(v) => Some(v),
54 _ => None,
55 }
56 }
57
58 pub fn is_text(&self) -> bool {
59 match self.inner.as_ref() {
60 BodyInner::String(_) => true,
61 _ => false,
62 }
63 }
64
65 pub fn as_text(&self) -> Option<&String> {
66 match self.inner.as_ref() {
67 BodyInner::String(v) => Some(v),
68 _ => None,
69 }
70 }
71
72 pub fn is_teon(&self) -> bool {
73 match self.inner.as_ref() {
74 BodyInner::Teon(_) => true,
75 _ => false,
76 }
77 }
78
79 pub fn as_teon(&self) -> Option<&Value> {
80 match self.inner.as_ref() {
81 BodyInner::Teon(v) => Some(v),
82 _ => None,
83 }
84 }
85}
86
87pub enum BodyInner {
88 Empty,
89 String(String),
90 File(PathBuf),
91 Teon(Value),
92}