Skip to content

Commit

Permalink
Add bindings for features added in piston_rs#12 (#6)
Browse files Browse the repository at this point in the history
* Add bindings for features added in Jonxslays/piston_rs#12

* Fix typo

* Add load_from and load_content_from method impls

Co-authored-by: Jonxslays <[email protected]>
  • Loading branch information
parafoxia and Jonxslays authored Jun 30, 2022
1 parent 9ceb26f commit 9cd4ade
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
34 changes: 29 additions & 5 deletions piston_rspy/piston_rspy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ class File:
name: str = ""
content: str = ""
encoding: str = "utf8"
def set_name(self, name: str) -> File:
"""Sets the name of the file.
@classmethod
def load_from(cls, path: str) -> File:
"""Creates a new `File` from an existing file on disk.
Args:
name: `str`
The name to use.
path: `str`
The path to the file.
Returns:
`File`: The file, for chained method calls.
`File`: The new file.
"""
...
def set_content(self, content: str) -> File:
Expand All @@ -78,6 +79,29 @@ class File:
content: `str`
The content to use.
Returns:
`File`: The file, for chained method calls.
"""
...
def load_content_from(self, path: str) -> File:
"""Sets the content of the file to the contents of an existing
file on disk.
Args:
path: `str`
The path to the file.
Returns:
`File`: The file, for chained method calls.
"""
...
def set_name(self, name: str) -> File:
"""Sets the name of the file.
Args:
name: `str`
The name to use.
Returns:
`File`: The file, for chained method calls.
"""
Expand Down
48 changes: 48 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use pyo3::PyObjectProtocol;

use piston_rs::File as File_;
use piston_rs::Runtime as Runtime_;
use pyo3::types::PyType;

/// A runtime available to be used by Piston.
///
Expand Down Expand Up @@ -217,6 +218,53 @@ impl File {
slf
}

/// Creates a new `File` from an existing file on disk.
///
/// ### Args:
///
/// - path `str`:
/// The path to the file.
///
/// ### Returns:
///
/// - `File`: The new file.
#[classmethod]
#[pyo3(text_signature = "(cls, path: str, /) -> File")]
fn load_from(_cls: &PyType, path: String) -> PyResult<Self> {
match File_::load_from(path.as_str()) {
Ok(file) => Ok(Python::with_gil(|_| Self { inner: file })),
Err(err) => Err(Python::with_gil(|_| {
pyo3::exceptions::PyRuntimeError::new_err(format!("{:?}", err.details))
})),
}
}

/// Sets the content of the file to the contents of an existing
/// file on disk.
///
/// ### Args:
///
/// - path `str`:
/// The path to the file.
///
/// ### Returns:
///
/// - `File`: The file, for chained method calls.
#[pyo3(text_signature = "(self, path: str, /) -> File")]
fn load_content_from(mut slf: PyRefMut<Self>, path: String) -> PyResult<PyRefMut<Self>> {
let file = slf.inner.clone();

match file.load_content_from(path.as_str()) {
Ok(file) => {
slf.inner.content = file.content;
Ok(slf)
}
Err(err) => Err(Python::with_gil(|_| {
pyo3::exceptions::PyRuntimeError::new_err(format!("{:?}", err.details))
})),
}
}

/// Copies the file, leaving the existing one unchanged.
///
/// ### Returns:
Expand Down

0 comments on commit 9cd4ade

Please sign in to comment.