Skip to content

Commit

Permalink
fix python
Browse files Browse the repository at this point in the history
  • Loading branch information
hardliner66 committed Dec 29, 2023
1 parent 0ec831a commit c57a0c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/generators/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ pub fn generate(
let raw = parse_raw_data(input.file)?;
tera.render(
&template.to_string_lossy(),
&Context::from_serialize(&RawModel { raw, defines })?,
&Context::from_serialize(RawModel { raw, defines })?,
)?
} else {
let module = parse_file(base, input.file)?;
let module = parse_file(base, &input.file)?;
let module = update_types(module, input.no_map, input.typemap, None)?;
tera.render(
&template.to_string_lossy(),
&Context::from_serialize(&SsdModel { module, defines })?,
&Context::from_serialize(SsdModel { module, defines })?,
)?
};
print_or_write(out.out, &result)?;
Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use ssd_data::{

#[cfg(feature = "_python")]
mod python {
use std::path::PathBuf;
use std::path::Path;

use pyo3::exceptions::PyException;
use pyo3::prelude::*;
Expand All @@ -21,19 +21,20 @@ mod python {
use ssd_data::SsdModule;

#[pyfunction]
pub fn parse(content: String, namespace: String) -> PyResult<SsdModule> {
crate::parse(&content, Namespace::new(&namespace))
pub fn parse(content: &str, namespace: &str) -> PyResult<SsdModule> {
crate::parse(content, Namespace::new(namespace))
.map_err(|e| PyException::new_err(e.to_string()))
}

#[pyfunction]
pub fn parse_file(base: PathBuf, path: PathBuf) -> PyResult<SsdModule> {
crate::parse_file(base, path).map_err(|e| PyException::new_err(e.to_string()))
pub fn parse_file(base: &str, path: &str) -> PyResult<SsdModule> {
crate::parse_file(&Path::new(base), &Path::new(path))
.map_err(|e| PyException::new_err(e.to_string()))
}

#[pyfunction]
pub fn parse_file_with_namespace(path: PathBuf, namespace: String) -> PyResult<SsdModule> {
crate::parse_file_with_namespace(path, Namespace::new(&namespace))
pub fn parse_file_with_namespace(path: &str, namespace: &str) -> PyResult<SsdModule> {
crate::parse_file_with_namespace(Path::new(path), Namespace::new(namespace))
.map_err(|e| PyException::new_err(e.to_string()))
}

Expand Down
14 changes: 8 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Write, num::ParseIntError, path::PathBuf};
use std::{io::Write, num::ParseIntError, path::Path};

use once_cell::sync::Lazy;
use pest::{
Expand Down Expand Up @@ -594,7 +594,7 @@ pub(crate) fn raw_to_ssd_file(namespace: Namespace, raw: &[AstElement]) -> SsdMo
SsdModule::new(namespace, imports, datatypes, enums, services)
}

pub fn parse_file_raw(path: &PathBuf) -> Result<Vec<AstElement>, ParseError> {
pub fn parse_file_raw<P: AsRef<Path>>(path: P) -> Result<Vec<AstElement>, ParseError> {
let content = std::fs::read_to_string(path).map_err(ParseError::from_dyn_error)?;

parse_raw(&content)
Expand All @@ -608,13 +608,15 @@ pub fn parse_file_raw(path: &PathBuf) -> Result<Vec<AstElement>, ParseError> {
///
/// * `base` - The base path of the file.
/// * `path` - The path to the file to parse.
pub fn parse_file(base: &PathBuf, path: &PathBuf) -> Result<SsdModule, ParseError> {
pub fn parse_file<P: AsRef<Path>>(base: &P, path: &P) -> Result<SsdModule, ParseError> {
let base = base.as_ref();
let path = path.as_ref();
let mut components = if path.starts_with(base) {
path.strip_prefix(base)
.map_err(ParseError::from_dyn_error)?
.to_owned()
} else {
path.clone()
path.to_owned()
};

components.set_extension("");
Expand All @@ -627,8 +629,8 @@ pub fn parse_file(base: &PathBuf, path: &PathBuf) -> Result<SsdModule, ParseErro
}

#[allow(unused)]
pub fn parse_file_with_namespace(
path: &PathBuf,
pub fn parse_file_with_namespace<P: AsRef<Path>>(
path: P,
namespace: Namespace,
) -> Result<SsdModule, ParseError> {
let raw = parse_file_raw(path)?;
Expand Down

0 comments on commit c57a0c2

Please sign in to comment.