From de063eae858eb3335d2558a57ee6a88689635200 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 19 Feb 2023 15:18:25 +0100 Subject: [PATCH] feat(sdk): with return result instead of unwrap --- crates/dagger-codegen/src/rust/functions.rs | 20 ++- .../examples/build-the-application/main.rs | 2 +- crates/dagger-sdk/examples/caching/main.rs | 8 +- .../examples/existing-dockerfile/main.rs | 5 +- .../examples/first-pipeline/main.rs | 2 +- .../examples/multi-stage-build/main.rs | 6 +- .../examples/publish-the-application/main.rs | 6 +- .../examples/test-the-application/main.rs | 4 +- crates/dagger-sdk/src/gen.rs | 158 +++++++++--------- crates/dagger-sdk/tests/mod.rs | 3 +- 10 files changed, 116 insertions(+), 98 deletions(-) diff --git a/crates/dagger-codegen/src/rust/functions.rs b/crates/dagger-codegen/src/rust/functions.rs index 011409c..7005b4a 100644 --- a/crates/dagger-codegen/src/rust/functions.rs +++ b/crates/dagger-codegen/src/rust/functions.rs @@ -1,5 +1,5 @@ use convert_case::{Case, Casing}; -use dagger_core::introspection::FullTypeFields; +use dagger_core::introspection::{FullTypeFields, TypeRef}; use genco::prelude::rust; use genco::quote; use genco::tokens::quoted; @@ -37,7 +37,7 @@ pub fn format_function(funcs: &CommonFunctions, field: &FullTypeFields) -> Optio let output_type = field .type_ .pipe(|t| &t.type_ref) - .pipe(|t| funcs.format_output_type(t)); + .pipe(|t| render_output_type(funcs, t)); Some(quote! { $(signature)( @@ -122,6 +122,20 @@ fn render_optional_args(_funcs: &CommonFunctions, field: &FullTypeFields) -> Opt } } +fn render_output_type(funcs: &CommonFunctions, type_ref: &TypeRef) -> rust::Tokens { + let output_type = funcs.format_output_type(type_ref); + + if type_ref_is_object(type_ref) || type_ref_is_list_of_objects(type_ref) { + return quote! { + $(output_type) + }; + } + + quote! { + eyre::Result<$output_type> + } +} + fn render_execution(funcs: &CommonFunctions, field: &FullTypeFields) -> rust::Tokens { if let Some(true) = field.type_.pipe(|t| type_ref_is_object(&t.type_ref)) { let output_type = funcs.format_output_type(&field.type_.as_ref().unwrap().type_ref); @@ -163,7 +177,7 @@ fn render_execution(funcs: &CommonFunctions, field: &FullTypeFields) -> rust::To let graphql_client = rust::import("crate::client", "graphql_client"); quote! { - query.execute(&$graphql_client(&self.conn)).unwrap() + query.execute(&$graphql_client(&self.conn)) } } diff --git a/crates/dagger-sdk/examples/build-the-application/main.rs b/crates/dagger-sdk/examples/build-the-application/main.rs index 0da6cf6..ba204c8 100644 --- a/crates/dagger-sdk/examples/build-the-application/main.rs +++ b/crates/dagger-sdk/examples/build-the-application/main.rs @@ -14,7 +14,7 @@ fn main() -> eyre::Result<()> { let source = client .container(None) .from("node:16".into()) - .with_mounted_directory("/src".into(), host_source_dir.id()); + .with_mounted_directory("/src".into(), host_source_dir.id()?); let runner = source .with_workdir("/src".into()) diff --git a/crates/dagger-sdk/examples/caching/main.rs b/crates/dagger-sdk/examples/caching/main.rs index 323926c..2761295 100644 --- a/crates/dagger-sdk/examples/caching/main.rs +++ b/crates/dagger-sdk/examples/caching/main.rs @@ -12,12 +12,12 @@ fn main() -> eyre::Result<()> { }), ); - let node_cache = client.cache_volume("node".into()).id(); + let node_cache = client.cache_volume("node".into()).id()?; let source = client .container(None) .from("node:16".into()) - .with_mounted_directory("/src".into(), host_source_dir.id()) + .with_mounted_directory("/src".into(), host_source_dir.id()?) .with_mounted_cache("/src/node_modules".into(), node_cache, None); let runner = source @@ -43,11 +43,11 @@ fn main() -> eyre::Result<()> { let ref_ = client .container(None) .from("nginx".into()) - .with_directory("/usr/share/nginx/html".into(), build_dir.id(), None) + .with_directory("/usr/share/nginx/html".into(), build_dir.id()?, None) .publish( format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::()), None, - ); + )?; println!("published image to: {}", ref_); diff --git a/crates/dagger-sdk/examples/existing-dockerfile/main.rs b/crates/dagger-sdk/examples/existing-dockerfile/main.rs index 4db5ba5..086e425 100644 --- a/crates/dagger-sdk/examples/existing-dockerfile/main.rs +++ b/crates/dagger-sdk/examples/existing-dockerfile/main.rs @@ -1,4 +1,3 @@ - use rand::Rng; fn main() -> eyre::Result<()> { @@ -12,11 +11,11 @@ fn main() -> eyre::Result<()> { let ref_ = client .container(None) - .build(context_dir.id(), None) + .build(context_dir.id()?, None) .publish( format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::()), None, - ); + )?; println!("published image to: {}", ref_); diff --git a/crates/dagger-sdk/examples/first-pipeline/main.rs b/crates/dagger-sdk/examples/first-pipeline/main.rs index 3eccc36..c1fbd5a 100644 --- a/crates/dagger-sdk/examples/first-pipeline/main.rs +++ b/crates/dagger-sdk/examples/first-pipeline/main.rs @@ -5,7 +5,7 @@ fn main() -> eyre::Result<()> { .container(None) .from("golang:1.19".into()) .with_exec(vec!["go".into(), "version".into()], None) - .stdout(); + .stdout()?; println!("Hello from Dagger and {}", version.trim()); diff --git a/crates/dagger-sdk/examples/multi-stage-build/main.rs b/crates/dagger-sdk/examples/multi-stage-build/main.rs index 1fd6f65..63fe24a 100644 --- a/crates/dagger-sdk/examples/multi-stage-build/main.rs +++ b/crates/dagger-sdk/examples/multi-stage-build/main.rs @@ -15,7 +15,7 @@ fn main() -> eyre::Result<()> { let source = client .container(None) .from("node:16".into()) - .with_mounted_directory("/src".into(), host_source_dir.id()); + .with_mounted_directory("/src".into(), host_source_dir.id()?); let runner = source .with_workdir("/src".into()) @@ -40,11 +40,11 @@ fn main() -> eyre::Result<()> { let ref_ = client .container(None) .from("nginx".into()) - .with_directory("/usr/share/nginx/html".into(), build_dir.id(), None) + .with_directory("/usr/share/nginx/html".into(), build_dir.id()?, None) .publish( format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::()), None, - ); + )?; println!("published image to: {}", ref_); diff --git a/crates/dagger-sdk/examples/publish-the-application/main.rs b/crates/dagger-sdk/examples/publish-the-application/main.rs index e626036..07935dc 100644 --- a/crates/dagger-sdk/examples/publish-the-application/main.rs +++ b/crates/dagger-sdk/examples/publish-the-application/main.rs @@ -16,7 +16,7 @@ fn main() -> eyre::Result<()> { let source = client .container(None) .from("node:16".into()) - .with_mounted_directory("/src".into(), host_source_dir.id()); + .with_mounted_directory("/src".into(), host_source_dir.id()?); let runner = source .with_workdir("/src".into()) @@ -44,13 +44,13 @@ fn main() -> eyre::Result<()> { .from("nginx".into()) .with_directory( "/usr/share/nginx/html".into(), - client.host().directory(output.into(), None).id(), + client.host().directory(output.into(), None).id()?, None, ) .publish( format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::()), None, - ); + )?; println!("published image to: {}", ref_); diff --git a/crates/dagger-sdk/examples/test-the-application/main.rs b/crates/dagger-sdk/examples/test-the-application/main.rs index 11e8188..795b707 100644 --- a/crates/dagger-sdk/examples/test-the-application/main.rs +++ b/crates/dagger-sdk/examples/test-the-application/main.rs @@ -14,7 +14,7 @@ fn main() -> eyre::Result<()> { let source = client .container(None) .from("node:16".into()) - .with_mounted_directory("/src".into(), host_source_dir.id()); + .with_mounted_directory("/src".into(), host_source_dir.id()?); let runner = source .with_workdir("/src".into()) @@ -30,7 +30,7 @@ fn main() -> eyre::Result<()> { ], None, ) - .stderr(); + .stderr()?; println!("{}", out); diff --git a/crates/dagger-sdk/src/gen.rs b/crates/dagger-sdk/src/gen.rs index c8e74fe..9e0d27c 100644 --- a/crates/dagger-sdk/src/gen.rs +++ b/crates/dagger-sdk/src/gen.rs @@ -21,8 +21,8 @@ pub struct SecretId(String); pub struct SocketId(String); #[derive(Serialize, Deserialize)] pub struct BuildArg { - pub value: String, pub name: String, + pub value: String, } pub struct CacheVolume { pub proc: Arc, @@ -31,10 +31,10 @@ pub struct CacheVolume { } impl CacheVolume { - pub fn id(&self) -> CacheId { + pub fn id(&self) -> eyre::Result { let mut query = self.selection.select("id"); - query.execute(&graphql_client(&self.conn)).unwrap() + query.execute(&graphql_client(&self.conn)) } } pub struct Container { @@ -112,10 +112,10 @@ impl Container { conn: self.conn.clone(), }; } - pub fn default_args(&self) -> Vec { + pub fn default_args(&self) -> eyre::Result> { let mut query = self.selection.select("defaultArgs"); - query.execute(&graphql_client(&self.conn)).unwrap() + query.execute(&graphql_client(&self.conn)) } pub fn directory(&self, path: String) -> Directory { let mut query = self.selection.select("directory"); @@ -128,17 +128,17 @@ impl Container { conn: self.conn.clone(), }; } - pub fn entrypoint(&self) -> Vec { + pub fn entrypoint(&self) -> eyre::Result> { let mut query = self.selection.select("entrypoint"); - query.execute(&graphql_client(&self.conn)).unwrap() + query.execute(&graphql_client(&self.conn)) } - pub fn env_variable(&self, name: String) -> String { + pub fn env_variable(&self, name: String) -> eyre::Result { let mut query = self.selection.select("envVariable"); query = query.arg("name", name); - query.execute(&graphql_client(&self.conn)).unwrap() + query.execute(&graphql_client(&self.conn)) } pub fn env_variables(&self) -> Vec { let mut query = self.selection.select("envVariables"); @@ -179,12 +179,12 @@ impl Container { conn: self.conn.clone(), }; } - pub fn exit_code(&self) -> isize { + pub fn exit_code(&self) -> eyre::Result { let mut query = self.selection.select("exitCode"); - query.execute(&graphql_client(&self.conn)).unwrap() + query.execute(&graphql_client(&self.conn)) } - pub fn export(&self, path: String, opts: Option) -> bool { + pub fn export(&self, path: String, opts: Option) -> eyre::Result { let mut query = self.selection.select("export"); query = query.arg("path", path); @@ -194,7 +194,7 @@ impl Container { } } - query.execute(&graphql_client(&self.conn)).unwrap() + query.execute(&graphql_client(&self.conn)) } pub fn file(&self, path: String) -> File { let mut query = self.selection.select("file"); @@ -227,17 +227,17 @@ impl Container { conn: self.conn.clone(), }; } - pub fn id(&self) -> ContainerId { + pub fn id(&self) -> eyre::Result { let mut query = self.selection.select("id"); - query.execute(&graphql_client(&self.conn)).unwrap() + query.execute(&graphql_client(&self.conn)) } - pub fn label(&self, name: String) -> String { + pub fn label(&self, name: String) -> eyre::Result { let mut query = self.selection.select("label"); query = query.arg("name", name); - query.execute(&graphql_client(&self.conn)).unwrap() + query.execute(&graphql_client(&self.conn)) } pub fn labels(&self) -> Vec