// Copyright 2024 RustFS Team
//
// 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.

use thiserror::Error;

use crate::data_usage_define::DataUsageCache;

/// Scanner-related errors
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ScannerError {
    /// Configuration error
    #[error("Configuration error: {0}")]
    Config(String),

    /// I/O error
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// Serialization error
    #[error("Serialization error: {0}")]
    Serialization(#[from] serde_json::Error),

    /// Generic error
    #[error("Scanner error: {0}")]
    Other(String),

    /// A remote namespace scanner request ID was already accepted.
    #[error("Remote namespace scanner request replay detected")]
    RemoteRequestReplay,

    /// The bounded remote namespace scanner replay cache cannot accept more IDs yet.
    #[error("Remote namespace scanner replay cache capacity exceeded")]
    RemoteReplayCapacity,

    /// A remote namespace scanner request is already active for the target disk.
    #[error("Remote namespace scanner disk is already active")]
    RemoteDiskBusy,

    /// Partial data usage cache produced before the scanner stopped.
    #[error("Scanner stopped with partial data usage cache")]
    PartialCache(Box<DataUsageCache>),

    /// Partial cache retained because the bucket or scan root disappeared.
    #[error("Scanner namespace path disappeared during the scan")]
    NamespaceNotFoundCache(Box<DataUsageCache>),
}
