Skip to content

Commit

Permalink
feat: "logseq" crate for reusable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Feb 4, 2024
1 parent 793cb8c commit f5c9ffe
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 50 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 3 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
[dependencies]
pyo3 = {version = "0.20.2"}
regex = "1.10.3"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]
name = "logseq_doctor"

[lints.clippy]
all = "deny"

[lints.rust]
missing_docs = "deny"

# https://doc.rust-lang.org/cargo/reference/manifest.html#the-package-section
[package]
authors = ["W Augusto Andreoli <andreoli@sent.com>"]
categories = ["command-line-utilities", "filesystem", "text-editors", "text-processing", "value-formatting"]
description = "Logseq Doctor: heal your Markdown files"
edition = "2021"
name = "logseq-doctor"
version = "0.1.1"
[workspace]
members = ["rust/logseq", "rust/logseq-doctor"]
resolver = "2"
20 changes: 8 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ build: # Build the Rust crate and Python package
maturin build
.PHONY: build

develop: uncomment-crate # Install the crate as module in the current virtualenv, rehash pyenv to put CLI scripts in PATH
develop: # Install the crate as module in the current virtualenv, rehash pyenv to put CLI scripts in PATH
maturin develop
# Rehashing is needed (once) to make the [project.scripts] section of pyproject.toml available in the PATH
pyenv rehash
$(MAKE) comment-crate
python -m pip freeze
.PHONY: develop

Expand Down Expand Up @@ -40,14 +39,11 @@ cli: develop # Run the CLI with a Python click script as the entry point
lsd --help
.PHONY: cli

comment-crate: # Comment out the crate-type line in Cargo.toml
gsed -i 's/crate-type/#crate-type/' Cargo.toml
.PHONY: comment-crate

uncomment-crate: # Uncomment the crate-type line in Cargo.toml
gsed -i 's/#*crate-type/crate-type/' Cargo.toml
.PHONY: uncomment-crate

test-rs: comment-crate # Run tests and doctests on Rust code
test: # Run tests on both Python and Rust
cargo test
.PHONY: test-rs
tox -e py311
.PHONY: test

test-watch: # Run tests and watch for changes
source .tox/py311/bin/activate && ptw --runner "pytest --testmon"
.PHONY: test-watch
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ target-version = ['py38']
# https://www.maturin.rs/config
[tool.maturin]
features = ["pyo3/extension-module"]
manifest-path = "rust/logseq-doctor/Cargo.toml"
module-name = "logseq_doctor._logseq_doctor"
python-source = "python"

Expand Down
23 changes: 23 additions & 0 deletions rust/logseq-doctor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[dependencies]
logseq = {path = "../logseq"}
pyo3 = {version = "0.20.2"}

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]
name = "logseq_doctor"

[lints.clippy]
all = "deny"

[lints.rust]
missing_docs = "deny"

# https://doc.rust-lang.org/cargo/reference/manifest.html#the-package-section
[package]
authors = ["W Augusto Andreoli <andreoli@sent.com>"]
categories = ["command-line-utilities", "filesystem", "text-editors", "text-processing", "value-formatting"]
description = "Logseq Doctor: heal your Markdown files"
edition = "2021"
name = "logseq-doctor"
version = "0.1.1"
15 changes: 15 additions & 0 deletions rust/logseq-doctor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Logseq Doctor: heal your Markdown files
//!
//! Python extension written in Rust, until the whole project is ported to Rust.
use pyo3::prelude::*;

#[pymodule]
fn _logseq_doctor(_python: Python, module: &PyModule) -> PyResult<()> {
module.add_function(wrap_pyfunction!(rust_remove_consecutive_spaces, module)?)?;
Ok(())
}

#[pyfunction]
fn rust_remove_consecutive_spaces(file_contents: String) -> PyResult<String> {
Ok(logseq::remove_consecutive_spaces(file_contents).unwrap())
}
9 changes: 9 additions & 0 deletions rust/logseq/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
regex = "1.10.3"

[package]
edition = "2021"
name = "logseq"
version = "0.1.0"
15 changes: 1 addition & 14 deletions src/lib.rs → rust/logseq/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
//! Logseq Doctor: heal your Markdown files
use pyo3::prelude::*;
use regex::Regex;

#[pymodule]
fn _logseq_doctor(_python: Python, module: &PyModule) -> PyResult<()> {
module.add_function(wrap_pyfunction!(rust_remove_consecutive_spaces, module)?)?;
Ok(())
}

#[pyfunction]
fn rust_remove_consecutive_spaces(file_contents: String) -> PyResult<String> {
Ok(remove_consecutive_spaces(file_contents).unwrap())
}

/// Remove consecutive spaces on lines that begin with a dash, keeping leading spaces
///
/// # Arguments
Expand All @@ -24,7 +11,7 @@ fn rust_remove_consecutive_spaces(file_contents: String) -> PyResult<String> {
/// # Examples
///
/// ```
/// use logseq_doctor::remove_consecutive_spaces;
/// use logseq::remove_consecutive_spaces;
/// assert_eq!(remove_consecutive_spaces(" abc 123 def ".to_string()).unwrap(), " abc 123 def ".to_string());
/// assert_eq!(remove_consecutive_spaces("\n - abc 123\n - def 4 5 ".to_string()).unwrap(), "\n - abc 123\n - def 4 5 ".to_string());
/// assert_eq!(remove_consecutive_spaces(
Expand Down

0 comments on commit f5c9ffe

Please sign in to comment.