g1t/crates/contracts/src/lib.rs
| 1 | //! Types and service interfaces shared by every g1t service. |
| 2 | //! |
| 3 | //! Each service has a module here holding the data it exchanges and the |
| 4 | //! arguments of each of its methods. Services and their callers depend on |
| 5 | //! this crate, never on each other's code. |
| 6 | |
| 7 | pub mod billing; |
| 8 | pub mod events; |
| 9 | pub mod identity; |
| 10 | pub mod integrations; |
| 11 | mod ids; |
| 12 | mod names; |
| 13 | mod outcome; |
| 14 | pub mod repos; |
| 15 | pub mod time; |
| 16 | pub mod work; |
| 17 | |
| 18 | pub use ids::new_id; |
| 19 | pub use names::{is_valid_namespace, is_valid_repo_name}; |
| 20 | pub use outcome::{Failure, FailureCode, Outcome}; |
| 21 | |
| 22 | use serde::{Deserialize, Serialize}; |
| 23 | |
| 24 | /// What a member may do in a workspace. |
| 25 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 26 | #[serde(rename_all = "lowercase")] |
| 27 | pub enum Role { |
| 28 | /// Everything a member can, plus managing members. |
| 29 | Owner, |
| 30 | /// Create repositories, push, manage issues and merge pull requests. |
| 31 | Member, |
| 32 | } |
| 33 | |
| 34 | /// One workspace a user belongs to. |
| 35 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 36 | pub struct Membership { |
| 37 | /// The workspace's name in URLs: `g1t.sh/<slug>`. |
| 38 | pub slug: String, |
| 39 | pub role: Role, |
| 40 | } |
| 41 | |
| 42 | /// What a set of credentials resolved to. |
| 43 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 44 | #[serde(rename_all = "lowercase")] |
| 45 | pub enum PrincipalKind { |
| 46 | /// A person's account. |
| 47 | #[default] |
| 48 | User, |
| 49 | /// A workspace, acting through one of its own access tokens. Its `id` |
| 50 | /// is the workspace's, its `username` the workspace's slug, and it is a |
| 51 | /// member of that workspace and no other. |
| 52 | Workspace, |
| 53 | /// A g1t agent at work in a sandbox, acting through a token that lives |
| 54 | /// as long as its run and can do only what that token's scope lists, in |
| 55 | /// one repository. Its `username` is `g1t-agent`. |
| 56 | Agent, |
| 57 | } |
| 58 | |
| 59 | #[derive(Clone, Debug, Default, Serialize, Deserialize)] |
| 60 | pub struct User { |
| 61 | pub id: String, |
| 62 | pub username: String, |
| 63 | #[serde(default)] |
| 64 | pub kind: PrincipalKind, |
| 65 | /// Whether the account's email address has been confirmed. Unverified |
| 66 | /// accounts can sign in but cannot create or change anything. |
| 67 | #[serde(default)] |
| 68 | pub verified: bool, |
| 69 | /// The workspaces this user belongs to. Filled in when a user is |
| 70 | /// resolved from credentials, so any service can authorize from it. |
| 71 | #[serde(default)] |
| 72 | pub workspaces: Vec<Membership>, |
| 73 | } |
| 74 | |
| 75 | impl User { |
| 76 | pub fn role_in(&self, slug: &str) -> Option<Role> { |
| 77 | self.workspaces |
| 78 | .iter() |
| 79 | .find(|membership| membership.slug == slug) |
| 80 | .map(|membership| membership.role) |
| 81 | } |
| 82 | |
| 83 | pub fn is_member(&self, slug: &str) -> bool { |
| 84 | self.role_in(slug).is_some() |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Who is asking. Every read and write in every service takes one. |
| 89 | pub type Viewer = Option<User>; |