-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl_macros.scala
77 lines (60 loc) · 1.75 KB
/
curl_macros.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package smithy4s_curl
import curl.all.{curl_easy_strerror, curl_url_strerror}
import curl.enumerations.{CURLUcode, CURLcode}
import scala.quoted.*
import scala.util.{Failure, Success, Try}
import scalanative.unsafe.fromCString
case class CurlClientStateException(msg: String) extends Exception
case class CurlException(code: CURLcode, msg: String)
extends Exception(
s"Curl error: ${CURLcode.getName(code).getOrElse("")}($code): $msg"
)
inline def checkTry(inline expr: => CURLcode): Try[CURLcode] = ${
checkTryImpl('expr)
}
private def checkTryImpl(expr: Expr[CURLcode])(using
Quotes
): Expr[Try[CURLcode]] =
'{
val code = $expr
if code != CURLcode.CURLE_OK then
Failure(
CurlException(
code,
fromCString(curl_easy_strerror(code))
)
)
else Success(code)
end if
}
end checkTryImpl
inline def check(inline expr: => CURLcode): CURLcode = ${ checkImpl('expr) }
private def checkImpl(expr: Expr[CURLcode])(using Quotes): Expr[CURLcode] =
'{
val code = $expr
if code != CURLcode.CURLE_OK then
throw new CurlException(
code,
fromCString(curl_easy_strerror(code))
)
end if
code
}
end checkImpl
case class CurlUrlParseException(code: CURLUcode, msg: String)
extends Exception(
s"Curl URL parsing error: ${CURLUcode.getName(code).getOrElse("")}($code): $msg"
)
inline def checkU(inline expr: => CURLUcode): CURLUcode = ${ checkUImpl('expr) }
private def checkUImpl(expr: Expr[CURLUcode])(using Quotes): Expr[CURLUcode] =
'{
val code = $expr
if code != CURLUcode.CURLUE_OK then
throw new CurlUrlParseException(
code,
fromCString(curl_url_strerror(code))
)
end if
code
}
end checkUImpl