pr_01m47d15m3e54sn21z27rpy5n9/crates/contracts/src/lib.rs

68 lines1,995 bytesCodeBlame
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
7pub mod events;
8pub mod identity;
9mod ids;
10mod names;
11mod outcome;
12pub mod repos;
13pub mod time;
14pub mod work;
15
16pub use ids::new_id;
17pub use names::{is_valid_namespace, is_valid_repo_name};
18pub use outcome::{Failure, FailureCode, Outcome};
19
20use serde::{Deserialize, Serialize};
21
22/// What a member may do in a workspace.
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
24#[serde(rename_all = "lowercase")]
25pub enum Role {
26 /// Everything a member can, plus managing members.
27 Owner,
28 /// Create repositories, push, manage issues and merge pull requests.
29 Member,
30}
31
32/// One workspace a user belongs to.
33#[derive(Clone, Debug, Serialize, Deserialize)]
34pub struct Membership {
35 /// The workspace's name in URLs: `g1t.sh/<slug>`.
36 pub slug: String,
37 pub role: Role,
38}
39
40#[derive(Clone, Debug, Default, Serialize, Deserialize)]
41pub struct User {
42 pub id: String,
43 pub username: String,
44 /// Whether the account's email address has been confirmed. Unverified
45 /// accounts can sign in but cannot create or change anything.
46 #[serde(default)]
47 pub verified: bool,
48 /// The workspaces this user belongs to. Filled in when a user is
49 /// resolved from credentials, so any service can authorize from it.
50 #[serde(default)]
51 pub workspaces: Vec<Membership>,
52}
53
54impl User {
55 pub fn role_in(&self, slug: &str) -> Option<Role> {
56 self.workspaces
57 .iter()
58 .find(|membership| membership.slug == slug)
59 .map(|membership| membership.role)
60 }
61
62 pub fn is_member(&self, slug: &str) -> bool {
63 self.role_in(slug).is_some()
64 }
65}
66
67/// Who is asking. Every read and write in every service takes one.
68pub type Viewer = Option<User>;