use inker::{
DocumentProvenance, DocumentTrustState, Engine, EngineDocument, EngineError, EngineInput,
};
use crate::GemtextEngine;
pub const ENGINE_ID: &str = "nematic.guppy";
pub struct GuppyEngine {
gemtext: GemtextEngine,
}
impl GuppyEngine {
pub fn new() -> Self {
Self {
gemtext: GemtextEngine::new(),
}
}
}
impl Default for GuppyEngine {
fn default() -> Self {
Self::new()
}
}
impl Engine for GuppyEngine {
fn engine_id(&self) -> &str {
ENGINE_ID
}
fn render(&self, input: &EngineInput) -> Result<EngineDocument, EngineError> {
let mut doc = self.gemtext.render(input)?;
doc.provenance = DocumentProvenance {
source_kind: Some(self.engine_id().to_string()),
canonical_uri: Some(input.address.clone()),
fetched_at: None,
source_label: Some("nematic.gemtext".to_string()),
};
doc.trust = DocumentTrustState::Unknown;
Ok(doc)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn render(body: &str) -> EngineDocument {
GuppyEngine::new()
.render(&EngineInput::new("guppy://example.test/", body))
.expect("render")
}
#[test]
fn engine_id_is_stable() {
assert_eq!(GuppyEngine::new().engine_id(), "nematic.guppy");
}
#[test]
fn body_parses_as_gemtext() {
let doc = render("# Hi\n\n=> guppy://capsule.test/ Visit\n");
assert_eq!(doc.title.as_deref(), Some("Hi"));
assert_eq!(doc.outgoing_links(), vec!["guppy://capsule.test/"]);
}
#[test]
fn provenance_records_guppy_with_gemtext_label() {
let doc = render("body\n");
assert_eq!(doc.provenance.source_kind.as_deref(), Some("nematic.guppy"));
assert_eq!(
doc.provenance.source_label.as_deref(),
Some("nematic.gemtext")
);
}
}