laraindexnow 0.1.2

IndexNow for Laravel Rust: tell Bing, Yandex and other search engines a URL changed, the moment it changes.
Documentation
//! IndexNow for Laravel Rust: tell search engines a URL changed, the moment it changes.
//!
//! IndexNow is a small protocol: you publish a key file on your domain, then POST the URLs that
//! changed. Bing, Yandex, Seznam, Naver and Yep consume it, and one submission to the shared
//! endpoint reaches all of them. **Google does not participate**, so this complements your
//! sitemap, it does not replace it.
//!
//! This crate wires the protocol into your models, so publishing a post submits its URL and
//! unpublishing it submits the URL again, which is the part most implementations miss.
//!
//! ```ignore
//! IndexNowServiceProvider::new().track(
//!     indexnow::track::<Post>()
//!         .url_template("https://example.com/blog/{slug}")
//!         .when_holds("status", "published"),
//! )
//! ```
//!
//! It is the port of the PHP package [`edulazaro/laraindexnow`](https://github.com/edulazaro/laraindexnow)
//! to Laravel Rust 0.1 (beta), and its API follows the framework's changes until 1.0.

pub mod config;
pub mod console;
pub mod events;
pub mod jobs;
pub mod keys;
pub mod manager;
pub mod observer;
pub mod policy;
pub mod provider;
pub mod record;
pub mod registration;
pub mod result;
pub mod source;
pub mod sources;
pub mod submitter;
pub mod support;
pub mod tracked;
pub mod value;

use std::sync::Arc;

use laravel::container::Inject;
use laravel::model::Model;

pub use config::{Driver, IndexNowConfig, LogMode};
pub use events::{SubmissionFailed, UrlsSubmitted};
pub use keys::KeyRepository;
pub use manager::{IndexNow, IntoUrls, without_submissions};
pub use policy::Policy;
pub use provider::IndexNowServiceProvider;
pub use record::{Change, ChangeKind, Record};
pub use registration::{Op, Registration};
pub use result::{RetryableSubmission, SubmissionResult};
pub use source::{Page, Source};
pub use sources::{ModelSource, SitemapSource};
pub use submitter::{Fake, Submitter};
pub use value::Attr;

/// Start tracking a model. Chain the rules on the returned [`Registration`] and hand it to
/// [`IndexNowServiceProvider::track`].
pub fn track<M: Model>() -> Registration<M> {
    Registration::new()
}

/// The IndexNow service of the running application.
///
/// # Errors
///
/// Fails when the [`IndexNowServiceProvider`] is not registered.
pub async fn manager() -> laravel::Result<Inject<IndexNow>> {
    laravel::resolve::<IndexNow>().await
}

/// Buffer URLs and let the queue send them. Sent inline when the queue is disabled.
///
/// # Errors
///
/// Fails when the cache or the queue does.
pub async fn submit(urls: impl IntoUrls) -> laravel::Result<()> {
    manager().await?.submit(urls).await
}

/// Send URLs inline and return what the endpoint said.
///
/// # Errors
///
/// Fails with a [`RetryableSubmission`] when the endpoint rate limits or errors.
pub async fn submit_now(urls: impl IntoUrls) -> laravel::Result<Vec<SubmissionResult>> {
    manager().await?.submit_now(urls, false).await
}

/// Submit the URL of a tracked record, resolved from its registrations.
///
/// # Errors
///
/// Fails when a registration has no URL resolver, or when [`submit`] does.
pub async fn submit_model<M: Model>(model: &M) -> laravel::Result<()> {
    manager().await?.submit_model(model).await
}

/// Register a source for `indexnow:due` while the application runs.
///
/// # Errors
///
/// Fails when the [`IndexNowServiceProvider`] is not registered.
pub async fn source(source: impl Source + 'static) -> laravel::Result<()> {
    manager().await?.add_source(source);

    Ok(())
}

/// Register a sitemap, or a sitemap index, as a source while the application runs.
///
/// # Errors
///
/// Fails when the [`IndexNowServiceProvider`] is not registered.
pub async fn sitemap(url: impl Into<String>) -> laravel::Result<()> {
    source(SitemapSource::new(url)).await
}

/// The sources `indexnow:due` reads.
///
/// # Errors
///
/// Fails when the [`IndexNowServiceProvider`] is not registered.
pub async fn sources() -> laravel::Result<Vec<Arc<dyn Source>>> {
    Ok(manager().await?.sources())
}

/// Replace the transport with one that records what would have been submitted, and bypass the
/// queue. Assert on the returned [`Fake`].
///
/// The environment guard still applies: submissions only happen in the environments listed in
/// [`IndexNowConfig::environments`], `production` by default, and that includes faked ones. In the
/// `testing` environment nothing is recorded and every assertion fails, so allow it in your
/// test's configuration:
///
/// ```ignore
/// .with_config(|config, _| {
///     config.section_mut::<IndexNowConfig>().environments = vec!["testing".into()];
/// })
/// ```
///
/// # Errors
///
/// Fails when the [`IndexNowServiceProvider`] is not registered.
pub async fn fake() -> laravel::Result<Fake> {
    Ok(manager().await?.fake())
}