coman/models/
collection.rs

1use core::fmt;
2use std::str::FromStr;
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Copy)]
7#[serde(try_from = "String")]
8pub enum Method {
9    Get,
10    Post,
11    Put,
12    Delete,
13    Patch,
14}
15
16#[derive(Debug, Serialize, Deserialize, Clone)]
17pub struct Collection {
18    pub name: String,
19    pub url: String,
20    pub headers: Vec<(String, String)>,
21    pub requests: Option<Vec<Request>>,
22}
23
24#[derive(Debug, Serialize, Deserialize, Clone)]
25pub struct Request {
26    pub name: String,
27    pub endpoint: String,
28    pub method: Method,
29    pub headers: Vec<(String, String)>,
30    pub body: Option<String>,
31}
32
33impl fmt::Display for Method {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "{:?}", self)
36    }
37}
38
39impl TryFrom<String> for Method {
40    type Error = String;
41
42    fn try_from(value: String) -> Result<Self, Self::Error> {
43        value
44            .parse()
45            .map_err(|_| format!("Invalid method: {}", value))
46    }
47}
48
49impl FromStr for Method {
50    type Err = String;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        match s.to_uppercase().as_str() {
54            "GET" => Ok(Method::Get),
55            "POST" => Ok(Method::Post),
56            "PUT" => Ok(Method::Put),
57            "DELETE" => Ok(Method::Delete),
58            "PATCH" => Ok(Method::Patch),
59            _ => Err(format!("Invalid method: {}", s)),
60        }
61    }
62}