snap_control/server/state/
dto.rs

1// Copyright 2025 Anapaya Systems
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//   http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! Data transfer objects (DTOs) for the SNAP control plane server state.
15
16use anyhow::Context;
17use serde::{Deserialize, Serialize};
18use utoipa::ToSchema;
19
20use crate::server::state::ControlPlaneIoConfig;
21
22/// The I/O configuration of a SNAP control plane.
23#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
24pub struct IoControlPlaneConfigDto {
25    /// The Control plane API address.
26    pub api_addr: Option<String>,
27}
28
29impl TryFrom<IoControlPlaneConfigDto> for ControlPlaneIoConfig {
30    type Error = anyhow::Error;
31
32    fn try_from(value: IoControlPlaneConfigDto) -> Result<Self, Self::Error> {
33        let api_addr = match value.api_addr {
34            Some(addr) => Some(addr.parse().context("invalid control plane API address")?),
35            None => None,
36        };
37
38        Ok(Self { api_addr })
39    }
40}
41
42impl From<&ControlPlaneIoConfig> for IoControlPlaneConfigDto {
43    fn from(value: &ControlPlaneIoConfig) -> Self {
44        IoControlPlaneConfigDto {
45            api_addr: value.api_addr.map(|addr| addr.to_string()),
46        }
47    }
48}