diff --git a/src/lib.rs b/src/lib.rs index 8ad9deb..5535c35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,14 +33,17 @@ pub mod util { pub mod pythonapi { pub mod shell_like; + pub mod pyshell; + // pub mod testapi; } use pyo3::prelude::*; -use pythonapi::shell_like::PyTty; +use pythonapi::{pyshell::PyShell, shell_like::PyTty}; #[pymodule] fn tester(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; + m.add_class::()?; Ok(()) } diff --git a/src/pythonapi/pyshell.rs b/src/pythonapi/pyshell.rs new file mode 100644 index 0000000..6008039 --- /dev/null +++ b/src/pythonapi/pyshell.rs @@ -0,0 +1,30 @@ +use pyo3::{exceptions::PyTypeError, pyclass, pymethods, PyResult}; + +use crate::{term::shell::Shell, util::anybase::heap_raw}; + +use super::shell_like::{PyTty, PyTtyWrapper, TtyType}; + +#[pyclass(extends=PyTty, subclass)] +pub struct PyShell {} + +#[pymethods] +impl PyShell { + #[new] + #[pyo3(signature = (shell=None))] + fn py_new(shell: Option<&str>) -> PyResult<(Self, PyTty)> { + let shell = Shell::build(shell); + if let Err(e) = shell { + return Err(PyTypeError::new_err(e.to_string())); + } + let shell = shell.unwrap(); + let shell = Box::new(shell) as TtyType; + Ok(( + PyShell {}, + PyTty::build( + PyTtyWrapper { + tty: heap_raw(shell), + }, + ), + )) + } +} diff --git a/src/pythonapi/shell_like.rs b/src/pythonapi/shell_like.rs index d417739..23712cc 100644 --- a/src/pythonapi/shell_like.rs +++ b/src/pythonapi/shell_like.rs @@ -12,13 +12,13 @@ use crate::{ shell::Shell, ssh::Ssh, tty::{DynTty, WrapperTty}, - }, + }, util::anybase::heap_raw, }; -type TtyType = DynTty; +pub type TtyType = DynTty; -struct PyTtyWrapper { - tty: *mut TtyType, +pub struct PyTtyWrapper { + pub tty: *mut TtyType, } impl PyTtyWrapper { @@ -56,11 +56,19 @@ impl PyTtyWrapper { unsafe impl Send for PyTtyWrapper {} -#[pyclass] +#[pyclass(subclass)] pub struct PyTty { inner: PyTtyWrapper, } +impl PyTty { + pub fn build(inner: PyTtyWrapper) -> Self { + PyTty { + inner + } + } +} + #[derive(Deserialize)] struct PyTtyConf { // unwrapable @@ -77,10 +85,6 @@ struct PyTtyShellConf { shell: Option, } -fn heap_raw(t: T) -> *mut T { - Box::into_raw(Box::new(t)) -} - fn handel_wrap(inner: &mut Option, be_wrapped: Option<&mut PyTty>) -> PyResult<()> { if be_wrapped.is_none() { return Err(PyTypeError::new_err( @@ -402,3 +406,4 @@ impl PyTty { } } } + diff --git a/src/util/anybase.rs b/src/util/anybase.rs index 2677f0a..28ff93d 100644 --- a/src/util/anybase.rs +++ b/src/util/anybase.rs @@ -26,3 +26,7 @@ macro_rules! dyn_into { $x.into_any().downcast::<$t>() }; } + +pub fn heap_raw(t: T) -> *mut T { + Box::into_raw(Box::new(t)) +} \ No newline at end of file