commit e2844fc7e0f38fe13d3498b406f978a0f972eccd Author: Joe Ardent Date: Fri May 17 11:20:28 2024 -0700 protocol should be good enough to start diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..4c8d0e1 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,4 @@ +imports_granularity = "Crate" +group_imports = "StdExternalCrate" +wrap_comments = true +edition = "2021" diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..05d359c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,109 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "gg-echo" +version = "0.0.1" + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "maelstrom-node" +version = "0.0.1" + +[[package]] +name = "maelstrom-protocol" +version = "0.0.1" +dependencies = [ + "serde", + "serde_json", + "serde_repr", +] + +[[package]] +name = "proc-macro2" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "serde" +version = "1.0.202" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.202" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_repr" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..72b48fa --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] +members = ["gg-echo", "maelstrom-protocol", "maelstrom-node"] +resolver = "2" + +[workspace.package] +version = "0.0.1" +authors = ["joe ardent"] diff --git a/gg-echo/Cargo.toml b/gg-echo/Cargo.toml new file mode 100644 index 0000000..52c0c15 --- /dev/null +++ b/gg-echo/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "gg-echo" +edition = "2021" +version.workspace = true +authors.workspace = true + +[dependencies] diff --git a/gg-echo/src/main.rs b/gg-echo/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/gg-echo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/maelstrom-node/Cargo.toml b/maelstrom-node/Cargo.toml new file mode 100644 index 0000000..7cc5479 --- /dev/null +++ b/maelstrom-node/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "maelstrom-node" +edition = "2021" +version.workspace = true +authors.workspace = true + +[dependencies] diff --git a/maelstrom-node/src/lib.rs b/maelstrom-node/src/lib.rs new file mode 100644 index 0000000..7d12d9a --- /dev/null +++ b/maelstrom-node/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/maelstrom-protocol/Cargo.toml b/maelstrom-protocol/Cargo.toml new file mode 100644 index 0000000..42ca124 --- /dev/null +++ b/maelstrom-protocol/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "maelstrom-protocol" +edition = "2021" +version.workspace = true +authors.workspace = true + +[dependencies] +serde = { version = "1", default-features = false, features = ["derive"] } +serde_json = { version = "1", default-features = false, features = ["std"] } +serde_repr = "0.1" diff --git a/maelstrom-protocol/src/lib.rs b/maelstrom-protocol/src/lib.rs new file mode 100644 index 0000000..5534b8a --- /dev/null +++ b/maelstrom-protocol/src/lib.rs @@ -0,0 +1,113 @@ +use serde::{Deserialize, Serialize}; +use serde_repr::{Deserialize_repr, Serialize_repr}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Message

{ + src: String, + dest: String, + body: Body

, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Body

{ + #[serde(rename = "type")] + pub typ: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub msg_id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub in_reply_to: Option, + #[serde(flatten)] + pub payload: P, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(untagged)] +pub enum ErrorCode { + Definite(DefiniteError), + Indefinite(IndefiniteError), +} + +#[derive(Debug, Clone, Serialize_repr, Deserialize_repr)] +#[repr(u64)] +pub enum DefiniteError { + NodeNotFound = 2, + NotSupported = 10, + TemporarilyUnavailable = 11, + MalformedRequest = 12, + Abort = 14, + KeyNotFound = 20, + KeyAlreadyExists = 21, + PreconditionFailed = 22, + TxnConflict = 30, +} + +#[derive(Debug, Clone, Serialize_repr, Deserialize_repr)] +#[repr(u64)] +pub enum IndefiniteError { + Timeout = 0, + Crash = 13, +} + +pub fn init_ok(msg_id: u64, in_reply_to: Option) -> Body { + Body { + typ: "init_ok".to_string(), + msg_id: Some(msg_id), + in_reply_to, + payload: payloads::InitOk, + } +} + +pub fn error( + msg_id: u64, + in_reply_to: Option, + code: ErrorCode, + text: Option<&str>, +) -> Body { + Body { + typ: "error".to_string(), + msg_id: Some(msg_id), + in_reply_to, + payload: payloads::ErrorResp { + code, + text: text.map(|s| s.to_string()), + }, + } +} + +pub mod payloads { + use crate::ErrorCode; + use serde::{de::DeserializeOwned, Deserialize, Serialize}; + + pub trait Payload: + std::fmt::Debug + Sized + Send + Clone + Serialize + DeserializeOwned + { + } + impl Payload for T where T: std::fmt::Debug + Sized + Send + Clone + Serialize + DeserializeOwned {} + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct Init { + pub node_id: String, + pub node_ids: Vec, + } + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct InitOk; + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct ErrorResp { + pub code: ErrorCode, + pub text: Option, + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn error_codes() { + let ec = ErrorCode::Definite(DefiniteError::Abort); + let e = serde_json::to_string(&ec).unwrap(); + assert_eq!(&e, "14"); + } +}