Skip to content

Commit cbc1fc0

Browse files
committed
Use trait class for API
1 parent 3555d85 commit cbc1fc0

File tree

2 files changed

+78
-42
lines changed

2 files changed

+78
-42
lines changed

tools/src/main/scala/org/scalanative/bindgen/Bindings.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ package org.scalanative.bindgen
33
import java.io.{File, PrintWriter}
44

55
class Bindings(private val bindings: String) {
6-
def writeToFile(pathToFile: String): Unit = {
7-
val pw = new PrintWriter(new File(pathToFile))
8-
try {
9-
pw.write(bindings)
10-
} finally {
11-
pw.close()
6+
def writeToFile(file: File): Unit = {
7+
new PrintWriter(file) {
8+
write(bindings)
9+
close()
1210
}
1311
}
1412
}
Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,111 @@
11
package org.scalanative.bindgen
22

3+
import java.io.File
4+
35
import scala.collection.mutable
46

5-
class Builder {
6-
private var library: String = _
7-
private var pathToHeader: String = _
8-
private var packageName: String = _
9-
private var excludePrefix: String = _
10-
private var extraArg: mutable.Seq[String] = mutable.Seq()
11-
private var extraArgBefore: mutable.Seq[String] = mutable.Seq()
7+
sealed trait Builder {
128

139
/**
1410
* Set header file for which bindings will be generated
1511
*/
16-
def header(pathToHeader: String): Unit = {
17-
this.pathToHeader = pathToHeader
18-
}
12+
def header(header: File): Unit
1913

2014
/**
2115
* Library to link with, e.g. -luv for libuv
2216
*/
23-
def link(library: String): Unit = {
24-
this.library = library
25-
}
17+
def link(library: String): Unit
2618

2719
/**
2820
* Package name of generated Scala file
2921
*/
30-
def packageName(packageName: String): Unit = {
31-
this.packageName = packageName
32-
}
22+
def packageName(packageName: String): Unit
3323

3424
/**
3525
* Declarations will be removed if their names
3626
* contain given prefix
3727
*/
38-
def excludePrefix(prefix: String): Unit = {
39-
if (!prefix.isEmpty) {
40-
excludePrefix = prefix
41-
}
42-
}
28+
def excludePrefix(prefix: String): Unit
4329

4430
/**
4531
* Additional argument to append to the compiler command line
4632
*/
47-
def extraArg(arg: String): Unit = {
48-
if (!arg.isEmpty) {
49-
extraArg += arg
50-
}
51-
}
33+
def extraArg(arg: String): Unit
5234

5335
/**
5436
* Additional argument to append to the compiler command line
5537
*/
56-
def extraArgBefore(arg: String): Unit = {
57-
if (!arg.isEmpty) {
58-
extraArgBefore += arg
59-
}
60-
}
38+
def extraArgBefore(arg: String): Unit
6139

6240
/**
6341
* Run binding generator
6442
*/
65-
def generate(): Bindings = {
66-
// TODO: generate bindings
67-
new Bindings("")
68-
}
43+
def generate(): Bindings
6944
}
7045

7146
object Builder {
72-
def apply(): Builder = new Builder()
47+
def apply(): Builder = Impl()
48+
49+
private final case class Impl() extends Builder {
50+
private var library: String = _
51+
private var header: File = _
52+
private var packageName: String = _
53+
private var excludePrefix: String = _
54+
private var extraArg: mutable.Seq[String] = mutable.Seq()
55+
private var extraArgBefore: mutable.Seq[String] = mutable.Seq()
56+
57+
/**
58+
* Set header file for which bindings will be generated
59+
*/
60+
def header(header: File): Unit = {
61+
this.header = header
62+
}
63+
64+
/**
65+
* Library to link with, e.g. -luv for libuv
66+
*/
67+
def link(library: String): Unit = {
68+
this.library = library
69+
}
70+
71+
/**
72+
* Package name of generated Scala file
73+
*/
74+
def packageName(packageName: String): Unit = {
75+
this.packageName = packageName
76+
}
77+
78+
/**
79+
* Declarations will be removed if their names
80+
* contain given prefix
81+
*/
82+
def excludePrefix(prefix: String): Unit = {
83+
require(!prefix.isEmpty)
84+
excludePrefix = prefix
85+
}
86+
87+
/**
88+
* Additional argument to append to the compiler command line
89+
*/
90+
def extraArg(arg: String): Unit = {
91+
require(!arg.isEmpty)
92+
extraArg :+ arg
93+
}
94+
95+
/**
96+
* Additional argument to append to the compiler command line
97+
*/
98+
def extraArgBefore(arg: String): Unit = {
99+
require(!arg.isEmpty)
100+
extraArgBefore :+ arg
101+
}
102+
103+
/**
104+
* Run binding generator
105+
*/
106+
def generate(): Bindings = {
107+
// TODO: generate bindings
108+
new Bindings("")
109+
}
110+
}
73111
}

0 commit comments

Comments
 (0)