drawbridge_client/
user.rs

1// SPDX-FileCopyrightText: 2022 Profian Inc. <opensource@profian.com>
2// SPDX-License-Identifier: Apache-2.0
3
4use super::{scope, Entity, Repository, Result, Scope};
5
6use std::ops::Deref;
7
8use drawbridge_type::{RepositoryName, UserName, UserRecord};
9
10use mime::APPLICATION_JSON;
11
12#[derive(Clone, Debug)]
13#[repr(transparent)]
14pub struct User<'a, S: Scope>(Entity<'a, S, scope::User>);
15
16impl<'a, S: Scope> Deref for User<'a, S> {
17    type Target = Entity<'a, S, scope::User>;
18
19    fn deref(&self) -> &Self::Target {
20        &self.0
21    }
22}
23
24impl<'a, S: Scope> User<'a, S> {
25    pub fn new(entity: Entity<'a, S, scope::Root>, name: &UserName) -> Self {
26        User(entity.child(&name.to_string()))
27    }
28
29    pub fn create(&self, conf: &UserRecord) -> Result<bool> {
30        self.0.create_json(&APPLICATION_JSON, conf)
31    }
32
33    pub fn get(&self) -> Result<UserRecord> {
34        // TODO: Use a reasonable byte limit
35        self.0.get_json(u64::MAX).map(|(_, v)| v)
36    }
37
38    pub fn repository(&self, name: &RepositoryName) -> Repository<'a, S> {
39        Repository::new(self.0.clone(), name)
40    }
41}