use std::collections::HashMap;
use std::path::Path;
use crate::attribute::Attribute;
use crate::datamodel;
use crate::exporters::Templates;
use crate::markdown::frontmatter::FrontMatter;
use crate::object::{Enumeration, Object};
use crate::option::AttrOption;
use pyo3::prelude::*;
use pyo3::types::PyType;
#[pyclass]
pub struct DataModel {
#[pyo3(get)]
pub model: datamodel::DataModel,
}
#[pymethods]
impl DataModel {
#[classmethod]
#[pyo3(signature = (path))]
fn from_markdown(_cls: &Bound<'_, PyType>, path: String) -> Self {
Self {
model: datamodel::DataModel::from_markdown(Path::new(&path)).unwrap(),
}
}
#[classmethod]
#[pyo3(signature = (path))]
fn from_json_schema(_cls: &Bound<'_, PyType>, path: String) -> Self {
Self {
model: datamodel::DataModel::from_json_schema(Path::new(&path)).unwrap(),
}
}
#[classmethod]
#[pyo3(signature = (content))]
fn from_json_schema_string(_cls: &Bound<'_, PyType>, content: String) -> Self {
Self {
model: datamodel::DataModel::from_json_schema_string(&content).unwrap(),
}
}
#[classmethod]
#[pyo3(signature = (content))]
fn from_markdown_string(_cls: &Bound<'_, PyType>, content: String) -> Self {
Self {
model: datamodel::DataModel::from_markdown_string(&content).unwrap(),
}
}
fn __repr__(&self) -> String {
self.model.internal_schema()
}
#[pyo3(signature = (template, config=None))]
fn convert_to(
&mut self,
template: Templates,
config: Option<HashMap<String, String>>,
) -> String {
let config = config.unwrap_or_default();
self.model
.convert_to(&template, Some(&config))
.expect("Failed to convert to template")
}
}
#[pymethods]
impl Object {
pub fn __repr__(&self) -> String {
serde_json::to_string_pretty(&self).unwrap()
}
}
#[pymethods]
impl Attribute {
fn __repr__(&self) -> String {
serde_json::to_string_pretty(&self).unwrap()
}
}
#[pymethods]
impl FrontMatter {
fn __repr__(&self) -> String {
serde_json::to_string_pretty(&self).unwrap()
}
}
#[pymethods]
impl Enumeration {
fn __repr__(&self) -> String {
serde_json::to_string_pretty(&self).unwrap()
}
}
#[pymethods]
impl AttrOption {
fn pair(&self) -> (String, String) {
self.to_pair()
}
fn k(&self) -> String {
self.key().to_string()
}
fn v(&self) -> String {
self.value().to_string()
}
fn __repr__(&self) -> String {
serde_json::to_string_pretty(&self).unwrap()
}
}