laraindexnow 0.1.0

IndexNow for Laravel Rust: tell Bing, Yandex and other search engines a URL changed, the moment it changes.
Documentation
use serde::Serialize;

/// What happened to one request to the endpoint.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SubmissionResult {
    /// The host the URLs belong to.
    pub host: String,
    /// The URLs that were sent.
    pub urls: Vec<String>,
    /// Whether the endpoint accepted them.
    pub successful: bool,
    /// The HTTP status, when the endpoint answered.
    pub status: Option<u16>,
    /// A message written to be read by a human.
    pub message: String,
    /// Whether nothing was sent, for instance because the host has no key.
    pub skipped: bool,
}

impl SubmissionResult {
    /// The endpoint accepted the URLs.
    pub fn success(host: &str, urls: &[String], status: u16, message: impl Into<String>) -> Self {
        Self {
            host: host.to_owned(),
            urls: urls.to_vec(),
            successful: true,
            status: Some(status),
            message: message.into(),
            skipped: false,
        }
    }

    /// The endpoint, or the pipeline, rejected the URLs for good.
    pub fn failure(host: &str, urls: &[String], status: Option<u16>, message: impl Into<String>) -> Self {
        Self {
            host: host.to_owned(),
            urls: urls.to_vec(),
            successful: false,
            status,
            message: message.into(),
            skipped: false,
        }
    }

    /// Nothing was sent.
    pub fn skipped(host: &str, urls: &[String], message: impl Into<String>) -> Self {
        Self {
            host: host.to_owned(),
            urls: urls.to_vec(),
            successful: false,
            status: None,
            message: message.into(),
            skipped: true,
        }
    }

    /// Whether the request failed, as opposed to succeeding or being skipped.
    pub fn failed(&self) -> bool {
        !self.successful && !self.skipped
    }

    /// How many URLs the request carried.
    pub fn count(&self) -> usize {
        self.urls.len()
    }
}

/// A failure that retrying may fix: a rate limit, a server error or a connection that did not
/// complete. A rejected key or a malformed URL is never one of these.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RetryableSubmission {
    /// What went wrong.
    pub message: String,
    /// The host the URLs belong to.
    pub host: String,
    /// The URLs that were being sent.
    pub urls: Vec<String>,
    /// The seconds the endpoint asked us to wait, from its `Retry-After` header.
    pub retry_after: Option<u64>,
}

impl RetryableSubmission {
    /// Describe a retryable failure.
    pub fn new(message: impl Into<String>, host: &str, urls: &[String], retry_after: Option<u64>) -> Self {
        Self {
            message: message.into(),
            host: host.to_owned(),
            urls: urls.to_vec(),
            retry_after,
        }
    }
}

impl std::fmt::Display for RetryableSubmission {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl std::error::Error for RetryableSubmission {}