| @@ -34,23 +34,23 @@ pub type Error { |
| 34 | 34 | Io(String) |
| 35 | 35 | /// The server refused, with its fields (the `"C"` field is the |
| 36 | 36 | /// SQLSTATE, `"M"` the message). |
| 37 | | − Server(List<(String, String)>) |
| 37 | +, Server(List<(String, String)>) |
| 38 | 38 | /// The driver itself: the protocol was violated, or authentication |
| 39 | 39 | /// could not run. |
| 40 | | − Driver(String) |
| 40 | +, Driver(String) |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /// A parameter's value, sent in text format; SQL `NULL` is `Null`. |
| 44 | 44 | @derive(Eq, Debug) |
| 45 | 45 | pub type Value { |
| 46 | 46 | Null |
| 47 | | − Integer(Int) |
| 48 | | − Real(Float) |
| 49 | | − Boolean(Bool) |
| 50 | | − Text(String) |
| 51 | | − Bytea(Bytes) |
| 52 | | − Uuid(uuid.Uuid) |
| 53 | | − Timestamptz(time.Timestamp) |
| 47 | +, Integer(Int) |
| 48 | +, Real(Float) |
| 49 | +, Boolean(Bool) |
| 50 | +, Text(String) |
| 51 | +, Bytea(Bytes) |
| 52 | +, Uuid(uuid.Uuid) |
| 53 | +, Timestamptz(time.Timestamp) |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /// A row set's columns (the protocol's fields) and rows. |
| @@ -64,25 +64,24 @@ pub type Row { Row(columns: List<protocol.Field>, values: List<Option<Bytes>>) } |
| 64 | 64 | /// The SQLSTATE of a server error, if it has one. |
| 65 | 65 | pub fn sqlstate(e: Error) -> Option<String> { |
| 66 | 66 | match e { |
| 67 | | − Server(fields) -> field(fields, "C"), |
| 68 | | − _ -> None, |
| 67 | + Server(fields) -> field(fields, "C") |
| 68 | + , _ -> None |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /// The server's message for a server error, or the text otherwise. |
| 73 | 73 | pub fn message(e: Error) -> String { |
| 74 | 74 | match e { |
| 75 | | − Io(m) -> m, |
| 76 | | − Server(fields) -> |
| 77 | | − option.unwrap_or(field(fields, "M"), debug_fields(fields)), |
| 78 | | − Driver(m) -> m, |
| 75 | + Io(m) -> m |
| 76 | + , Server(fields) -> option.unwrap_or(field(fields, "M"), debug_fields(fields)) |
| 77 | + , Driver(m) -> m |
| 79 | 78 | } |
| 80 | 79 | } |
| 81 | 80 | |
| 82 | 81 | fn field(fields: List<(String, String)>, name: String) -> Option<String> { |
| 83 | 82 | match fields { |
| 84 | | − [] -> None, |
| 85 | | − [(n, v), ..rest] -> if n == name { Some(v) } else { field(rest, name) }, |
| 83 | + [] -> None |
| 84 | + , [(n, v), ..rest] -> if n == name { Some(v) } else { field(rest, name) } |
| 86 | 85 | } |
| 87 | 86 | } |
| 88 | 87 | |
| @@ -95,7 +94,7 @@ fn debug_fields(fields: List<(String, String)>) -> String { |
| 95 | 94 | /// The socket: plain TCP or TLS. |
| 96 | 95 | type Conn { |
| 97 | 96 | Tcp(Resource<TcpStream>) |
| 98 | | − Tls(Resource<TlsStream>) |
| 97 | +, Tls(Resource<TlsStream>) |
| 99 | 98 | } |
| 100 | 99 | |
| 101 | 100 | /// A connection's socket after the handshake: the connection, the read |
| @@ -142,8 +141,8 @@ fn state_of(socket: Socket) -> State { |
| 142 | 141 | fn maybe_tls(tcp: Resource<TcpStream>, cfg: Config) -> Result<Conn, Error> |
| 143 | 142 | / <io> { |
| 144 | 143 | match cfg.tls { |
| 145 | | − Disable -> Ok(Tcp(tcp)), |
| 146 | | − mode -> { |
| 144 | + Disable -> Ok(Tcp(tcp)) |
| 145 | + , mode -> { |
| 147 | 146 | use _ <- result.try( |
| 148 | 147 | net.write(tcp, protocol.encode(protocol.SslRequest)) |
| 149 | 148 | |> result.map_error(Io) |
| @@ -159,15 +158,15 @@ fn maybe_tls(tcp: Resource<TcpStream>, cfg: Config) -> Result<Conn, Error> |
| 159 | 158 | tls.connect(tcp, cfg.host, tls_config) |> result.map_error(tls_io) |
| 160 | 159 | ) |
| 161 | 160 | Ok(Tls(stream)) |
| 162 | | − }, |
| 163 | | − Some(protocol.Plain) -> match mode { |
| 161 | + } |
| 162 | + , Some(protocol.Plain) -> match mode { |
| 164 | 163 | Require -> |
| 165 | | − Error(Driver("the server does not do TLS (sslmode=require)")), |
| 166 | | − _ -> Ok(Tcp(tcp)), |
| 167 | | − }, |
| 168 | | − None -> Error(Driver("a byte that is not an SSL answer")), |
| 169 | | − }, |
| 170 | | − _ -> Error(Io("the server closed the connection")), |
| 164 | + Error(Driver("the server does not do TLS (sslmode=require)")) |
| 165 | + , _ -> Ok(Tcp(tcp)) |
| 166 | + } |
| 167 | + , None -> Error(Driver("a byte that is not an SSL answer")) |
| 168 | + } |
| 169 | + , _ -> Error(Io("the server closed the connection")) |
| 171 | 170 | } |
| 172 | 171 | } |
| 173 | 172 | } |
| @@ -203,35 +202,34 @@ fn auth_loop( |
| 203 | 202 | ) -> Result<(Bytes, Int, Option<(Int, Int)>), Error> / <io, random> { |
| 204 | 203 | use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 205 | 204 | match msg { |
| 206 | | − protocol.AuthenticationOk -> auth_loop(conn, buffer, at, cfg, backend), |
| 207 | | − protocol.AuthenticationCleartext -> { |
| 205 | + protocol.AuthenticationOk -> auth_loop(conn, buffer, at, cfg, backend) |
| 206 | + , protocol.AuthenticationCleartext -> { |
| 208 | 207 | use _ <- result.try(cleartext_password(conn, cfg)) |
| 209 | 208 | auth_loop(conn, buffer, at, cfg, backend) |
| 210 | | − }, |
| 211 | | − protocol.AuthenticationSasl(mechanisms) -> { |
| 209 | + } |
| 210 | + , protocol.AuthenticationSasl(mechanisms) -> { |
| 212 | 211 | use (buffer, at) <- result.try( |
| 213 | 212 | scram_exchange(conn, buffer, at, cfg, mechanisms) |
| 214 | 213 | ) |
| 215 | 214 | auth_loop(conn, buffer, at, cfg, backend) |
| 216 | | − }, |
| 217 | | − protocol.ParameterStatus(..) -> auth_loop(conn, buffer, at, cfg, backend), |
| 218 | | − protocol.BackendKeyData(pid, secret) -> |
| 219 | | − auth_loop(conn, buffer, at, cfg, Some((pid, secret))), |
| 220 | | − protocol.NoticeResponse(_) -> auth_loop(conn, buffer, at, cfg, backend), |
| 221 | | − protocol.ReadyForQuery(_) -> Ok((buffer, at, backend)), |
| 222 | | − protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 223 | | − _ -> Error(Driver("an unexpected message during startup")), |
| 215 | + } |
| 216 | + , protocol.ParameterStatus(..) -> auth_loop(conn, buffer, at, cfg, backend) |
| 217 | + , protocol.BackendKeyData(pid, secret) -> |
| 218 | + auth_loop(conn, buffer, at, cfg, Some((pid, secret))) |
| 219 | + , protocol.NoticeResponse(_) -> auth_loop(conn, buffer, at, cfg, backend) |
| 220 | + , protocol.ReadyForQuery(_) -> Ok((buffer, at, backend)) |
| 221 | + , protocol.ErrorResponse(fields) -> Error(Server(fields)) |
| 222 | + , _ -> Error(Driver("an unexpected message during startup")) |
| 224 | 223 | } |
| 225 | 224 | } |
| 226 | 225 | |
| 227 | 226 | /// A cleartext password, over TLS or to localhost only. |
| 228 | 227 | fn cleartext_password(conn: Conn, cfg: Config) -> Result<(), Error> / <io> { |
| 229 | 228 | let safe = match conn { |
| 230 | | − Tls(_) -> True, |
| 231 | | − Tcp( |
| 229 | + Tls(_) -> True |
| 230 | + , Tcp( |
| 232 | 231 | _ |
| 233 | | − ) -> |
| 234 | | − cfg.host == "localhost" || cfg.host == "127.0.0.1" || cfg.host == "::1", |
| 232 | + ) -> cfg.host == "localhost" || cfg.host == "127.0.0.1" || cfg.host == "::1" |
| 235 | 233 | } |
| 236 | 234 | if !safe { |
| 237 | 235 | Error( |
| @@ -295,11 +293,11 @@ fn scram_continue( |
| 295 | 293 | match msg { |
| 296 | 294 | protocol.SaslContinue(data) -> match bytes.to_string(data) { |
| 297 | 295 | Ok(text) -> |
| 298 | | − scram.client_final(state, password, text) |> result.map_error(Driver), |
| 299 | | − Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")), |
| 300 | | − }, |
| 301 | | − protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 302 | | − _ -> Error(Driver("expected a SASL continuation")), |
| 296 | + scram.client_final(state, password, text) |> result.map_error(Driver) |
| 297 | + , Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")) |
| 298 | + } |
| 299 | + , protocol.ErrorResponse(fields) -> Error(Server(fields)) |
| 300 | + , _ -> Error(Driver("expected a SASL continuation")) |
| 303 | 301 | } |
| 304 | 302 | } |
| 305 | 303 | |
| @@ -308,11 +306,11 @@ fn scram_continue( |
| 308 | 306 | fn scram_final(msg: protocol.In, state: scram.Scram) -> Result<(), Error> { |
| 309 | 307 | match msg { |
| 310 | 308 | protocol.SaslFinal(data) -> match bytes.to_string(data) { |
| 311 | | − Ok(text) -> scram.verify_server(state, text) |> result.map_error(Driver), |
| 312 | | − Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")), |
| 313 | | − }, |
| 314 | | − protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 315 | | − _ -> Error(Driver("expected a SASL final")), |
| 309 | + Ok(text) -> scram.verify_server(state, text) |> result.map_error(Driver) |
| 310 | + , Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")) |
| 311 | + } |
| 312 | + , protocol.ErrorResponse(fields) -> Error(Server(fields)) |
| 313 | + , _ -> Error(Driver("expected a SASL final")) |
| 316 | 314 | } |
| 317 | 315 | } |
| 318 | 316 | |
| @@ -352,8 +350,8 @@ pub type Msg { |
| 352 | 350 | /// cheapest round trip the protocol has. It fails when the connection |
| 353 | 351 | /// is not idle (a pooled connection has no transaction open). The actor |
| 354 | 352 | /// stops after a failed ping: the connection is not to be trusted. |
| 355 | | − Ping(reply: Subject<Result<(), Error>>) |
| 356 | | − Close(reply: Subject<()>) |
| 353 | +, Ping(reply: Subject<Result<(), Error>>) |
| 354 | +, Close(reply: Subject<()>) |
| 357 | 355 | } |
| 358 | 356 | |
| 359 | 357 | fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| @@ -362,23 +360,23 @@ fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 362 | 360 | let (res, state) = run_query(state, sql, params) |
| 363 | 361 | process.send(reply, res) |
| 364 | 362 | Continue(state) |
| 365 | | − }, |
| 366 | | − Ping(reply) -> { |
| 363 | + } |
| 364 | + , Ping(reply) -> { |
| 367 | 365 | let (res, buffer, at) = ping(state.conn, state.buffer, state.at) |
| 368 | 366 | process.send(reply, res) |
| 369 | 367 | match res { |
| 370 | | − Ok(()) -> Continue(State(..state, buffer: buffer, at: at)), |
| 371 | | − Error(_) -> { |
| 368 | + Ok(()) -> Continue(State(..state, buffer: buffer, at: at)) |
| 369 | + , Error(_) -> { |
| 372 | 370 | close_conn(state.conn) |
| 373 | 371 | Stop |
| 374 | | − }, |
| 372 | + } |
| 375 | 373 | } |
| 376 | | − }, |
| 377 | | − Close(reply) -> { |
| 374 | + } |
| 375 | + , Close(reply) -> { |
| 378 | 376 | close_conn(state.conn) |
| 379 | 377 | process.send(reply, ()) |
| 380 | 378 | Stop |
| 381 | | − }, |
| 379 | + } |
| 382 | 380 | } |
| 383 | 381 | } |
| 384 | 382 | |
| @@ -387,8 +385,8 @@ fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 387 | 385 | fn ping(conn: Conn, buffer: Bytes, at: Int) -> (Result<(), Error>, Bytes, Int) |
| 388 | 386 | / <io> { |
| 389 | 387 | match write_msg(conn, protocol.Sync) { |
| 390 | | − Error(e) -> (Error(e), bytes.empty(), 0), |
| 391 | | − Ok(()) -> ping_read(conn, buffer, at, None), |
| 388 | + Error(e) -> (Error(e), bytes.empty(), 0) |
| 389 | + , Ok(()) -> ping_read(conn, buffer, at, None) |
| 392 | 390 | } |
| 393 | 391 | } |
| 394 | 392 | |
| @@ -399,23 +397,23 @@ fn ping_read(conn: Conn, buffer: Bytes, at: Int, error: Option<Error>) -> ( |
| 399 | 397 | ) |
| 400 | 398 | / <io> { |
| 401 | 399 | match read_message(conn, buffer, at) { |
| 402 | | − Error(e) -> (Error(e), bytes.empty(), 0), |
| 403 | | − Ok((msg, buffer, at)) -> match msg { |
| 400 | + Error(e) -> (Error(e), bytes.empty(), 0) |
| 401 | + , Ok((msg, buffer, at)) -> match msg { |
| 404 | 402 | protocol.ReadyForQuery(status) -> match error { |
| 405 | | − Some(e) -> (Error(e), buffer, at), |
| 406 | | − None -> match status { |
| 407 | | − protocol.Idle -> (Ok(()), buffer, at), |
| 408 | | − _ -> ( |
| 403 | + Some(e) -> (Error(e), buffer, at) |
| 404 | + , None -> match status { |
| 405 | + protocol.Idle -> (Ok(()), buffer, at) |
| 406 | + , _ -> ( |
| 409 | 407 | Error(Driver("the connection has a transaction open")) |
| 410 | 408 | , buffer |
| 411 | 409 | , at |
| 412 | | − ), |
| 413 | | − }, |
| 414 | | − }, |
| 415 | | − protocol.ErrorResponse(fields) -> |
| 416 | | − ping_read(conn, buffer, at, Some(Server(fields))), |
| 417 | | − _ -> ping_read(conn, buffer, at, error), |
| 418 | | − }, |
| 410 | + ) |
| 411 | + } |
| 412 | + } |
| 413 | + , protocol.ErrorResponse(fields) -> |
| 414 | + ping_read(conn, buffer, at, Some(Server(fields))) |
| 415 | + , _ -> ping_read(conn, buffer, at, error) |
| 416 | + } |
| 419 | 417 | } |
| 420 | 418 | } |
| 421 | 419 | |
| @@ -428,8 +426,8 @@ fn run_query(state: State, sql: String, params: List<Value>) -> ( |
| 428 | 426 | ) |
| 429 | 427 | / <io> { |
| 430 | 428 | match prepare(state, sql) { |
| 431 | | − (Error(e), state) -> (Error(e), state), |
| 432 | | − (Ok(prep), state) -> if list.length(params) |
| 429 | + (Error(e), state) -> (Error(e), state) |
| 430 | + , (Ok(prep), state) -> if list.length(params) |
| 433 | 431 | != list.length(prep.param_oids) { |
| 434 | 432 | ( |
| 435 | 433 | Error( |
| @@ -457,8 +455,8 @@ fn run_query(state: State, sql: String, params: List<Value>) -> ( |
| 457 | 455 | state.conn |
| 458 | 456 | , bytes.concat_all(list.map(out, protocol.encode)) |
| 459 | 457 | ) { |
| 460 | | − Error(e) -> (Error(e), state), |
| 461 | | − Ok(()) -> { |
| 458 | + Error(e) -> (Error(e), state) |
| 459 | + , Ok(()) -> { |
| 462 | 460 | let (res, buffer, at) = collect( |
| 463 | 461 | state.conn |
| 464 | 462 | , state.buffer |
| @@ -469,9 +467,9 @@ fn run_query(state: State, sql: String, params: List<Value>) -> ( |
| 469 | 467 | , None |
| 470 | 468 | ) |
| 471 | 469 | (res, State(..state, buffer: buffer, at: at)) |
| 472 | | − }, |
| 470 | + } |
| 473 | 471 | } |
| 474 | | − }, |
| 472 | + } |
| 475 | 473 | } |
| 476 | 474 | } |
| 477 | 475 | |
| @@ -480,8 +478,8 @@ fn run_query(state: State, sql: String, params: List<Value>) -> ( |
| 480 | 478 | fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) |
| 481 | 479 | / <io> { |
| 482 | 480 | match dict.get(state.prepared, sql) { |
| 483 | | − Some(prep) -> (Ok(prep), state), |
| 484 | | − None -> { |
| 481 | + Some(prep) -> (Ok(prep), state) |
| 482 | + , None -> { |
| 485 | 483 | let name = "q{state.next_statement}" |
| 486 | 484 | let out = [ |
| 487 | 485 | protocol.Parse(name, sql) |
| @@ -492,8 +490,8 @@ fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) |
| 492 | 490 | state.conn |
| 493 | 491 | , bytes.concat_all(list.map(out, protocol.encode)) |
| 494 | 492 | ) { |
| 495 | | − Error(e) -> (Error(e), state), |
| 496 | | − Ok(()) -> match read_describe( |
| 493 | + Error(e) -> (Error(e), state) |
| 494 | + , Ok(()) -> match read_describe( |
| 497 | 495 | state.conn |
| 498 | 496 | , state.buffer |
| 499 | 497 | , state.at |
| @@ -504,8 +502,8 @@ fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) |
| 504 | 502 | (Error(e), buffer, at) -> ( |
| 505 | 503 | Error(e) |
| 506 | 504 | , State(..state, buffer: buffer, at: at) |
| 507 | | − ), |
| 508 | | − (Ok((oids, fields)), buffer, at) -> { |
| 505 | + ) |
| 506 | + , (Ok((oids, fields)), buffer, at) -> { |
| 509 | 507 | let prep = Prepared(name, oids, fields) |
| 510 | 508 | ( |
| 511 | 509 | Ok(prep) |
| @@ -517,10 +515,10 @@ fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) |
| 517 | 515 | , next_statement: state.next_statement + 1 |
| 518 | 516 | ) |
| 519 | 517 | ) |
| 520 | | − }, |
| 521 | | − }, |
| 518 | + } |
| 519 | + } |
| 522 | 520 | } |
| 523 | | − }, |
| 521 | + } |
| 524 | 522 | } |
| 525 | 523 | } |
| 526 | 524 | |
| @@ -535,20 +533,20 @@ fn read_describe( |
| 535 | 533 | , error: Option<Error> |
| 536 | 534 | ) -> (Result<(List<Int>, List<protocol.Field>), Error>, Bytes, Int) / <io> { |
| 537 | 535 | match read_message(conn, buffer, at) { |
| 538 | | − Error(e) -> (Error(e), bytes.empty(), 0), |
| 539 | | − Ok((msg, buffer, at)) -> match msg { |
| 536 | + Error(e) -> (Error(e), bytes.empty(), 0) |
| 537 | + , Ok((msg, buffer, at)) -> match msg { |
| 540 | 538 | protocol.ParameterDescription(oids) -> |
| 541 | | − read_describe(conn, buffer, at, oids, fields, error), |
| 542 | | − protocol.RowDescription(f) -> |
| 543 | | − read_describe(conn, buffer, at, param_oids, f, error), |
| 544 | | − protocol.NoData -> read_describe(conn, buffer, at, param_oids, [], error), |
| 545 | | − protocol.ErrorResponse(f) -> |
| 546 | | − read_describe(conn, buffer, at, param_oids, fields, Some(Server(f))), |
| 547 | | − protocol.ReadyForQuery(_) -> match error { |
| 548 | | − Some(e) -> (Error(e), buffer, at), |
| 549 | | − None -> (Ok((param_oids, fields)), buffer, at), |
| 550 | | − }, |
| 551 | | − _ -> read_describe(conn, buffer, at, param_oids, fields, error), |
| 539 | + read_describe(conn, buffer, at, oids, fields, error) |
| 540 | + , protocol.RowDescription(f) -> |
| 541 | + read_describe(conn, buffer, at, param_oids, f, error) |
| 542 | + , protocol.NoData -> read_describe(conn, buffer, at, param_oids, [], error) |
| 543 | + , protocol.ErrorResponse(f) -> |
| 544 | + read_describe(conn, buffer, at, param_oids, fields, Some(Server(f))) |
| 545 | + , protocol.ReadyForQuery(_) -> match error { |
| 546 | + Some(e) -> (Error(e), buffer, at) |
| 547 | + , None -> (Ok((param_oids, fields)), buffer, at) |
| 548 | + } |
| 549 | + , _ -> read_describe(conn, buffer, at, param_oids, fields, error) |
| 552 | 550 | } |
| 553 | 551 | } |
| 554 | 552 | } |
| @@ -563,11 +561,11 @@ fn collect( |
| 563 | 561 | , error: Option<Error> |
| 564 | 562 | ) -> (Result<QueryResult, Error>, Bytes, Int) / <io> { |
| 565 | 563 | match read_message(conn, buffer, at) { |
| 566 | | − Error(e) -> (Error(e), bytes.empty(), 0), |
| 567 | | − Ok((msg, buffer, at)) -> match msg { |
| 564 | + Error(e) -> (Error(e), bytes.empty(), 0) |
| 565 | + , Ok((msg, buffer, at)) -> match msg { |
| 568 | 566 | protocol.RowDescription(fields) -> |
| 569 | | − collect(conn, buffer, at, fields, rows, tag, error), |
| 570 | | − protocol.DataRow(values) -> |
| 567 | + collect(conn, buffer, at, fields, rows, tag, error) |
| 568 | + , protocol.DataRow(values) -> |
| 571 | 569 | collect( |
| 572 | 570 | conn |
| 573 | 571 | , buffer |
| @@ -576,16 +574,16 @@ fn collect( |
| 576 | 574 | , [Row(columns, values), ..rows] |
| 577 | 575 | , tag |
| 578 | 576 | , error |
| 579 | | − ), |
| 580 | | − protocol.CommandComplete(t) -> |
| 581 | | − collect(conn, buffer, at, columns, rows, t, error), |
| 582 | | − protocol.ErrorResponse(fields) -> |
| 583 | | − collect(conn, buffer, at, columns, rows, tag, Some(Server(fields))), |
| 584 | | − protocol.ReadyForQuery(_) -> match error { |
| 585 | | − Some(e) -> (Error(e), buffer, at), |
| 586 | | − None -> (Ok(QueryResult(columns, list.reverse(rows), tag)), buffer, at), |
| 587 | | − }, |
| 588 | | − _ -> |
| 577 | + ) |
| 578 | + , protocol.CommandComplete(t) -> |
| 579 | + collect(conn, buffer, at, columns, rows, t, error) |
| 580 | + , protocol.ErrorResponse(fields) -> |
| 581 | + collect(conn, buffer, at, columns, rows, tag, Some(Server(fields))) |
| 582 | + , protocol.ReadyForQuery(_) -> match error { |
| 583 | + Some(e) -> (Error(e), buffer, at) |
| 584 | + , None -> (Ok(QueryResult(columns, list.reverse(rows), tag)), buffer, at) |
| 585 | + } |
| 586 | + , _ -> |
| 589 | 587 | collect( |
| 590 | 588 | conn |
| 591 | 589 | , buffer |
| @@ -594,7 +592,7 @@ fn collect( |
| 594 | 592 | , rows |
| 595 | 593 | , tag |
| 596 | 594 | , error |
| 597 | | − ), // ParseComplete, BindComplete, NoData, ... |
| 595 | + ) // ParseComplete, BindComplete, NoData, ... |
| 598 | 596 | } |
| 599 | 597 | } |
| 600 | 598 | } |
| @@ -615,10 +613,10 @@ fn read_message(conn: Conn, buffer: Bytes, at: Int) -> Result< |
| 615 | 613 | Ok((msg, bytes.empty(), 0)) |
| 616 | 614 | } else { |
| 617 | 615 | Ok((msg, buffer, past)) |
| 618 | | − }, |
| 619 | | − protocol.Malformed(e) -> Error(Driver(e)), |
| 620 | | − protocol.Incomplete -> |
| 621 | | − fill(conn, bytes.slice(buffer, at, bytes.length(buffer))), |
| 616 | + } |
| 617 | + , protocol.Malformed(e) -> Error(Driver(e)) |
| 618 | + , protocol.Incomplete -> |
| 619 | + fill(conn, bytes.slice(buffer, at, bytes.length(buffer))) |
| 622 | 620 | } |
| 623 | 621 | } |
| 624 | 622 | |
| @@ -660,12 +658,12 @@ fn fill_loop(conn: Conn, b: bytes.Builder, have: Int, total: Int) -> Result< |
| 660 | 658 | if have >= total { |
| 661 | 659 | let buffer = bytes.build(b) |
| 662 | 660 | match protocol.next(buffer, 0) { |
| 663 | | − protocol.Message(msg, past) -> Ok((msg, buffer, past)), |
| 664 | | − protocol.Malformed(e) -> Error(Driver(e)), |
| 665 | | − protocol.Incomplete -> |
| 661 | + protocol.Message(msg, past) -> Ok((msg, buffer, past)) |
| 662 | + , protocol.Malformed(e) -> Error(Driver(e)) |
| 663 | + , protocol.Incomplete -> |
| 666 | 664 | Error( |
| 667 | 665 | Driver("protocol: a whole frame did not parse (a bug in the codec)") |
| 668 | | − ), |
| 666 | + ) |
| 669 | 667 | } |
| 670 | 668 | } else { |
| 671 | 669 | use chunk <- result.try(conn_read(conn, 65536)) |
| @@ -683,23 +681,23 @@ fn write_msg(conn: Conn, msg: protocol.Out) -> Result<(), Error> / <io> { |
| 683 | 681 | |
| 684 | 682 | fn conn_read(conn: Conn, max: Int) -> Result<Bytes, Error> / <io> { |
| 685 | 683 | match conn { |
| 686 | | − Tcp(c) -> net.read(c, max) |> result.map_error(Io), |
| 687 | | − Tls(c) -> tls.read(c, max) |> result.map_error(tls_io), |
| 684 | + Tcp(c) -> net.read(c, max) |> result.map_error(Io) |
| 685 | + , Tls(c) -> tls.read(c, max) |> result.map_error(tls_io) |
| 688 | 686 | } |
| 689 | 687 | } |
| 690 | 688 | |
| 691 | 689 | fn conn_write(conn: Conn, data: Bytes) -> Result<(), Error> / <io> { |
| 692 | 690 | match conn { |
| 693 | | − Tcp(c) -> net.write(c, data) |> result.map_error(Io), |
| 694 | | − Tls(c) -> tls.write(c, data) |> result.map_error(tls_io), |
| 691 | + Tcp(c) -> net.write(c, data) |> result.map_error(Io) |
| 692 | + , Tls(c) -> tls.write(c, data) |> result.map_error(tls_io) |
| 695 | 693 | } |
| 696 | 694 | } |
| 697 | 695 | |
| 698 | 696 | fn close_conn(conn: Conn) -> () / <io> { |
| 699 | 697 | let _ = conn_write(conn, protocol.encode(protocol.Terminate)) |
| 700 | 698 | match conn { |
| 701 | | − Tcp(c) -> net.close(c), |
| 702 | | − Tls(c) -> tls.close(c), |
| 699 | + Tcp(c) -> net.close(c) |
| 700 | + , Tls(c) -> tls.close(c) |
| 703 | 701 | } |
| 704 | 702 | } |
| 705 | 703 | |
| @@ -759,9 +757,9 @@ fn drain_until_ready(conn: Conn, buffer: Bytes, at: Int) -> Result< |
| 759 | 757 | / <io> { |
| 760 | 758 | use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 761 | 759 | match msg { |
| 762 | | − protocol.ReadyForQuery(_) -> Ok((buffer, at)), |
| 763 | | − protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 764 | | − _ -> drain_until_ready(conn, buffer, at), |
| 760 | + protocol.ReadyForQuery(_) -> Ok((buffer, at)) |
| 761 | + , protocol.ErrorResponse(fields) -> Error(Server(fields)) |
| 762 | + , _ -> drain_until_ready(conn, buffer, at) |
| 765 | 763 | } |
| 766 | 764 | } |
| 767 | 765 | |
| @@ -775,8 +773,8 @@ fn listen_serve( |
| 775 | 773 | , subscriber: Subject<Notification> |
| 776 | 774 | ) -> () / <io, proc> { |
| 777 | 775 | match read_message(conn, buffer, at) { |
| 778 | | − Error(_) -> (), |
| 779 | | − Ok((msg, buffer, at)) -> { |
| 776 | + Error(_) -> () |
| 777 | + , Ok((msg, buffer, at)) -> { |
| 780 | 778 | match msg { |
| 781 | 779 | protocol.NotificationResponse( |
| 782 | 780 | source |
| @@ -786,8 +784,8 @@ fn listen_serve( |
| 786 | 784 | if ch == channel { |
| 787 | 785 | process.send(subscriber, Notification(ch, payload, source)) |
| 788 | 786 | } |
| 789 | | − }, |
| 790 | | − _ -> (), |
| 787 | + } |
| 788 | + , _ -> () |
| 791 | 789 | } |
| 792 | 790 | listen_serve(conn, buffer, at, channel, subscriber) |
| 793 | 791 | } |
| @@ -804,48 +802,48 @@ fn encode_params( |
| 804 | 802 | , acc: List<protocol.BindParam> |
| 805 | 803 | ) -> List<protocol.BindParam> { |
| 806 | 804 | match (params, oids) { |
| 807 | | − ([], _) -> list.reverse(acc), |
| 808 | | − ([param, ..rest], [oid, ..rest_oids]) -> |
| 809 | | − encode_params(rest, rest_oids, [encode_one(param, oid), ..acc]), |
| 810 | | − (_, []) -> list.reverse(acc), // the arity check in run_query came first |
| 805 | + ([], _) -> list.reverse(acc) |
| 806 | + , ([param, ..rest], [oid, ..rest_oids]) -> |
| 807 | + encode_params(rest, rest_oids, [encode_one(param, oid), ..acc]) |
| 808 | + , (_, []) -> list.reverse(acc) // the arity check in run_query came first |
| 811 | 809 | } |
| 812 | 810 | } |
| 813 | 811 | |
| 814 | 812 | fn encode_one(value: Value, oid: Int) -> protocol.BindParam { |
| 815 | 813 | match value { |
| 816 | | − Null -> protocol.BindParam(None, 0), |
| 817 | | − Integer(n) -> match oid { |
| 818 | | − 21 -> binary_param(binary.be16(n)), // int2 |
| 819 | | − 23 -> binary_param(binary.be32(n)), // int4 |
| 820 | | − 20 -> binary_param(binary.be64(n)), // int8 |
| 821 | | − _ -> text_param("{n}"), |
| 822 | | − }, |
| 823 | | − Real(f) -> match oid { |
| 824 | | − 700 -> binary_param(binary.f32(f)), // float4 |
| 825 | | − 701 -> binary_param(binary.f64(f)), // float8 |
| 826 | | − _ -> text_param("{f}"), |
| 827 | | − }, |
| 828 | | − Boolean(b) -> if oid == 16 { |
| 814 | + Null -> protocol.BindParam(None, 0) |
| 815 | + , Integer(n) -> match oid { |
| 816 | + 21 -> binary_param(binary.be16(n)) // int2 |
| 817 | + , 23 -> binary_param(binary.be32(n)) // int4 |
| 818 | + , 20 -> binary_param(binary.be64(n)) // int8 |
| 819 | + , _ -> text_param("{n}") |
| 820 | + } |
| 821 | + , Real(f) -> match oid { |
| 822 | + 700 -> binary_param(binary.f32(f)) // float4 |
| 823 | + , 701 -> binary_param(binary.f64(f)) // float8 |
| 824 | + , _ -> text_param("{f}") |
| 825 | + } |
| 826 | + , Boolean(b) -> if oid == 16 { |
| 829 | 827 | binary_param(bytes.from_list([if b { 1 } else { 0 }])) |
| 830 | 828 | } else { |
| 831 | 829 | text_param(show(value)) |
| 832 | | − }, |
| 833 | | − Text(s) -> text_param(s), |
| 834 | | − Bytea(b) -> if oid == 17 { |
| 830 | + } |
| 831 | + , Text(s) -> text_param(s) |
| 832 | + , Bytea(b) -> if oid == 17 { |
| 835 | 833 | binary_param(b) // raw, no `\x` form |
| 836 | 834 | } else { |
| 837 | 835 | text_param(show(value)) |
| 838 | | − }, |
| 839 | | − Uuid(id) -> if oid == 2950 { |
| 836 | + } |
| 837 | + , Uuid(id) -> if oid == 2950 { |
| 840 | 838 | binary_param(uuid.to_bytes(id)) |
| 841 | 839 | } else { |
| 842 | 840 | text_param(show(value)) |
| 843 | | − }, |
| 844 | | − Timestamptz(t) -> if oid == 1184 { |
| 841 | + } |
| 842 | + , Timestamptz(t) -> if oid == 1184 { |
| 845 | 843 | binary_param(binary.be64(pg_micros(t))) |
| 846 | 844 | } else { |
| 847 | 845 | text_param(show(value)) |
| 848 | | − }, |
| 846 | + } |
| 849 | 847 | } |
| 850 | 848 | } |
| 851 | 849 | |
| @@ -873,14 +871,14 @@ fn pg_micros(t: Timestamp) -> Int { |
| 873 | 871 | impl Show<Value> { |
| 874 | 872 | fn show(value) { |
| 875 | 873 | match value { |
| 876 | | − Null -> "", |
| 877 | | − Integer(n) -> "{n}", |
| 878 | | − Real(f) -> "{f}", |
| 879 | | − Boolean(b) -> if b { "True" } else { "False" }, |
| 880 | | − Text(s) -> s, |
| 881 | | − Bytea(b) -> "\\x" <> hex(b), |
| 882 | | − Uuid(id) -> uuid.to_rfc(id), |
| 883 | | − Timestamptz(t) -> time.format(t), |
| 874 | + Null -> "" |
| 875 | + , Integer(n) -> "{n}" |
| 876 | + , Real(f) -> "{f}" |
| 877 | + , Boolean(b) -> if b { "True" } else { "False" } |
| 878 | + , Text(s) -> s |
| 879 | + , Bytea(b) -> "\\x" <> hex(b) |
| 880 | + , Uuid(id) -> uuid.to_rfc(id) |
| 881 | + , Timestamptz(t) -> time.format(t) |
| 884 | 882 | } |
| 885 | 883 | } |
| 886 | 884 | } |
| @@ -896,12 +894,12 @@ fn hex_byte(b: Int) -> String { |
| 896 | 894 | |
| 897 | 895 | fn hex_digit(d: Int) -> String { |
| 898 | 896 | match d { |
| 899 | | − 10 -> "a", |
| 900 | | − 11 -> "b", |
| 901 | | − 12 -> "c", |
| 902 | | − 13 -> "d", |
| 903 | | − 14 -> "e", |
| 904 | | − 15 -> "f", |
| 905 | | − _ -> "{d}", |
| 897 | + 10 -> "a" |
| 898 | + , 11 -> "b" |
| 899 | + , 12 -> "c" |
| 900 | + , 13 -> "d" |
| 901 | + , 14 -> "e" |
| 902 | + , 15 -> "f" |
| 903 | + , _ -> "{d}" |
| 906 | 904 | } |
| 907 | 905 | } |