coman/models/
collection.rs1use 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
24impl Collection {
25 pub fn get_request(&self, name: &str) -> Option<Request> {
27 if let Some(ref requests) = self.requests {
28 requests.iter().find(|r| r.name == name).cloned()
29 } else {
30 None
31 }
32 }
33}
34
35#[derive(Debug, Serialize, Deserialize, Clone)]
36pub struct Request {
37 pub name: String,
38 pub endpoint: String,
39 pub method: Method,
40 pub headers: Vec<(String, String)>,
41 pub body: Option<String>,
42}
43
44impl fmt::Display for Method {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 write!(f, "{:?}", self)
47 }
48}
49
50impl TryFrom<String> for Method {
51 type Error = String;
52
53 fn try_from(value: String) -> Result<Self, Self::Error> {
54 value
55 .parse()
56 .map_err(|_| format!("Invalid method: {}", value))
57 }
58}
59
60impl FromStr for Method {
61 type Err = String;
62
63 fn from_str(s: &str) -> Result<Self, Self::Err> {
64 match s.to_uppercase().as_str() {
65 "GET" => Ok(Method::Get),
66 "POST" => Ok(Method::Post),
67 "PUT" => Ok(Method::Put),
68 "DELETE" => Ok(Method::Delete),
69 "PATCH" => Ok(Method::Patch),
70 _ => Err(format!("Invalid method: {}", s)),
71 }
72 }
73}