workspace.rs 2.4 KB

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