Skip to content

Commit

Permalink
basic python binding
Browse files Browse the repository at this point in the history
  • Loading branch information
SichangHe committed May 24, 2024
1 parent f2e8397 commit 538778e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 8 deletions.
68 changes: 60 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,64 @@
# Archive
**.7z
**.bz2
**.gz
**.gzip
**.jar
**.lz4
**.lzma
**.pak
**.rar
**.tar
**.tbz2
**.tgz
**.tlz
**.txz
**.xz
**.zip
**.zst

# Artifacts
**.buildlog/
**.class
**.dart_tool/
**.flutter*
**.ipynb_checkpoints/
**.flutter-plugins
**.flutter-plugins-dependencies
**.history
**.iml
**.ipr
**.iws
**.lock
**.o
**.pub-cache/
**.pub/
**.pyc
**__pycache__/
**build/
**.so
**/.dSYM/
**/.dart_tool/
**/.idea/
**/.ipynb_checkpoints/
**/__pycache__/
**/android/app/debug/
**/android/app/profile/
**/android/app/release/
**/app.*.map.json
**/app.*.symbols
**/build/
**/doc/api/
**/ios/Flutter/.last_build_id
**/migrate_working_dir/
**/node_modules/
**/package-lock.json
**/target/
**/venv/

# Audio
**.wav

# Auxiliary files
**.bak
**.swp
**.tmp

# Databases
Expand All @@ -17,6 +68,7 @@
# Documents
**.csv
**.doc*
**.html
**.pdf
**.ppt*
**.txt
Expand Down Expand Up @@ -72,20 +124,20 @@
**.dvi
**.fdb_latexmk
**.fls
**.gz
**.lof
**.log
**.lot
**.out
**.pdf
**.run.xml
**.synctex.gz
**.toc
**.xcp
**.xdv
**.xml
**blx.bib
**comment.cut

# Specific
**.lock
/target
# Video
**.mkv
**.mov
**.mp4
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.2
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ repository = "https://github.com/SichangHe/fmtt"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "fmtt"
crate-type = ["cdylib", "lib"]

[dependencies]
anyhow = "1"
clap = { version = "4", features = ["derive"] }
pyo3 = { version = "0.21", features = [
"experimental-declarative-modules",
], optional = true }
regex = "1"
tracing = { version = "0.1", default_features = false }
tracing-subscriber = { version = "0.3", default_features = false, features = [
Expand All @@ -22,6 +29,10 @@ tracing-subscriber = { version = "0.3", default_features = false, features = [
[dev-dependencies]
insta = "1.39"

[features]
default = []
py = ["dep:pyo3"]

[profile.release]
opt-level = "s"
lto = true
Expand Down
23 changes: 23 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[project]
name = "fmtt"
version = "0.5.0"
description = "Python binding for ForMaT Text (FMTT)"
authors = [
{ name = "Steven Hé (Sīchàng)", email = "[email protected]" },
]
dependencies = []
readme = "README.md"
requires-python = ">= 3.8"

[build-system]
requires = ["maturin>=1.2,<2.0"]
build-backend = "maturin"

[tool.rye]
managed = true
dev-dependencies = []

[tool.maturin]
python-source = "python"
module-name = "fmtt._lowlevel"
features = ["pyo3/extension-module", "py"]
3 changes: 3 additions & 0 deletions python/fmtt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from fmtt._lowlevel import format

__all__ = ["format"]
48 changes: 48 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,53 @@ pub fn format<'a>(
result
}

#[cfg(feature = "py")]
#[pyo3::pymodule]
mod _lowlevel {
use pyo3::{exceptions::PyValueError, prelude::*};

use super::*;

/// Format text diff-friendly by breaking lines of sensible punctuations and
/// words.
///
/// See <https://github.com/SichangHe/fmtt> for the options.
#[pyfunction]
#[pyo3(signature = (
text,
line_width=80,
allow_indented_paragraphs=false,
single_line_starts=vec![],
multi_line_starts=vec![],
ignore_line_starts=vec![],
))]
fn format(
text: &str,
line_width: usize,
allow_indented_paragraphs: bool,
single_line_starts: Vec<String>,
multi_line_starts: Vec<String>,
ignore_line_starts: Vec<String>,
) -> PyResult<String> {
let paragraph_starts = ParagraphStarts::try_from_str_slices(
&borrowed_str_slice(&single_line_starts),
&borrowed_str_slice(&multi_line_starts),
&borrowed_str_slice(&ignore_line_starts),
)
.map_err(|why| PyValueError::new_err(format!("{why}")))?;
let formatted_words = super::format(
text,
line_width,
allow_indented_paragraphs,
&paragraph_starts,
);
Ok(formatted_words.join(""))
}

fn borrowed_str_slice(slice: &[String]) -> Vec<&str> {
slice.iter().map(String::as_str).collect()
}
}

#[cfg(test)]
mod tests;

0 comments on commit 538778e

Please sign in to comment.