use std::ffi::CString;
use std::{
collections::{BTreeMap, HashSet},
default::Default,
};
#[derive(Clone, Debug, PartialEq)]
pub enum AsyncRuntime {
Tokio,
AsyncStd,
Smol,
Custom {
attribute: String,
import: String,
},
}
impl Default for AsyncRuntime {
fn default() -> Self {
AsyncRuntime::Tokio
}
}
impl AsyncRuntime {
pub fn main_attribute(&self) -> &str {
match self {
AsyncRuntime::Tokio => "tokio::main",
AsyncRuntime::AsyncStd => "async_std::main",
AsyncRuntime::Smol => "smol::main",
AsyncRuntime::Custom { attribute, .. } => attribute,
}
}
pub fn import(&self) -> &str {
match self {
AsyncRuntime::Tokio => "tokio",
AsyncRuntime::AsyncStd => "async_std",
AsyncRuntime::Smol => "smol",
AsyncRuntime::Custom { import, .. } => import,
}
}
}
#[derive(Clone, Debug)]
pub struct PythonOptions {
pub python_namespace: String,
pub python_path: Vec<String>,
pub imports: BTreeMap<String, HashSet<String>>,
pub stdpython: String,
pub with_std_python: bool,
pub allow_unsafe: bool,
pub async_runtime: AsyncRuntime,
pub lossy_warnings: bool,
pub optional_names: std::rc::Rc<std::collections::HashSet<String>>,
pub clone_str_attribute_returns: bool,
pub local_types: std::rc::Rc<std::collections::HashMap<String, String>>,
pub no_std: bool,
}
impl Default for PythonOptions {
fn default() -> Self {
Self {
python_namespace: String::from("__python_namespace__"),
python_path: Vec::new(),
imports: BTreeMap::new(),
stdpython: "stdpython".to_string(),
with_std_python: true,
allow_unsafe: false,
async_runtime: AsyncRuntime::default(),
lossy_warnings: true,
optional_names: std::rc::Rc::new(std::collections::HashSet::new()),
clone_str_attribute_returns: false,
local_types: std::rc::Rc::new(std::collections::HashMap::new()),
no_std: false,
}
}
}
impl PythonOptions {
pub fn with_tokio() -> Self {
let mut options = Self::default();
options.async_runtime = AsyncRuntime::Tokio;
options
}
pub fn with_async_std() -> Self {
let mut options = Self::default();
options.async_runtime = AsyncRuntime::AsyncStd;
options
}
pub fn with_smol() -> Self {
let mut options = Self::default();
options.async_runtime = AsyncRuntime::Smol;
options
}
pub fn with_custom_runtime(attribute: impl Into<String>, import: impl Into<String>) -> Self {
let mut options = Self::default();
options.async_runtime = AsyncRuntime::Custom { attribute: attribute.into(), import: import.into() };
options
}
pub fn set_async_runtime(&mut self, runtime: AsyncRuntime) -> &mut Self {
self.async_runtime = runtime;
self
}
}