surrealdb_core/api/
method.rs1use std::fmt::{self, Display};
2
3use revision::revisioned;
4use serde::{Deserialize, Serialize};
5
6use crate::{
7 err::Error,
8 sql::{statements::info::InfoStructure, Value},
9};
10
11#[revisioned(revision = 1)]
12#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14#[non_exhaustive]
15pub enum Method {
16 Delete,
17 Get,
18 Patch,
19 Post,
20 Put,
21 Trace,
22}
23
24impl TryFrom<&Value> for Method {
25 type Error = Error;
26 fn try_from(value: &Value) -> Result<Self, Self::Error> {
27 match value {
28 Value::Strand(s) => match s.to_ascii_lowercase().as_str() {
29 "delete" => Ok(Self::Delete),
30 "get" => Ok(Self::Get),
31 "patch" => Ok(Self::Patch),
32 "post" => Ok(Self::Post),
33 "put" => Ok(Self::Put),
34 "trace" => Ok(Self::Trace),
35 _ => Err(Error::Thrown("method does not match".into())),
36 },
37 _ => Err(Error::Thrown("method does not match".into())),
38 }
39 }
40}
41
42impl Display for Method {
43 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44 match self {
45 Self::Delete => write!(f, "delete"),
46 Self::Get => write!(f, "get"),
47 Self::Patch => write!(f, "patch"),
48 Self::Post => write!(f, "post"),
49 Self::Put => write!(f, "put"),
50 Self::Trace => write!(f, "trace"),
51 }
52 }
53}
54
55impl InfoStructure for Method {
56 fn structure(self) -> Value {
57 Value::from(self.to_string())
58 }
59}