Skip to content

Commit

Permalink
Merge pull request #84 from thatstoasty/feature/keywords
Browse files Browse the repository at this point in the history
Switch to new keywords
  • Loading branch information
saviorand authored Jan 3, 2025
2 parents 2b3c5e7 + 1660336 commit cdaec54
Show file tree
Hide file tree
Showing 24 changed files with 153 additions and 153 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Once you have a Mojo project set up locally,
```mojo
trait HTTPService:
fn func(inout self, req: HTTPRequest) raises -> HTTPResponse:
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
...
```

Expand All @@ -101,7 +101,7 @@ Once you have a Mojo project set up locally,
@value
struct Printer(HTTPService):
fn func(inout self, req: HTTPRequest) raises -> HTTPResponse:
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
var uri = req.uri
print("Request URI: ", to_string(uri.request_uri))
Expand Down Expand Up @@ -143,7 +143,7 @@ from lightbug_http import *
@value
struct ExampleRouter(HTTPService):
fn func(inout self, req: HTTPRequest) raises -> HTTPResponse:
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
var body = req.body_raw
var uri = req.uri
Expand Down Expand Up @@ -173,7 +173,7 @@ from lightbug_http import *
@value
struct Welcome(HTTPService):
fn func(inout self, req: HTTPRequest) raises -> HTTPResponse:
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
var uri = req.uri
if uri.path == "/":
Expand All @@ -199,7 +199,7 @@ Create a file, e.g `client.mojo` with the following code. Run `magic run mojo cl
from lightbug_http import *
from lightbug_http.client import Client
fn test_request(inout client: Client) raises -> None:
fn test_request(mut client: Client) raises -> None:
var uri = URI.parse_raises("http://httpbin.org/status/404")
var headers = Header("Host", "httpbin.org")
Expand Down
12 changes: 6 additions & 6 deletions bench.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var headers_struct = Headers(


@parameter
fn lightbug_benchmark_response_encode(inout b: Bencher):
fn lightbug_benchmark_response_encode(mut b: Bencher):
@always_inline
@parameter
fn response_encode():
Expand All @@ -73,7 +73,7 @@ fn lightbug_benchmark_response_encode(inout b: Bencher):


@parameter
fn lightbug_benchmark_response_parse(inout b: Bencher):
fn lightbug_benchmark_response_parse(mut b: Bencher):
@always_inline
@parameter
fn response_parse():
Expand All @@ -87,7 +87,7 @@ fn lightbug_benchmark_response_parse(inout b: Bencher):


@parameter
fn lightbug_benchmark_request_parse(inout b: Bencher):
fn lightbug_benchmark_request_parse(mut b: Bencher):
@always_inline
@parameter
fn request_parse():
Expand All @@ -101,7 +101,7 @@ fn lightbug_benchmark_request_parse(inout b: Bencher):


@parameter
fn lightbug_benchmark_request_encode(inout b: Bencher):
fn lightbug_benchmark_request_encode(mut b: Bencher):
@always_inline
@parameter
fn request_encode():
Expand All @@ -116,7 +116,7 @@ fn lightbug_benchmark_request_encode(inout b: Bencher):


@parameter
fn lightbug_benchmark_header_encode(inout b: Bencher):
fn lightbug_benchmark_header_encode(mut b: Bencher):
@always_inline
@parameter
fn header_encode():
Expand All @@ -128,7 +128,7 @@ fn lightbug_benchmark_header_encode(inout b: Bencher):


@parameter
fn lightbug_benchmark_header_parse(inout b: Bencher):
fn lightbug_benchmark_header_parse(mut b: Bencher):
@always_inline
@parameter
fn header_parse():
Expand Down
2 changes: 1 addition & 1 deletion client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from lightbug_http import *
from lightbug_http.client import Client


fn test_request(inout client: Client) raises -> None:
fn test_request(mut client: Client) raises -> None:
var uri = URI.parse_raises("google.com")
var headers = Headers(Header("Host", "google.com"))
var request = HTTPRequest(uri, headers)
Expand Down
10 changes: 5 additions & 5 deletions integration_test_client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@ fn u(s: String) raises -> URI:
struct IntegrationTest:
var client: Client

fn __init__(inout self):
fn __init__(out self):
self.client = Client()

fn test_redirect(inout self) raises:
fn test_redirect(mut self) raises:
print("Testing redirect...")
var h = Headers(Header(HeaderKey.CONNECTION, 'keep-alive'))
var res = self.client.do(HTTPRequest(u("redirect"), headers=h))
assert_equal(res.status_code, StatusCode.OK)
assert_equal(to_string(res.body_raw), "yay you made it")
assert_equal(res.headers[HeaderKey.CONNECTION], "keep-alive")

fn test_close_connection(inout self) raises:
fn test_close_connection(mut self) raises:
print("Testing close connection...")
var h = Headers(Header(HeaderKey.CONNECTION, 'close'))
var res = self.client.do(HTTPRequest(u("close-connection"), headers=h))
assert_equal(res.status_code, StatusCode.OK)
assert_equal(to_string(res.body_raw), "connection closed")
assert_equal(res.headers[HeaderKey.CONNECTION], "close")

fn test_server_error(inout self) raises:
fn test_server_error(mut self) raises:
print("Testing internal server error...")
var res = self.client.do(HTTPRequest(u("error")))
assert_equal(res.status_code, StatusCode.INTERNAL_ERROR)
assert_equal(res.status_text, "Internal Server Error")


fn run_tests(inout self) raises:
fn run_tests(mut self) raises:
self.test_redirect()
self.test_close_connection()
self.test_server_error()
Expand Down
2 changes: 1 addition & 1 deletion integration_test_server.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from lightbug_http import *

@value
struct IntegerationTestService(HTTPService):
fn func(inout self, req: HTTPRequest) raises -> HTTPResponse:
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
var p = req.uri.path
if p == "/redirect":
return HTTPResponse(
Expand Down
2 changes: 1 addition & 1 deletion lightbug_http/__init__.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ from lightbug_http.strings import to_string


trait DefaultConstructible:
fn __init__(inout self) raises:
fn __init__(out self) raises:
...
10 changes: 5 additions & 5 deletions lightbug_http/client.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ struct Client:

var _connections: Dict[String, SysConnection]

fn __init__(inout self):
fn __init__(out self):
self.host = "127.0.0.1"
self.port = 8888
self.name = "lightbug_http_client"
self._connections = Dict[String, SysConnection]()

fn __init__(inout self, host: StringLiteral, port: Int):
fn __init__(out self, host: StringLiteral, port: Int):
self.host = host
self.port = port
self.name = "lightbug_http_client"
Expand All @@ -45,7 +45,7 @@ struct Client:
# TODO: Add an optional debug log entry here
pass

fn do(inout self, owned req: HTTPRequest) raises -> HTTPResponse:
fn do(mut self, owned req: HTTPRequest) raises -> HTTPResponse:
"""
The `do` method is responsible for sending an HTTP request to a server and receiving the corresponding response.
Expand Down Expand Up @@ -137,7 +137,7 @@ struct Client:
return HTTPResponse(Bytes())

fn _handle_redirect(
inout self, owned original_req: HTTPRequest, owned original_response: HTTPResponse
mut self, owned original_req: HTTPRequest, owned original_response: HTTPResponse
) raises -> HTTPResponse:
var new_uri: URI
var new_location = original_response.headers[HeaderKey.LOCATION]
Expand All @@ -150,7 +150,7 @@ struct Client:
original_req.uri = new_uri
return self.do(original_req^)

fn _close_conn(inout self, host: String) raises:
fn _close_conn(mut self, host: String) raises:
if host in self._connections:
self._connections[host].close()
_ = self._connections.pop(host)
8 changes: 4 additions & 4 deletions lightbug_http/cookie/cookie.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct Cookie(CollectionElement):
return cookie

fn __init__(
inout self,
mut self,
name: String,
value: String,
expires: Expiration = Expiration.session(),
Expand All @@ -88,7 +88,7 @@ struct Cookie(CollectionElement):
fn __str__(self) -> String:
return "Name: " + self.name + " Value: " + self.value

fn __copyinit__(inout self: Cookie, existing: Cookie):
fn __copyinit__(out self: Cookie, existing: Cookie):
self.name = existing.name
self.value = existing.value
self.max_age = existing.max_age
Expand All @@ -100,7 +100,7 @@ struct Cookie(CollectionElement):
self.same_site = existing.same_site
self.partitioned = existing.partitioned

fn __moveinit__(inout self: Cookie, owned existing: Cookie):
fn __moveinit__(out self: Cookie, owned existing: Cookie):
self.name = existing.name
self.value = existing.value
self.max_age = existing.max_age
Expand All @@ -112,7 +112,7 @@ struct Cookie(CollectionElement):
self.same_site = existing.same_site
self.partitioned = existing.partitioned

fn clear_cookie(inout self):
fn clear_cookie(mut self):
self.max_age = Optional[Duration](None)
self.expires = Expiration.invalidate()

Expand Down
2 changes: 1 addition & 1 deletion lightbug_http/cookie/duration.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
struct Duration:
var total_seconds: Int

fn __init__(inout self, seconds: Int = 0, minutes: Int = 0, hours: Int = 0, days: Int = 0):
fn __init__(out self, seconds: Int = 0, minutes: Int = 0, hours: Int = 0, days: Int = 0):
self.total_seconds = seconds
self.total_seconds += minutes * 60
self.total_seconds += hours * 60 * 60
Expand Down
10 changes: 5 additions & 5 deletions lightbug_http/cookie/request_cookie_jar.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ from lightbug_http.utils import ByteReader, ByteWriter, is_newline, is_space
struct RequestCookieJar(Writable, Stringable):
var _inner: Dict[String, String]

fn __init__(inout self):
fn __init__(out self):
self._inner = Dict[String, String]()

fn __init__(inout self, *cookies: Cookie):
fn __init__(out self, *cookies: Cookie):
self._inner = Dict[String, String]()
for cookie in cookies:
self._inner[cookie[].name] = cookie[].value

fn parse_cookies(inout self, headers: Headers) raises:
fn parse_cookies(mut self, headers: Headers) raises:
var cookie_header = headers[HeaderKey.COOKIE]
if not cookie_header:
return None
Expand Down Expand Up @@ -66,12 +66,12 @@ struct RequestCookieJar(Writable, Stringable):
header_value.append(cookie[].key + equal + cookie[].value)
return Header(HeaderKey.COOKIE, "; ".join(header_value))

fn encode_to(inout self, inout writer: ByteWriter):
fn encode_to(mut self, mut writer: ByteWriter):
var header = self.to_header()
if header:
write_header(writer, header.value().key, header.value().value)

fn write_to[T: Writer](self, inout writer: T):
fn write_to[T: Writer](self, mut writer: T):
var header = self.to_header()
if header:
write_header(writer, header.value().key, header.value().value)
Expand Down
20 changes: 10 additions & 10 deletions lightbug_http/cookie/response_cookie_jar.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct ResponseCookieKey(KeyElement):
var path: String

fn __init__(
inout self,
mut self,
name: String,
domain: Optional[String] = Optional[String](None),
path: Optional[String] = Optional[String](None),
Expand All @@ -26,12 +26,12 @@ struct ResponseCookieKey(KeyElement):
fn __eq__(self: Self, other: Self) -> Bool:
return self.name == other.name and self.domain == other.domain and self.path == other.path

fn __moveinit__(inout self: Self, owned existing: Self):
fn __moveinit__(out self: Self, owned existing: Self):
self.name = existing.name
self.domain = existing.domain
self.path = existing.path

fn __copyinit__(inout self: Self, existing: Self):
fn __copyinit__(out self: Self, existing: Self):
self.name = existing.name
self.domain = existing.domain
self.path = existing.path
Expand All @@ -44,16 +44,16 @@ struct ResponseCookieKey(KeyElement):
struct ResponseCookieJar(Writable, Stringable):
var _inner: Dict[ResponseCookieKey, Cookie]

fn __init__(inout self):
fn __init__(out self):
self._inner = Dict[ResponseCookieKey, Cookie]()

fn __init__(inout self, *cookies: Cookie):
fn __init__(out self, *cookies: Cookie):
self._inner = Dict[ResponseCookieKey, Cookie]()
for cookie in cookies:
self.set_cookie(cookie[])

@always_inline
fn __setitem__(inout self, key: ResponseCookieKey, value: Cookie):
fn __setitem__(mut self, key: ResponseCookieKey, value: Cookie):
self._inner[key] = value

fn __getitem__(self, key: ResponseCookieKey) raises -> Cookie:
Expand All @@ -80,26 +80,26 @@ struct ResponseCookieJar(Writable, Stringable):
return len(self._inner)

@always_inline
fn set_cookie(inout self, cookie: Cookie):
fn set_cookie(mut self, cookie: Cookie):
self[ResponseCookieKey(cookie.name, cookie.domain, cookie.path)] = cookie

@always_inline
fn empty(self) -> Bool:
return len(self) == 0

fn from_headers(inout self, headers: List[String]) raises:
fn from_headers(mut self, headers: List[String]) raises:
for header in headers:
try:
self.set_cookie(Cookie.from_set_header(header[]))
except:
raise Error("Failed to parse cookie header string " + header[])

fn encode_to(inout self, inout writer: ByteWriter):
fn encode_to(mut self, mut writer: ByteWriter):
for cookie in self._inner.values():
var v = cookie[].build_header_value()
write_header(writer, HeaderKey.SET_COOKIE, v)

fn write_to[T: Writer](self, inout writer: T):
fn write_to[T: Writer](self, mut writer: T):
for cookie in self._inner.values():
var v = cookie[].build_header_value()
write_header(writer, HeaderKey.SET_COOKIE, v)
Loading

0 comments on commit cdaec54

Please sign in to comment.