surrealdb_core/api/
method.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::fmt::{self, Display};

use revision::revisioned;
use serde::{Deserialize, Serialize};

use crate::{
	err::Error,
	sql::{statements::info::InfoStructure, Value},
};

#[revisioned(revision = 1)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub enum Method {
	Delete,
	Get,
	Patch,
	Post,
	Put,
	Trace,
}

impl TryFrom<&Value> for Method {
	type Error = Error;
	fn try_from(value: &Value) -> Result<Self, Self::Error> {
		match value {
			Value::Strand(s) => match s.to_ascii_lowercase().as_str() {
				"delete" => Ok(Self::Delete),
				"get" => Ok(Self::Get),
				"patch" => Ok(Self::Patch),
				"post" => Ok(Self::Post),
				"put" => Ok(Self::Put),
				"trace" => Ok(Self::Trace),
				_ => Err(Error::Thrown("method does not match".into())),
			},
			_ => Err(Error::Thrown("method does not match".into())),
		}
	}
}

impl Display for Method {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::Delete => write!(f, "DELETE"),
			Self::Get => write!(f, "GET"),
			Self::Patch => write!(f, "PATCH"),
			Self::Post => write!(f, "POST"),
			Self::Put => write!(f, "PUT"),
			Self::Trace => write!(f, "TRACE"),
		}
	}
}

impl InfoStructure for Method {
	fn structure(self) -> Value {
		Value::from(self.to_string())
	}
}