use parse_display_derive::{Display, FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::shared::{FileExportFormat, FileImportFormat};
pub mod fbx;
pub mod gltf;
pub mod obj;
pub mod ply;
pub mod step;
pub mod stl;
pub mod sldprt;
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[serde(tag = "type", rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum OutputFormat {
#[display("{}: {0}")]
Fbx(fbx::export::Options),
#[display("{}: {0}")]
Gltf(gltf::export::Options),
#[display("{}: {0}")]
Obj(obj::export::Options),
#[display("{}: {0}")]
Ply(ply::export::Options),
#[display("{}: {0}")]
Step(step::ExportOptions),
#[display("{}: {0}")]
Stl(stl::export::Options),
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[serde(tag = "type", rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum InputFormat {
#[display("{}: {0}")]
Fbx(fbx::import::Options),
#[display("{}: {0}")]
Gltf(gltf::import::Options),
#[display("{}: {0}")]
Obj(obj::import::Options),
#[display("{}: {0}")]
Ply(ply::import::Options),
#[display("{}: {0}")]
Sldprt(sldprt::import::Options),
#[display("{}: {0}")]
Step(step::ImportOptions),
#[display("{}: {0}")]
Stl(stl::import::Options),
}
#[derive(Clone, Debug, Default, Display, Eq, FromStr, Hash, PartialEq, JsonSchema, Deserialize, Serialize)]
#[display(style = "snake_case")]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum Selection {
#[default]
DefaultScene,
#[display("{}: {index}")]
SceneByIndex {
index: usize,
},
#[display("{}: {name}")]
SceneByName {
name: String,
},
#[display("{}: {index}")]
MeshByIndex {
index: usize,
},
#[display("{}: {name}")]
MeshByName {
name: String,
},
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct VirtualFile {
pub path: std::path::PathBuf,
pub data: Vec<u8>,
}
impl VirtualFile {
pub fn has_extension(&self, required_extension: &str) -> bool {
self.path
.extension()
.and_then(std::ffi::OsStr::to_str)
.map(|extension| extension.eq_ignore_ascii_case(required_extension))
.unwrap_or(false)
}
fn read_fs_impl(path: std::path::PathBuf) -> std::io::Result<Self> {
let data = std::fs::read(&path)?;
Ok(Self { path, data })
}
pub fn read_fs<P>(path: P) -> std::io::Result<Self>
where
P: Into<std::path::PathBuf>,
{
Self::read_fs_impl(path.into())
}
}
impl From<OutputFormat> for FileExportFormat {
fn from(output_format: OutputFormat) -> Self {
match output_format {
OutputFormat::Fbx(_) => Self::Fbx,
OutputFormat::Gltf(_) => Self::Gltf,
OutputFormat::Obj(_) => Self::Obj,
OutputFormat::Ply(_) => Self::Ply,
OutputFormat::Step(_) => Self::Step,
OutputFormat::Stl(_) => Self::Stl,
}
}
}
impl From<FileExportFormat> for OutputFormat {
fn from(export_format: FileExportFormat) -> Self {
match export_format {
FileExportFormat::Fbx => OutputFormat::Fbx(Default::default()),
FileExportFormat::Glb => OutputFormat::Gltf(gltf::export::Options {
storage: gltf::export::Storage::Binary,
..Default::default()
}),
FileExportFormat::Gltf => OutputFormat::Gltf(gltf::export::Options {
storage: gltf::export::Storage::Embedded,
presentation: gltf::export::Presentation::Pretty,
}),
FileExportFormat::Obj => OutputFormat::Obj(Default::default()),
FileExportFormat::Ply => OutputFormat::Ply(Default::default()),
FileExportFormat::Step => OutputFormat::Step(Default::default()),
FileExportFormat::Stl => OutputFormat::Stl(stl::export::Options {
storage: stl::export::Storage::Ascii,
..Default::default()
}),
}
}
}
impl From<InputFormat> for FileImportFormat {
fn from(input_format: InputFormat) -> Self {
match input_format {
InputFormat::Fbx(_) => Self::Fbx,
InputFormat::Gltf(_) => Self::Gltf,
InputFormat::Obj(_) => Self::Obj,
InputFormat::Ply(_) => Self::Ply,
InputFormat::Sldprt(_) => Self::Sldprt,
InputFormat::Step(_) => Self::Step,
InputFormat::Stl(_) => Self::Stl,
}
}
}
impl From<FileImportFormat> for InputFormat {
fn from(import_format: FileImportFormat) -> Self {
match import_format {
FileImportFormat::Fbx => InputFormat::Fbx(Default::default()),
FileImportFormat::Gltf => InputFormat::Gltf(Default::default()),
FileImportFormat::Obj => InputFormat::Obj(Default::default()),
FileImportFormat::Ply => InputFormat::Ply(Default::default()),
FileImportFormat::Sldprt => InputFormat::Sldprt(Default::default()),
FileImportFormat::Step => InputFormat::Step(Default::default()),
FileImportFormat::Stl => InputFormat::Stl(Default::default()),
}
}
}