Repository: edgedb/edgedb-cli
Pull request #840 (2022-09-15 15:12:02)

Upgrade of Rust bindings


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 4c347c7..60445e8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1206,7 +1206,7 @@ dependencies = [
 [[package]]
 name = "edgedb-client"
 version = "0.3.0"
-source = "git+https://github.com/edgedb/edgedb-rust#b8be0a885de87f3a9cc578e8f28ab1b78516bada"
+source = "git+https://github.com/edgedb/edgedb-rust#6b385b3c9f8c548fddc61cd53c1182c13c92ded7"
 dependencies = [
  "anyhow",
  "async-std",
@@ -1239,7 +1239,7 @@ dependencies = [
 [[package]]
 name = "edgedb-derive"
 version = "0.4.0"
-source = "git+https://github.com/edgedb/edgedb-rust#b8be0a885de87f3a9cc578e8f28ab1b78516bada"
+source = "git+https://github.com/edgedb/edgedb-rust#6b385b3c9f8c548fddc61cd53c1182c13c92ded7"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -1250,7 +1250,7 @@ dependencies = [
 [[package]]
 name = "edgedb-errors"
 version = "0.3.0"
-source = "git+https://github.com/edgedb/edgedb-rust#b8be0a885de87f3a9cc578e8f28ab1b78516bada"
+source = "git+https://github.com/edgedb/edgedb-rust#6b385b3c9f8c548fddc61cd53c1182c13c92ded7"
 dependencies = [
  "bytes",
 ]
@@ -1258,7 +1258,7 @@ dependencies = [
 [[package]]
 name = "edgedb-protocol"
 version = "0.4.0"
-source = "git+https://github.com/edgedb/edgedb-rust#b8be0a885de87f3a9cc578e8f28ab1b78516bada"
+source = "git+https://github.com/edgedb/edgedb-rust#6b385b3c9f8c548fddc61cd53c1182c13c92ded7"
 dependencies = [
  "bigdecimal",
  "bitflags",
diff --git a/src/commands/backslash.rs b/src/commands/backslash.rs
index 86b0ec8..8a4eafb 100644
--- a/src/commands/backslash.rs
+++ b/src/commands/backslash.rs
@@ -663,8 +663,10 @@ pub async fn execute(cmd: &BackslashCmd, prompt: &mut repl::State)
                          prompt.edgeql_state.clone())
                     )
             };
-            let value = desc.decode(&data)?;
-            println!("Descriptor id: {}", desc.descriptor_id());
+            let desc = desc.decode()?;
+            let codec = desc.build_codec()?;
+            let value = codec.decode(&data.data)?;
+            println!("Descriptor id: {}", desc.id());
             print::native_to_stdout(
                 stream::from_iter([Ok::<_, Error>(value)]),
                 &prompt.print,
@@ -680,8 +682,8 @@ pub async fn execute(cmd: &BackslashCmd, prompt: &mut repl::State)
                     .map(|c| c.get_state_desc())
                     .unwrap_or(prompt.edgeql_state_desc.clone())
             };
-            let typedesc = desc.decoded()?;
-            eprintln!("Descriptor id: {}", desc.descriptor_id());
+            let typedesc = desc.decode()?;
+            eprintln!("Descriptor id: {}", desc.id);
             eprintln!("Descriptor: {:#?}", typedesc.descriptors());
             eprintln!("Codec: {:#?}", typedesc.build_codec()?);
             Ok(Skip)
diff --git a/src/interactive.rs b/src/interactive.rs
index 7f378bc..c0ebdc4 100644
--- a/src/interactive.rs
+++ b/src/interactive.rs
@@ -12,14 +12,14 @@ use async_std::channel::{bounded as channel};
 use bytes::{Bytes, BytesMut};
 use colorful::Colorful;
 
-use edgedb_client::client::{EdgeqlStateDesc, EdgeqlState};
 use edgedb_client::errors::{ErrorKind, ClientEncodingError};
 use edgedb_client::errors::{Error as EdgedbError, StateMismatchError};
 use edgedb_protocol::client_message::ClientMessage;
 use edgedb_protocol::client_message::{DescribeStatement, DescribeAspect};
 use edgedb_protocol::client_message::{Execute0, Execute1};
 use edgedb_protocol::client_message::{Parse, Prepare, IoFormat, Cardinality};
-use edgedb_protocol::common::{Capabilities, CompilationFlags};
+use edgedb_protocol::common::{Capabilities, CompilationFlags, State};
+use edgedb_protocol::common::{RawTypedesc};
 use edgedb_protocol::model::Duration;
 use edgedb_protocol::query_arg::{Encoder, QueryArgs};
 use edgedb_protocol::server_message::ServerMessage;
@@ -136,8 +136,8 @@ pub fn main(options: Options, cfg: Config) -> Result<(), anyhow::Error> {
         last_version: None,
         connection: None,
         initial_text: "".into(),
-        edgeql_state_desc: EdgeqlStateDesc::uninitialized(),
-        edgeql_state: EdgeqlState::empty(),
+        edgeql_state_desc: RawTypedesc::uninitialized(),
+        edgeql_state: State::empty(),
     };
     let handle = task::spawn(_main(options, state, cfg));
     prompt::main(repl_wr, control_rd)?;
@@ -303,8 +303,7 @@ async fn execute_query1(options: &Options, mut state: &mut repl::State,
                 Json => IoFormat::Json,
             },
             expected_cardinality: Cardinality::Many,
-            state_typedesc_id: seq.get_state_typedesc_id(),
-            state_data: seq.get_state_data(),
+            state: seq.get_state().clone(),
             command_text: String::from(statement),
         }),
         ClientMessage::Flush,
@@ -390,10 +389,9 @@ async fn execute_query1(options: &Options, mut state: &mut repl::State,
             },
             expected_cardinality: Cardinality::Many,
             command_text: String::from(statement),
-            state_typedesc_id: seq.get_state_typedesc_id(),
-            state_data: seq.get_state_data(),
-            input_typedesc_id: data_description.input_typedesc_id,
-            output_typedesc_id: data_description.output_typedesc_id,
+            state: seq.get_state().clone(),
+            input_typedesc_id: data_description.input.id,
+            output_typedesc_id: data_description.output.id,
             arguments: arguments.freeze(),
         }),
         ClientMessage::Sync,
diff --git a/src/prompt/variable.rs b/src/prompt/variable.rs
index 70fa55b..6d9cc80 100644
--- a/src/prompt/variable.rs
+++ b/src/prompt/variable.rs
@@ -7,6 +7,7 @@ use anyhow::Context as _;
 use colorful::Colorful;
 use bigdecimal::BigDecimal;
 use edgedb_protocol::value::Value;
+use edgedb_protocol::model;
 use num_bigint::ToBigInt;
 use rustyline::completion::Completer;
 use rustyline::error::ReadlineError;
@@ -150,7 +151,9 @@ impl VariableInput for Json {
             Err(e) if e.classify()  == serde_json::error::Category::Eof
             => Err(Error::Incomplete),
             Err(e) => Err(no_pos_err(e)),
-            Ok(_) => Ok(Value::Json(input.into())),
+            Ok(_) => Ok(Value::Json(unsafe {
+                model::Json::new_unchecked(input.into())
+            })),
         }
     }
 }
diff --git a/src/repl.rs b/src/repl.rs
index c7858d0..e2858dd 100644
--- a/src/repl.rs
+++ b/src/repl.rs
@@ -3,8 +3,10 @@ use std::time::Duration;
 
 use anyhow::Context;
 use async_std::channel::{Sender, Receiver, RecvError};
+use bytes::BytesMut;
 use colorful::Colorful;
-use edgedb_client::client::{Connection, EdgeqlStateDesc, EdgeqlState};
+use edgedb_client::client::Connection;
+use edgedb_protocol::common::{State as EdgeqlState, RawTypedesc};
 use edgedb_protocol::model::{Duration as EdbDuration};
 use edgedb_protocol::server_message::TransactionState;
 
@@ -64,7 +66,7 @@ pub struct State {
     pub connection: Option<Connection>,
     pub last_version: Option<String>,
     pub initial_text: String,
-    pub edgeql_state_desc: EdgeqlStateDesc,
+    pub edgeql_state_desc: RawTypedesc,
     pub edgeql_state: EdgeqlState,
 }
 
@@ -262,14 +264,22 @@ impl State {
     }
     pub fn try_update_state(&mut self) -> anyhow::Result<bool> {
         if let Some(conn) = &mut self.connection {
-            match self.edgeql_state_desc.decode(&self.edgeql_state)? {
-                Some(value) => {
-                    let nstate = conn.get_state_desc().encode(&value)?;
-                    self.edgeql_state = nstate.clone();
-                    conn.set_state(nstate);
-                    return Ok(true)
-                }
-                None => {}
+            if self.edgeql_state.data.len() > 0 {
+                let desc = self.edgeql_state_desc.decode()?;
+                let codec = desc.build_codec()?;
+                let value = codec.decode(&self.edgeql_state.data)?;
+
+                let desc = conn.get_state_desc().decode()?;
+                let codec = desc.build_codec()?;
+                let mut buf = BytesMut::with_capacity(
+                    self.edgeql_state.data.len());
+                codec.encode(&mut buf, &value)?;
+                self.edgeql_state = EdgeqlState {
+                    typedesc_id: desc.id().clone(),
+                    data: buf.freeze(),
+                };
+                conn.set_state(self.edgeql_state.clone());
+                return Ok(true)
             }
         }
         Ok(false)
diff --git a/src/variables.rs b/src/variables.rs
index 451b843..da8778a 100644
--- a/src/variables.rs
+++ b/src/variables.rs
@@ -4,7 +4,7 @@ use std::sync::Arc;
 
 use edgedb_protocol::value::Value;
 use edgedb_protocol::codec;
-use edgedb_protocol::descriptors::{InputTypedesc, Descriptor};
+use edgedb_protocol::descriptors::{Typedesc, Descriptor};
 use crate::repl;
 use crate::prompt;
 use crate::prompt::variable::{self, VariableInput};
@@ -14,7 +14,7 @@ use crate::prompt::variable::{self, VariableInput};
 pub struct Canceled;
 
 
-pub async fn input_variables(desc: &InputTypedesc, state: &mut repl::PromptRpc)
+pub async fn input_variables(desc: &Typedesc, state: &mut repl::PromptRpc)
     -> Result<Value, anyhow::Error>
 {
     // only for protocol < 0.12
@@ -66,7 +66,7 @@ pub async fn input_variables(desc: &InputTypedesc, state: &mut repl::PromptRpc)
     }
 }
 
-async fn input_item(name: &str, mut item: &Descriptor, all: &InputTypedesc,
+async fn input_item(name: &str, mut item: &Descriptor, all: &Typedesc,
     state: &mut repl::PromptRpc, optional: bool)
     -> Result<Option<Value>, anyhow::Error>
 {


Test patch:
diff --git a/src/print/tests.rs b/src/print/tests.rs
index 73295b8..06ba16d 100644
--- a/src/print/tests.rs
+++ b/src/print/tests.rs
@@ -3,9 +3,10 @@ use std::convert::TryFrom;
 use std::str::FromStr;
 use std::pin::Pin;
 
-use async_std::task;
 use async_std::stream::Stream;
+use async_std::task;
 use bigdecimal::BigDecimal;
+use bytes::Bytes;
 
 use edgedb_protocol::value::Value;
 use edgedb_protocol::model::Datetime;
@@ -317,13 +318,13 @@ fn str() {
 #[test]
 fn bytes() {
     assert_eq!(
-        test_format(&[Value::Bytes(b"hello".to_vec())]).unwrap(),
+        test_format(&[Value::Bytes(Bytes::from_static(b"hello"))]).unwrap(),
         "{b'hello'}");
     assert_eq!(
-        test_format(&[Value::Bytes(b"a\nb".to_vec())]).unwrap(),
+        test_format(&[Value::Bytes(Bytes::from_static(b"a\nb"))]).unwrap(),
         "{b'a\\nb'}");
     assert_eq!(
-        test_format(&[Value::Bytes(b"a'b".to_vec())]).unwrap(),
+        test_format(&[Value::Bytes(Bytes::from_static(b"a'b"))]).unwrap(),
         r"{b'a\'b'}");
 }