Files
schemsearch/schemsearch-cli/src/types.rs
Chaoscaot 7d9e7f28a9 Fix SQL
2023-04-23 12:17:59 +02:00

58 lines
1.7 KiB
Rust

/*
* Copyright (C) 2023 Chaoscaot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::io::Cursor;
use std::path::PathBuf;
#[cfg(feature = "sql")]
use futures::executor::block_on;
use schemsearch_files::SchematicVersioned;
#[cfg(feature = "sql")]
use schemsearch_sql::{load_schemdata, SchematicNode};
pub enum SchematicSupplierType {
PATH(Box<PathSchematicSupplier>),
#[cfg(feature = "sql")]
SQL(SqlSchematicSupplier),
}
pub struct PathSchematicSupplier {
pub path: PathBuf,
}
impl PathSchematicSupplier {
pub fn get_name(&self) -> String {
self.path.file_stem().unwrap().to_str().unwrap().to_string()
}
}
#[cfg(feature = "sql")]
pub struct SqlSchematicSupplier {
pub node: SchematicNode,
}
#[cfg(feature = "sql")]
impl SqlSchematicSupplier {
pub fn get_schematic(&self) -> Result<SchematicVersioned, String> {
let mut schemdata = block_on(load_schemdata(self.node.id));
SchematicVersioned::load_data(&mut Cursor::new(schemdata.as_mut_slice()))
}
pub fn get_name(&self) -> String {
format!("{} ({})", self.node.name, self.node.id)
}
}