Skip to content

Commit

Permalink
C bindings (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Aug 21, 2022
1 parent e6e8058 commit 57a1e37
Show file tree
Hide file tree
Showing 10 changed files with 1,041 additions and 94 deletions.
82 changes: 63 additions & 19 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[workspace]
members = [
"node",
"selectors"
"selectors",
"c"
]

[package]
Expand Down Expand Up @@ -55,6 +56,7 @@ serde_json = "1"

[features]
default = ["grid"]
browserslist = ["browserslist-rs"]
cli = ["clap", "serde_json", "pathdiff", "browserslist-rs", "jemallocator"]
grid = []
serde = ["smallvec/serde", "cssparser/serde"]
Expand Down
17 changes: 17 additions & 0 deletions c/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
authors = ["Devon Govett <devongovett@gmail.com>"]
name = "parcel_css_c_bindings"
version = "0.1.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
parcel_css = { path = "../", features = ["browserslist"] }
parcel_sourcemap = { version = "2.1.0", features = ["json"] }
browserslist-rs = { version = "0.7.0" }

[build-dependencies]
cbindgen = "0.24.3"
11 changes: 11 additions & 0 deletions c/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern crate cbindgen;

use std::env;

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

cbindgen::generate(crate_dir)
.expect("Unable to generate bindings")
.write_to_file("parcel_css.h");
}
11 changes: 11 additions & 0 deletions c/cbindgen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language = "C"

[parse]
parse_deps = true
include = ["parcel_css"]

[export.rename]
StyleSheetWrapper = "StyleSheet"

[enum]
prefix_with_name = true
159 changes: 159 additions & 0 deletions c/parcel_css.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct CssError CssError;

typedef struct StyleSheet StyleSheet;

typedef struct Targets {
uint32_t android;
uint32_t chrome;
uint32_t edge;
uint32_t firefox;
uint32_t ie;
uint32_t ios_saf;
uint32_t opera;
uint32_t safari;
uint32_t samsung;
} Targets;

typedef struct ParseOptions {
const char *filename;
bool nesting;
bool custom_media;
bool css_modules;
const char *css_modules_pattern;
bool css_modules_dashed_idents;
bool error_recovery;
} ParseOptions;

typedef struct TransformOptions {
struct Targets targets;
char **unused_symbols;
uintptr_t unused_symbols_len;
} TransformOptions;

typedef struct RawString {
char *text;
uintptr_t len;
} RawString;

typedef enum CssModuleReference_Tag {
/**
* A local reference.
*/
CssModuleReference_Local,
/**
* A global reference.
*/
CssModuleReference_Global,
/**
* A reference to an export in a different file.
*/
CssModuleReference_Dependency,
} CssModuleReference_Tag;

typedef struct CssModuleReference_Local_Body {
/**
* The local (compiled) name for the reference.
*/
struct RawString name;
} CssModuleReference_Local_Body;

typedef struct CssModuleReference_Global_Body {
/**
* The referenced global name.
*/
struct RawString name;
} CssModuleReference_Global_Body;

typedef struct CssModuleReference_Dependency_Body {
/**
* The name to reference within the dependency.
*/
struct RawString name;
/**
* The dependency specifier for the referenced file.
*/
struct RawString specifier;
} CssModuleReference_Dependency_Body;

typedef struct CssModuleReference {
CssModuleReference_Tag tag;
union {
CssModuleReference_Local_Body local;
CssModuleReference_Global_Body global;
CssModuleReference_Dependency_Body dependency;
};
} CssModuleReference;

typedef struct CssModuleExport {
struct RawString exported;
struct RawString local;
bool is_referenced;
struct CssModuleReference *composes;
uintptr_t composes_len;
} CssModuleExport;

typedef struct CssModulePlaceholder {
struct RawString placeholder;
struct CssModuleReference reference;
} CssModulePlaceholder;

typedef struct ToCssResult {
struct RawString code;
struct RawString map;
struct CssModuleExport *exports;
uintptr_t exports_len;
struct CssModulePlaceholder *references;
uintptr_t references_len;
} ToCssResult;

typedef struct PseudoClasses {
const char *hover;
const char *active;
const char *focus;
const char *focus_visible;
const char *focus_within;
} PseudoClasses;

typedef struct ToCssOptions {
bool minify;
bool source_map;
const char *input_source_map;
uintptr_t input_source_map_len;
struct Targets targets;
bool analyze_dependencies;
struct PseudoClasses pseudo_classes;
} ToCssOptions;

bool parcel_css_browserslist_to_targets(const char *query,
struct Targets *targets,
struct CssError **error);

struct StyleSheet *parcel_css_stylesheet_parse(const char *source,
uintptr_t len,
struct ParseOptions options,
struct CssError **error);

bool parcel_css_stylesheet_transform(struct StyleSheet *stylesheet,
struct TransformOptions options,
struct CssError **error);

struct ToCssResult parcel_css_stylesheet_to_css(struct StyleSheet *stylesheet,
struct ToCssOptions options,
struct CssError **error);

void parcel_css_stylesheet_free(struct StyleSheet *stylesheet);

void parcel_css_to_css_result_free(struct ToCssResult result);

const char *parcel_css_error_message(struct CssError *error);

void parcel_css_error_free(struct CssError *error);

uintptr_t parcel_css_stylesheet_get_warning_count(struct StyleSheet *stylesheet);

const char *parcel_css_stylesheet_get_warning(struct StyleSheet *stylesheet, uintptr_t index);

0 comments on commit 57a1e37

Please sign in to comment.