use std::sync::Arc;
use std::sync::OnceLock;
use canvist_core::Editor;
use canvist_core::EditorBuilder;
use canvist_core::Platform;
use canvist_model::Document;
use canvist_model::Schema;
use serde_json::Value as Json;
use crate::Doc;
use crate::Format;
use crate::HeadlessError;
use crate::Snapshot;
use crate::format;
type Configure<H> = dyn Fn(&mut EditorBuilder) -> H + Send + Sync;
pub struct Engine<H = ()> {
configure: Arc<Configure<H>>,
handles: H,
schema: Arc<Schema>,
metadata: Arc<OnceLock<Json>>,
client: u32,
platform: Platform,
}
impl<H: Copy> Clone for Engine<H> {
fn clone(&self) -> Self {
Self {
configure: self.configure.clone(),
handles: self.handles,
schema: self.schema.clone(),
metadata: self.metadata.clone(),
client: self.client,
platform: self.platform,
}
}
}
impl<H> std::fmt::Debug for Engine<H> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Engine")
.field("client", &self.client)
.finish_non_exhaustive()
}
}
#[cfg(feature = "starter-kit")]
impl Engine<canvist_extensions::StarterKitHandles> {
#[must_use]
pub fn starter_kit() -> Self {
Self::new(canvist_extensions::StarterKit::add_all)
.expect("the StarterKit is a valid configuration")
}
}
impl<H: Copy> Engine<H> {
pub fn new(
configure: impl Fn(&mut EditorBuilder) -> H + Send + Sync + 'static,
) -> Result<Self, HeadlessError> {
let mut builder = Editor::builder();
let handles = configure(&mut builder);
let editor = builder.build().map_err(HeadlessError::Config)?;
Ok(Self {
configure: Arc::new(configure),
handles,
schema: editor.schema().clone(),
metadata: Arc::new(OnceLock::new()),
client: 1,
platform: Platform::default(),
})
}
#[must_use]
pub fn with_client(mut self, client: u32) -> Self {
self.client = client;
self
}
#[must_use]
pub fn with_platform(mut self, platform: Platform) -> Self {
self.platform = platform;
self
}
#[must_use]
pub fn handles(&self) -> H {
self.handles
}
#[must_use]
pub fn schema(&self) -> &Arc<Schema> {
&self.schema
}
#[must_use]
pub fn metadata(&self) -> &Json {
self.metadata.get_or_init(|| {
let mut builder = Editor::builder();
(self.configure)(&mut builder);
builder
.build()
.expect("`Engine::new` built the same extensions without error")
.metadata_json()
})
}
#[must_use]
pub fn client(&self) -> u32 {
self.client
}
pub fn create(&self) -> Result<Doc, HeadlessError> {
self.wrap(Document::new(self.schema.clone(), self.client))
}
pub fn parse(&self, format: Format, input: &str) -> Result<Doc, HeadlessError> {
self.wrap(format::read(&self.schema, self.client, format, input)?)
}
pub fn from_json(&self, json: &Json) -> Result<Doc, HeadlessError> {
self.wrap(Document::from_json(self.schema.clone(), self.client, json)?)
}
pub fn restore(&self, snapshot: &Snapshot) -> Result<Doc, HeadlessError> {
let mut doc = self.from_json(snapshot.content())?;
if let Some((anchor, head)) = snapshot.selection() {
doc.select_range(anchor, head)?;
}
Ok(doc)
}
pub(crate) fn wrap(&self, doc: Document) -> Result<Doc, HeadlessError> {
Ok(Doc::new(self.editor(doc)?))
}
pub(crate) fn editor(&self, doc: Document) -> Result<Editor, HeadlessError> {
debug_assert!(Arc::ptr_eq(doc.schema(), &self.schema));
let mut builder = Editor::builder()
.client(self.client)
.platform(self.platform);
(self.configure)(&mut builder);
builder
.build_with(|_, _| Ok(doc))
.map_err(HeadlessError::Config)
}
}