Skip to main content

couchbase_core/
on_behalf_of.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use crate::{error, httpx};
20
21#[derive(Clone, PartialEq, Eq, Debug)]
22#[non_exhaustive]
23pub struct OnBehalfOfInfo {
24    pub(crate) username: String,
25    pub(crate) password_or_domain: Option<OboPasswordOrDomain>,
26}
27
28impl OnBehalfOfInfo {
29    pub fn new(username: impl Into<String>) -> Self {
30        Self {
31            username: username.into(),
32            password_or_domain: None,
33        }
34    }
35
36    pub fn password_or_domain(mut self, password_or_domain: OboPasswordOrDomain) -> Self {
37        self.password_or_domain = Some(password_or_domain);
38        self
39    }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
43pub enum OboPasswordOrDomain {
44    Password(String),
45    Domain(String),
46}
47
48impl TryFrom<OnBehalfOfInfo> for httpx::request::OnBehalfOfInfo {
49    type Error = error::Error;
50
51    fn try_from(info: OnBehalfOfInfo) -> Result<Self, Self::Error> {
52        let password_or_domain = info.password_or_domain.ok_or_else(|| {
53            error::Error::new_message_error("OnBehalfOfInfo must have a password or domain set")
54        })?;
55
56        Ok(httpx::request::OnBehalfOfInfo {
57            username: info.username,
58            password_or_domain: password_or_domain.into(),
59        })
60    }
61}
62
63impl From<OboPasswordOrDomain> for httpx::request::OboPasswordOrDomain {
64    fn from(info: OboPasswordOrDomain) -> Self {
65        match info {
66            OboPasswordOrDomain::Password(password) => {
67                httpx::request::OboPasswordOrDomain::Password(password)
68            }
69            OboPasswordOrDomain::Domain(domain) => {
70                httpx::request::OboPasswordOrDomain::Domain(domain)
71            }
72        }
73    }
74}