scion_sdk_address_manager/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 IP networks.
15
16use std::str::FromStr;
17
18use ipnet::IpNet;
19use serde::{Deserialize, Serialize};
20use utoipa::ToSchema;
21
22/// IP network.
23// XXX(ake): We should figure out a place to collect foreign dto types
24
25#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
26pub struct IpNetDto(String);
27
28impl From<IpNet> for IpNetDto {
29 fn from(ipnet: IpNet) -> Self {
30 Self(ipnet.to_string())
31 }
32}
33
34impl TryFrom<IpNetDto> for IpNet {
35 type Error = anyhow::Error;
36
37 fn try_from(value: IpNetDto) -> Result<Self, Self::Error> {
38 IpNet::from_str(&value.0).map_err(|_| anyhow::anyhow!("Invalid IPNet format"))
39 }
40}