Skip to main content

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    #[schema(nullable = false)]
27    #[serde(skip_serializing_if = "Option::is_none", default)]
28    pub api_addr: Option<String>,
29}
30
31impl TryFrom<IoControlPlaneConfigDto> for ControlPlaneIoConfig {
32    type Error = anyhow::Error;
33
34    fn try_from(value: IoControlPlaneConfigDto) -> Result<Self, Self::Error> {
35        let api_addr = match value.api_addr {
36            Some(addr) => Some(addr.parse().context("invalid control plane API address")?),
37            None => None,
38        };
39
40        Ok(Self { api_addr })
41    }
42}
43
44impl From<&ControlPlaneIoConfig> for IoControlPlaneConfigDto {
45    fn from(value: &ControlPlaneIoConfig) -> Self {
46        IoControlPlaneConfigDto {
47            api_addr: value.api_addr.map(|addr| addr.to_string()),
48        }
49    }
50}