1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This file is part of Astarte.
//
// Copyright 2023 SECO Mind Srl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Typed reference to an interface.

use std::{borrow::Borrow, ops::Deref};

use crate::Interface;

use super::{mapping::path::MappingPath, DatastreamObject, Mapping};

/// Struct to hold a reference to an [`Interface`], which is a property.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PropertyRef<'a>(pub(crate) &'a Interface);

impl<'a> Borrow<Interface> for PropertyRef<'a> {
    fn borrow(&self) -> &Interface {
        self.0
    }
}

impl<'a> Deref for PropertyRef<'a> {
    type Target = Interface;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

impl<'a> AsRef<Interface> for PropertyRef<'a> {
    fn as_ref(&self) -> &Interface {
        self.0
    }
}

/// Reference to an [`Interface`] and a [`DatastreamObject`] that is guaranty to belong to the interface.
#[derive(Debug, Clone, Copy)]
pub(crate) struct ObjectRef<'a> {
    pub(crate) interface: &'a Interface,
    pub(crate) object: &'a DatastreamObject,
}

impl<'a> ObjectRef<'a> {
    /// Create a new reference only if the interface is an object.
    pub(crate) fn new(interface: &'a Interface) -> Option<Self> {
        match &interface.inner {
            crate::interface::InterfaceType::DatastreamIndividual(_)
            | crate::interface::InterfaceType::Properties(_) => None,
            crate::interface::InterfaceType::DatastreamObject(object) => {
                Some(Self { interface, object })
            }
        }
    }
}

impl<'a> Deref for ObjectRef<'a> {
    type Target = DatastreamObject;

    fn deref(&self) -> &Self::Target {
        self.object
    }
}

/// Reference to an [`Interface`] and a [`Mapping`] that is guaranty to belong to the interface.
#[derive(Debug, Clone, Copy)]
pub(crate) struct MappingRef<'a, I: 'a> {
    path: &'a MappingPath<'a>,
    mapping: Mapping<&'a str>,
    interface: I,
}

impl<'a> MappingRef<'a, &'a Interface> {
    pub(crate) fn new(interface: &'a Interface, path: &'a MappingPath) -> Option<Self> {
        interface.mapping(path).map(|mapping| Self {
            interface,
            path,
            mapping,
        })
    }

    pub(crate) fn as_prop(&self) -> Option<MappingRef<'a, PropertyRef<'a>>> {
        self.interface.as_prop_ref().map(|interface| MappingRef {
            interface,
            path: self.path,
            mapping: self.mapping,
        })
    }
}

impl<'a> MappingRef<'a, PropertyRef<'a>> {
    pub(crate) fn with_prop(interface: PropertyRef<'a>, path: &'a MappingPath) -> Option<Self> {
        let mapping = interface.0.mapping(path)?;

        Some(Self {
            interface,
            path,
            mapping,
        })
    }
}

impl<'a, I> MappingRef<'a, I> {
    #[inline]
    pub(crate) fn mapping(&self) -> &Mapping<&str> {
        &self.mapping
    }

    #[inline]
    pub(crate) fn interface(&self) -> &I {
        &self.interface
    }

    #[inline]
    pub(crate) fn path(&self) -> &MappingPath {
        self.path
    }
}

impl<'a, I: 'a> Deref for MappingRef<'a, I> {
    type Target = Mapping<&'a str>;

    fn deref(&self) -> &Self::Target {
        &self.mapping
    }
}