An example of a C interface for the Harper grammar checking library, built in Rust.
This project is intended to help illustrate how to:
- Make a C interface for a Rust library.
- Use the Harper library.
-
Build the Rust library:
cargo build --release
-
Compile your C code:
cc your_program.c -L target/release -lharper_c -o your_program
-
From a Bash, Zsh, etc. shell:
./florb "Plz chek for a error ." Using Harper Core version: 0.67.0 4 lints: Lint 0: 'a' : Incorrect indefinite article. (suggestions: 1) Suggestion 0: Replace with: "an" Lint 1: ' ' : Unnecessary space at the end of the sentence. (suggestions: 1) Suggestion 0: Remove error Lint 2: 'Plz' : Did you mean to spell `Plz` this way? (suggestions: 3) Suggestion 0: Replace with: "Pl" Suggestion 1: Replace with: "P's" Suggestion 2: Replace with: "Pb" Lint 3: 'chek' : Did you mean to spell `chek` this way? (suggestions: 3) Suggestion 0: Replace with: "chew" Suggestion 1: Replace with: "check" Suggestion 2: Replace with: "cheek"
Document* harper_create_document(const char* text)- Create document from textvoid harper_free_document(Document* doc)- Free a documentchar* harper_get_document_text(const Document* doc)- Get document text (free withfree())int32_t harper_get_token_count(const Document* doc)- Get token countchar* harper_get_token_text(const Document* doc, int32_t index)- Get token text (free withfree())
LintGroup* harper_create_lint_group(void)- Create a lint groupvoid harper_free_lint_group(LintGroup* group)- Free a lint groupLint** harper_get_lints(const Document* doc, LintGroup* group, int32_t* count)- Get lintsvoid harper_free_lints(Lint** lints, int32_t count)- Free lints array
char* harper_get_lint_message(const Lint* lint)- Get lint message (free withfree())int32_t harper_get_lint_start(const Lint* lint)- Get start positionint32_t harper_get_lint_end(const Lint* lint)- Get end positionint32_t harper_get_suggestion_count(const Lint* lint)- Get number of suggestionschar* harper_get_suggestion_text(const Lint* lint, int32_t index)- Get suggestion text (free withfree())
char* harper_get_version(void)- Get Harper Core version (free withfree())
#include <stdio.h>
#include <stdlib.h>
#include "harper.h"
int main() {
char* version = harper_get_version();
printf("Using Harper Core version: %s\n", version);
free(version);
Document* doc = harper_create_document("Example text");
// ... use the document ...
harper_free_document(doc);
return 0;
}- All functions that return
char*must be freed withfree() - Check for
NULLreturn values to detect errors - Free all allocated resources to prevent memory leaks
The example (src/florb.c) demonstrates basic usage of the Harper library:
- Creating a document from text
- Getting the document's text content
- Getting token information
- Proper memory management
The C API is defined in src/harper.h:
-
harper_create_document(text): Create a new document from text -
harper_free_document(doc): Free a document -
harper_get_document_text(doc): Get the full text of a document -
harper_get_token_count(doc): Get the number of tokens in a document -
harper_get_token_text(doc, index): Get the text of a specific token -
harper_create_lint_group(): Create a new lint group -
harper_free_lint_group(lint_group): Free a lint group -
harper_get_lints(doc, lint_group, count): Get all lints for a document -
harper_free_lints(lints, count): Free an array of lints -
harper_get_lint_message(lint): Get the message for a lint -
harper_get_lint_start(lint): Get the start position of a lint -
harper_get_lint_end(lint): Get the end position of a lint
- Documents created with
harper_create_documentmust be freed withharper_free_document - Lint groups created with
harper_create_lint_groupmust be freed withharper_free_lint_group - Lints created with
harper_get_lintsmust be freed withharper_free_lints - Strings returned by
harper_get_document_text,harper_get_token_text, andharper_get_versionmust be freed withfree()
- Functions that return pointers return
NULLon error - Functions that return integers return
-1on error - Always check return values for errors