feat: add python bingen

This commit is contained in:
leeeon233 2023-01-28 19:59:37 +08:00 committed by Leonzhao
parent c10b1237e0
commit 83972746a6
5 changed files with 187 additions and 1 deletions

97
Cargo.lock generated
View file

@ -381,7 +381,7 @@ dependencies = [
"autocfg", "autocfg",
"cfg-if", "cfg-if",
"crossbeam-utils", "crossbeam-utils",
"memoffset", "memoffset 0.7.1",
"scopeguard", "scopeguard",
] ]
@ -646,6 +646,12 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "indoc"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da2d6f23ffea9d7e76c53eee25dfb67bcd8fde7f1198b0855350698c9f07c780"
[[package]] [[package]]
name = "instant" name = "instant"
version = "0.1.12" version = "0.1.12"
@ -793,6 +799,15 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "memoffset"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "memoffset" name = "memoffset"
version = "0.7.1" version = "0.7.1"
@ -1145,6 +1160,74 @@ dependencies = [
"syn 0.15.44", "syn 0.15.44",
] ]
[[package]]
name = "pyloro"
version = "0.1.0"
dependencies = [
"loro-internal",
"pyo3",
]
[[package]]
name = "pyo3"
version = "0.17.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "268be0c73583c183f2b14052337465768c07726936a260f480f0857cb95ba543"
dependencies = [
"cfg-if",
"indoc",
"libc",
"memoffset 0.6.5",
"parking_lot",
"pyo3-build-config",
"pyo3-ffi",
"pyo3-macros",
"unindent",
]
[[package]]
name = "pyo3-build-config"
version = "0.17.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28fcd1e73f06ec85bf3280c48c67e731d8290ad3d730f8be9dc07946923005c8"
dependencies = [
"once_cell",
"target-lexicon",
]
[[package]]
name = "pyo3-ffi"
version = "0.17.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f6cb136e222e49115b3c51c32792886defbfb0adead26a688142b346a0b9ffc"
dependencies = [
"libc",
"pyo3-build-config",
]
[[package]]
name = "pyo3-macros"
version = "0.17.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94144a1266e236b1c932682136dc35a9dee8d3589728f68130c7c3861ef96b28"
dependencies = [
"proc-macro2 1.0.49",
"pyo3-macros-backend",
"quote 1.0.23",
"syn 1.0.107",
]
[[package]]
name = "pyo3-macros-backend"
version = "0.17.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8df9be978a2d2f0cdebabb03206ed73b11314701a5bfe71b0d753b81997777f"
dependencies = [
"proc-macro2 1.0.49",
"quote 1.0.23",
"syn 1.0.107",
]
[[package]] [[package]]
name = "quick-error" name = "quick-error"
version = "1.2.3" version = "1.2.3"
@ -1584,6 +1667,12 @@ dependencies = [
"syn 1.0.107", "syn 1.0.107",
] ]
[[package]]
name = "target-lexicon"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9410d0f6853b1d94f0e519fb95df60f29d2c1eff2d921ffdf01a4c8a3b54f12d"
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.3.0" version = "3.3.0"
@ -1745,6 +1834,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
[[package]]
name = "unindent"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1766d682d402817b5ac4490b3c3002d91dfa0d22812f341609f97b08757359c"
[[package]] [[package]]
name = "untrusted" name = "untrusted"
version = "0.7.1" version = "0.7.1"

View file

@ -0,0 +1,10 @@
[package]
name = "pyloro"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pyo3 = { version = "0.17", features = ["extension-module"] }
loro-internal = { path = "../loro-internal" }

View file

@ -0,0 +1,7 @@
# loro-python
```bash
pip3 install maturin
maturin build # or python3 -m maturin build
```

View file

@ -0,0 +1,6 @@
from pyloro import Loro, LoroText;
loro = Loro()
text = loro.get_text("text")
text.insert(loro, 0, "123")
print(text.value())

View file

@ -0,0 +1,68 @@
use std::ops::{Deref, DerefMut};
use loro_internal::{LoroCore, Text};
use pyo3::prelude::*;
#[pyclass]
struct Loro(LoroCore);
#[pyclass]
struct LoroText(Text);
impl Deref for Loro {
type Target = LoroCore;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Loro {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Deref for LoroText {
type Target = Text;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for LoroText {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[pymethods]
impl Loro {
#[new]
pub fn __new__() -> Self {
Self(LoroCore::default())
}
pub fn get_text(&mut self, id: &str) -> LoroText {
let text = self.0.get_text(id);
LoroText(text)
}
}
#[pymethods]
impl LoroText {
pub fn insert(&mut self, ctx: &Loro, pos: usize, value: &str) -> PyResult<()> {
self.0.insert(&ctx.0, pos, value).unwrap();
Ok(())
}
pub fn value(&self) -> String {
self.0.get_value().into_string().unwrap().into_string()
}
}
#[pymodule]
fn pyloro(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Loro>()?;
m.add_class::<LoroText>()?;
Ok(())
}