custom_arrays/model/endpoint_type/
mod.rs

1use crate::model::protocol_type::{PgProtocolType, ProtocolType};
2use diesel::deserialize::{FromSql, FromSqlRow};
3use diesel::expression::AsExpression;
4use diesel::pg::{Pg, PgValue};
5use diesel::serialize::{Output, ToSql};
6use diesel::sql_types::{Integer, Record, Text};
7use diesel::{deserialize, serialize};
8
9#[derive(Debug, Clone, FromSqlRow, AsExpression, PartialEq, Eq)]
10#[diesel(sql_type=crate::schema::smdb::sql_types::ServiceEndpoint)]
11pub struct Endpoint {
12    pub name: String,
13    pub version: i32,
14    pub base_uri: String,
15    pub port: i32,
16    pub protocol: ProtocolType,
17}
18
19impl Endpoint {
20    pub fn new(
21        name: String,
22        version: i32,
23        base_uri: String,
24        port: i32,
25        protocol: ProtocolType,
26    ) -> Self {
27        Self {
28            name,
29            version,
30            base_uri,
31            port,
32            protocol,
33        }
34    }
35}
36
37impl ToSql<crate::schema::smdb::sql_types::ServiceEndpoint, Pg> for Endpoint {
38    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
39        serialize::WriteTuple::<(Text, Integer, Text, Integer, PgProtocolType)>::write_tuple(
40            &(
41                &self.name,
42                &self.version,
43                &self.base_uri,
44                &self.port,
45                &self.protocol,
46            ),
47            &mut out.reborrow(),
48        )
49    }
50}
51
52impl FromSql<crate::schema::smdb::sql_types::ServiceEndpoint, Pg> for Endpoint {
53    fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
54        let (name, version, base_uri, port, protocol) =
55            FromSql::<Record<(Text, Integer, Text, Integer, PgProtocolType)>, Pg>::from_sql(bytes)?;
56
57        Ok(Endpoint {
58            name,
59            version,
60            base_uri,
61            port,
62            protocol,
63        })
64    }
65}