astarte-interfaces 1.0.0

Interfaces used by Astarte to define how data is exchange with a Device.
Documentation
// This file is part of Astarte.
//
// Copyright 2023 - 2025 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

//! Errors generated by the interface module.

use crate::{
    interface::{datastream::object::ObjectError, name::InterfaceNameError, version::VersionError},
    mapping::MappingError,
    schema::{Aggregation, InterfaceType, SchemaError},
};

use super::{interface::validation::VersionChangeError, mapping::endpoint::EndpointError};

/// Error for parsing and validating an interface.
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// Couldn't parse the interface JSON.
    #[error("cannot parse interface JSON")]
    Parse(#[from] serde_json::Error),
    /// Invalid [`InterfaceJson`](crate::schema::InterfaceJson)
    #[error("invalid interface JSON schema")]
    Schema(#[from] SchemaError),
    /// Invalid [`InterfaceVersion`](crate::interface::version::InterfaceVersion)
    #[error("invalid interface version")]
    Version(#[from] VersionError),
    /// Couldn't parse interface for an invalid mapping
    #[error("couldn't parse interface for invalid mapping")]
    Mapping(#[from] MappingError),
    /// Couldn't parse object datastream interface
    #[error("couldn't parse object datastream interface")]
    Object(#[from] ObjectError),
    /// Couldn't parse interface name.
    #[error("couldn't parse object datastream interface")]
    Name(#[from] InterfaceNameError),
    /// The database retention policy is set to `use_ttl` but the TTL was not specified.
    #[error("Database retention policy set to `use_ttl` but the TTL was not specified")]
    MissingTtl,
    /// Error while parsing the endpoint.
    #[error("invalid endpoint")]
    InvalidEndpoint(#[from] EndpointError),
    /// The interfaced endpoints must all be unique.
    #[error("duplicate endpoint mapping '{endpoint}' and '{duplicate}'")]
    DuplicateMapping {
        /// The existing endpoint.
        endpoint: String,
        /// The duplicated endpoint.
        duplicate: String,
    },
    /// The name of the interface was changed.
    #[error("this version has a different name {name} than the previous version {prev_name}")]
    NameMismatch {
        /// The new version name.
        name: String,
        /// The previous version name.
        prev_name: String,
    },
    /// Invalid version change.
    #[error("incompatible interface version")]
    VersionChange(#[from] VersionChangeError),
    /// Invalid interface type
    #[error("couldn't convert interface, expected {exp_type} {exp_aggregation} but got {got_type} {got_aggregation}")]
    InterfaceConversion {
        /// The interface type we expected.
        exp_type: InterfaceType,
        /// The aggregation we expected.
        exp_aggregation: Aggregation,
        /// The actual interface type.
        got_type: InterfaceType,
        /// The actual aggregation.
        got_aggregation: Aggregation,
    },
    /// Unsupported interface type.
    ///
    // Yeh we wished.
    #[error("property object are supported")]
    PropertyObject,
}