Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
toblux committed Jan 27, 2024
0 parents commit c2ad76f
Show file tree
Hide file tree
Showing 11 changed files with 462 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Run tests
on: [push, pull_request]

jobs:
test_macos:
runs-on: macos-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Install libeditorconfig
run: brew install editorconfig
- name: Run tests
run: cargo test
test_ubuntu:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Install libeditorconfig
run: sudo apt-get update && sudo apt-get install libeditorconfig-dev
- name: Run tests
run: cargo test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
22 changes: 22 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "editorconfig-sys"
version = "0.1.0"
edition = "2021"
authors = ["Thorsten Blum <[email protected]>"]
homepage = "https://github.com/toblux/editorconfig-sys"
repository = "https://github.com/toblux/editorconfig-sys"
documentation = "https://docs.rs/editorconfig-sys"
description = "Native Rust bindings to libeditorconfig"
readme = "README.md"
license = "MIT"
keywords = ["editorconfig", "libeditorconfig", "bindings", "ffi", "sys"]
categories = ["external-ffi-bindings"]
links = "editorconfig"
build = "build.rs"

[dev-dependencies]
rand = "0.8.5"

[build-dependencies]
bindgen = "0.69.2"
pkg-config = "0.3.29"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Thorsten Blum

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Native Rust Bindings to libeditorconfig

This crate uses [bindgen](https://crates.io/crates/bindgen) and [pkg-config](https://crates.io/crates/pkg-config) to automatically generate Rust FFI bindings to the [EditorConfig Core C](https://github.com/editorconfig/editorconfig-core-c) library.

Following the `*-sys` package convention, `editorconfig-sys` is just a thin wrapper around the native `libeditorconfig` library.

<!-- The "safe" Rust bindings to `libeditorconfig`, built on top of this `editorconfig-sys` crate, can be found [here](https://github.com/toblux/rust-editorconfig). -->

![Workflow status](https://github.com/toblux/editorconfig-sys/actions/workflows/test.yml/badge.svg)

## Dependencies

To use this crate, `libeditorconfig >= 0.12.5` must be installed and `pkg-config` must be able to find it. You can check if `pkg-config` can find the library and which version is installed with:

```sh
pkg-config --modversion editorconfig
```

## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
editorconfig-sys = "0.1.0"
```

## Usage

Some `unsafe` Rust code examples can be found in the [tests](tests/editorconfig_sys.rs).
37 changes: 37 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use pkg_config::Library;

const LIBRARY_NAME: &str = "editorconfig";

// Technically libeditorconfig v0.12.2 already supports pkg-config:
// https://github.com/editorconfig/editorconfig-core-c/releases/tag/v0.12.2
const MIN_VERSION: &str = "0.12.5";
const MAX_VERSION: &str = "1.0.0";

fn main() {
let err_msg = format!("Unable to find library {} >= {}", LIBRARY_NAME, MIN_VERSION);
let lib = pkg_config::Config::new()
.range_version(MIN_VERSION..MAX_VERSION)
.probe(LIBRARY_NAME)
.expect(&err_msg);
gen_bindings(lib);
}

fn gen_bindings(lib: Library) {
let include_paths = lib
.include_paths
.iter()
.map(|path| format!("-I{}", path.to_string_lossy()));

let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_args(include_paths)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Failed to generate bindings");

// Write bindings to `$OUT_DIR/bindings.rs`
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Failed to write bindings");
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![allow(non_camel_case_types)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
9 changes: 9 additions & 0 deletions tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf

[*.rs]
insert_final_newline = true
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions tests/.editorconfig.invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
root = true
# Comments are fine
LINE 3 IS INVALID AND SHOULD CAUSE AN ERROR
Loading

0 comments on commit c2ad76f

Please sign in to comment.