Skip to content

Commit 57a1e37

Browse files
authored
C bindings (parcel-bundler#256)
1 parent e6e8058 commit 57a1e37

File tree

10 files changed

+1041
-94
lines changed

10 files changed

+1041
-94
lines changed

Cargo.lock

Lines changed: 63 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[workspace]
22
members = [
33
"node",
4-
"selectors"
4+
"selectors",
5+
"c"
56
]
67

78
[package]
@@ -55,6 +56,7 @@ serde_json = "1"
5556

5657
[features]
5758
default = ["grid"]
59+
browserslist = ["browserslist-rs"]
5860
cli = ["clap", "serde_json", "pathdiff", "browserslist-rs", "jemallocator"]
5961
grid = []
6062
serde = ["smallvec/serde", "cssparser/serde"]

c/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
authors = ["Devon Govett <devongovett@gmail.com>"]
3+
name = "parcel_css_c_bindings"
4+
version = "0.1.0"
5+
edition = "2021"
6+
publish = false
7+
8+
[lib]
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
parcel_css = { path = "../", features = ["browserslist"] }
13+
parcel_sourcemap = { version = "2.1.0", features = ["json"] }
14+
browserslist-rs = { version = "0.7.0" }
15+
16+
[build-dependencies]
17+
cbindgen = "0.24.3"

c/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extern crate cbindgen;
2+
3+
use std::env;
4+
5+
fn main() {
6+
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
7+
8+
cbindgen::generate(crate_dir)
9+
.expect("Unable to generate bindings")
10+
.write_to_file("parcel_css.h");
11+
}

c/cbindgen.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language = "C"
2+
3+
[parse]
4+
parse_deps = true
5+
include = ["parcel_css"]
6+
7+
[export.rename]
8+
StyleSheetWrapper = "StyleSheet"
9+
10+
[enum]
11+
prefix_with_name = true

c/parcel_css.h

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include <stdarg.h>
2+
#include <stdbool.h>
3+
#include <stdint.h>
4+
#include <stdlib.h>
5+
6+
typedef struct CssError CssError;
7+
8+
typedef struct StyleSheet StyleSheet;
9+
10+
typedef struct Targets {
11+
uint32_t android;
12+
uint32_t chrome;
13+
uint32_t edge;
14+
uint32_t firefox;
15+
uint32_t ie;
16+
uint32_t ios_saf;
17+
uint32_t opera;
18+
uint32_t safari;
19+
uint32_t samsung;
20+
} Targets;
21+
22+
typedef struct ParseOptions {
23+
const char *filename;
24+
bool nesting;
25+
bool custom_media;
26+
bool css_modules;
27+
const char *css_modules_pattern;
28+
bool css_modules_dashed_idents;
29+
bool error_recovery;
30+
} ParseOptions;
31+
32+
typedef struct TransformOptions {
33+
struct Targets targets;
34+
char **unused_symbols;
35+
uintptr_t unused_symbols_len;
36+
} TransformOptions;
37+
38+
typedef struct RawString {
39+
char *text;
40+
uintptr_t len;
41+
} RawString;
42+
43+
typedef enum CssModuleReference_Tag {
44+
/**
45+
* A local reference.
46+
*/
47+
CssModuleReference_Local,
48+
/**
49+
* A global reference.
50+
*/
51+
CssModuleReference_Global,
52+
/**
53+
* A reference to an export in a different file.
54+
*/
55+
CssModuleReference_Dependency,
56+
} CssModuleReference_Tag;
57+
58+
typedef struct CssModuleReference_Local_Body {
59+
/**
60+
* The local (compiled) name for the reference.
61+
*/
62+
struct RawString name;
63+
} CssModuleReference_Local_Body;
64+
65+
typedef struct CssModuleReference_Global_Body {
66+
/**
67+
* The referenced global name.
68+
*/
69+
struct RawString name;
70+
} CssModuleReference_Global_Body;
71+
72+
typedef struct CssModuleReference_Dependency_Body {
73+
/**
74+
* The name to reference within the dependency.
75+
*/
76+
struct RawString name;
77+
/**
78+
* The dependency specifier for the referenced file.
79+
*/
80+
struct RawString specifier;
81+
} CssModuleReference_Dependency_Body;
82+
83+
typedef struct CssModuleReference {
84+
CssModuleReference_Tag tag;
85+
union {
86+
CssModuleReference_Local_Body local;
87+
CssModuleReference_Global_Body global;
88+
CssModuleReference_Dependency_Body dependency;
89+
};
90+
} CssModuleReference;
91+
92+
typedef struct CssModuleExport {
93+
struct RawString exported;
94+
struct RawString local;
95+
bool is_referenced;
96+
struct CssModuleReference *composes;
97+
uintptr_t composes_len;
98+
} CssModuleExport;
99+
100+
typedef struct CssModulePlaceholder {
101+
struct RawString placeholder;
102+
struct CssModuleReference reference;
103+
} CssModulePlaceholder;
104+
105+
typedef struct ToCssResult {
106+
struct RawString code;
107+
struct RawString map;
108+
struct CssModuleExport *exports;
109+
uintptr_t exports_len;
110+
struct CssModulePlaceholder *references;
111+
uintptr_t references_len;
112+
} ToCssResult;
113+
114+
typedef struct PseudoClasses {
115+
const char *hover;
116+
const char *active;
117+
const char *focus;
118+
const char *focus_visible;
119+
const char *focus_within;
120+
} PseudoClasses;
121+
122+
typedef struct ToCssOptions {
123+
bool minify;
124+
bool source_map;
125+
const char *input_source_map;
126+
uintptr_t input_source_map_len;
127+
struct Targets targets;
128+
bool analyze_dependencies;
129+
struct PseudoClasses pseudo_classes;
130+
} ToCssOptions;
131+
132+
bool parcel_css_browserslist_to_targets(const char *query,
133+
struct Targets *targets,
134+
struct CssError **error);
135+
136+
struct StyleSheet *parcel_css_stylesheet_parse(const char *source,
137+
uintptr_t len,
138+
struct ParseOptions options,
139+
struct CssError **error);
140+
141+
bool parcel_css_stylesheet_transform(struct StyleSheet *stylesheet,
142+
struct TransformOptions options,
143+
struct CssError **error);
144+
145+
struct ToCssResult parcel_css_stylesheet_to_css(struct StyleSheet *stylesheet,
146+
struct ToCssOptions options,
147+
struct CssError **error);
148+
149+
void parcel_css_stylesheet_free(struct StyleSheet *stylesheet);
150+
151+
void parcel_css_to_css_result_free(struct ToCssResult result);
152+
153+
const char *parcel_css_error_message(struct CssError *error);
154+
155+
void parcel_css_error_free(struct CssError *error);
156+
157+
uintptr_t parcel_css_stylesheet_get_warning_count(struct StyleSheet *stylesheet);
158+
159+
const char *parcel_css_stylesheet_get_warning(struct StyleSheet *stylesheet, uintptr_t index);

0 commit comments

Comments
 (0)