workspace.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. use crate::{
  2. db::Update,
  3. request::{RequestPathUpdate, WorkspaceRequest},
  4. };
  5. use serde::{Deserialize, Serialize};
  6. use sqlx::prelude::Type;
  7. #[derive(Debug, Serialize)]
  8. pub struct Workspace {
  9. pub id: i64,
  10. pub name: String,
  11. }
  12. #[derive(Debug, Serialize)]
  13. #[serde(tag = "type", content = "data")]
  14. pub enum WorkspaceEntry {
  15. Collection(WorkspaceEntryBase),
  16. Request(WorkspaceRequest),
  17. }
  18. impl WorkspaceEntry {
  19. pub fn new_req(req: WorkspaceRequest) -> Self {
  20. Self::Request(req)
  21. }
  22. pub fn new_col(col: WorkspaceEntryBase) -> Self {
  23. Self::Collection(col)
  24. }
  25. }
  26. /// Database model representation of either a collection or a request.
  27. #[derive(Debug, Serialize)]
  28. pub struct WorkspaceEntryBase {
  29. /// Entry ID.
  30. pub id: i64,
  31. /// Containing workspace ID.
  32. pub workspace_id: i64,
  33. /// If present, holds the parent collection ID of the entry.
  34. pub parent_id: Option<i64>,
  35. /// User friendly display name for the entry.
  36. pub name: String,
  37. /// Whether this type is a collection or a request.
  38. pub r#type: WorkspaceEntryType,
  39. /// If present, holds the [Authentication][crate::auth::Authentication] ID of the entry.
  40. pub auth: Option<i64>,
  41. /// If `true`, the entry will inherit the auth scheme of its parent entry.
  42. pub auth_inherit: bool,
  43. }
  44. #[derive(Debug, Serialize)]
  45. pub struct WorkspaceEnvironment {
  46. pub id: i64,
  47. pub name: String,
  48. pub workspace_id: i64,
  49. pub variables: Vec<WorkspaceEnvVariable>,
  50. }
  51. #[derive(Debug, Serialize)]
  52. pub struct WorkspaceEnvVariable {
  53. pub id: i64,
  54. pub env_id: i64,
  55. pub workspace_id: i64,
  56. pub name: String,
  57. pub value: String,
  58. pub secret: bool,
  59. }
  60. #[derive(Debug, Deserialize)]
  61. pub enum WorkspaceEntryCreate {
  62. Collection {
  63. name: String,
  64. workspace_id: i64,
  65. parent_id: Option<i64>,
  66. },
  67. Request {
  68. name: String,
  69. workspace_id: i64,
  70. parent_id: Option<i64>,
  71. method: String,
  72. url: String,
  73. },
  74. }
  75. #[derive(Debug, Deserialize)]
  76. pub enum WorkspaceEntryUpdate {
  77. Collection(WorkspaceEntryUpdateBase),
  78. Request {
  79. base: WorkspaceEntryUpdateBase,
  80. method: Option<String>,
  81. url: Option<String>,
  82. path_params: Option<Vec<RequestPathUpdate>>,
  83. },
  84. }
  85. #[derive(Debug, Deserialize, Default)]
  86. pub struct WorkspaceEntryUpdateBase {
  87. pub name: Option<String>,
  88. pub parent_id: Option<Update<i64>>,
  89. }
  90. #[derive(Debug, Clone, Copy, Type, Serialize)]
  91. #[sqlx(type_name = "INTEGER")]
  92. pub enum WorkspaceEntryType {
  93. Request,
  94. Collection,
  95. }
  96. impl From<i64> for WorkspaceEntryType {
  97. fn from(value: i64) -> Self {
  98. match value {
  99. 0 => Self::Request,
  100. 1 => Self::Collection,
  101. _ => panic!("unrecognized entry type: {value}"),
  102. }
  103. }
  104. }