Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1,536 changes: 1,536 additions & 0 deletions Rust_Enum/Cargo.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Rust_Enum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "Rust_Enum"
version = "0.1.0"
edition = "2024"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
ureq = "2.9"
image = "0.25"
63 changes: 63 additions & 0 deletions Rust_Enum/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use serde::Deserialize;
use std::error::Error;
use std::io::Read; // required for read_to_end()

#[derive(Debug)]
enum DogError {
Network(String),
Json(String),
ImageBytes(String),
}

impl std::fmt::Display for DogError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

impl Error for DogError {}

#[derive(Deserialize)]
struct DogApiResponse {
message: String,
status: String,
}

fn get_random_dog_url() -> Result<String, DogError> {
let url = "https://dog.ceo/api/breeds/image/random";

let resp = ureq::get(url).call()
.map_err(|e| DogError::Network(e.to_string()))?;

// Use a reader
let reader = resp.into_reader();
let json: DogApiResponse = serde_json::from_reader(reader)
.map_err(|e| DogError::Json(e.to_string()))?;

Ok(json.message)
}

fn download_image(url: &str) -> Result<Vec<u8>, DogError> {
let resp = ureq::get(url).call()
.map_err(|e| DogError::Network(e.to_string()))?;

let mut reader = resp.into_reader();
let mut buf = Vec::new();
reader.read_to_end(&mut buf)
.map_err(|e| DogError::ImageBytes(e.to_string()))?;

Ok(buf)
}

fn main() -> Result<(), Box<dyn Error>> {
println!("Fetching random dog image URL...");

let dog_url = get_random_dog_url()?;
println!("Dog image URL: {}", dog_url);

println!("Downloading image bytes...");
let bytes = download_image(&dog_url)?;
println!("Downloaded {} bytes", bytes.len());

Ok(())
}
1 change: 1 addition & 0 deletions Rust_Enum/target/.rustc_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc_fingerprint":3087243518391675650,"outputs":{"17747080675513052775":{"success":true,"status":"","code":0,"stdout":"rustc 1.92.0 (ded5c06cf 2025-12-08)\nbinary: rustc\ncommit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234\ncommit-date: 2025-12-08\nhost: x86_64-unknown-linux-gnu\nrelease: 1.92.0\nLLVM version: 21.1.3\n","stderr":""},"7971740275564407648":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/codespace/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
3 changes: 3 additions & 0 deletions Rust_Enum/target/CACHEDIR.TAG
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"$message_type":"diagnostic","message":"no method named `into_json` found for struct `Response<T>` in the current scope","code":{"code":"E0599","explanation":"This error occurs when a method is used on a type which doesn't implement it:\n\nErroneous code example:\n\n```compile_fail,E0599\nstruct Mouth;\n\nlet x = Mouth;\nx.chocolate(); // error: no method named `chocolate` found for type `Mouth`\n // in the current scope\n```\n\nIn this case, you need to implement the `chocolate` method to fix the error:\n\n```\nstruct Mouth;\n\nimpl Mouth {\n fn chocolate(&self) { // We implement the `chocolate` method here.\n println!(\"Hmmm! I love chocolate!\");\n }\n}\n\nlet x = Mouth;\nx.chocolate(); // ok!\n```\n"},"level":"error","spans":[{"file_name":"src/main.rs","byte_start":646,"byte_end":655,"line_start":31,"line_end":31,"column_start":10,"column_end":19,"is_primary":true,"text":[{"text":" resp.into_json::<DogImage>()","highlight_start":10,"highlight_end":19}],"label":"method not found in `Response<Body>`","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"\u001b[1m\u001b[91merror[E0599]\u001b[0m\u001b[1m: no method named `into_json` found for struct `Response<T>` in the current scope\u001b[0m\n \u001b[1m\u001b[94m--> \u001b[0msrc/main.rs:31:10\n \u001b[1m\u001b[94m|\u001b[0m\n\u001b[1m\u001b[94m31\u001b[0m \u001b[1m\u001b[94m|\u001b[0m resp.into_json::<DogImage>()\n \u001b[1m\u001b[94m|\u001b[0m \u001b[1m\u001b[91m^^^^^^^^^\u001b[0m \u001b[1m\u001b[91mmethod not found in `Response<Body>`\u001b[0m\n\n"}
{"$message_type":"diagnostic","message":"type annotations needed","code":{"code":"E0282","explanation":"The compiler could not infer a type and asked for a type annotation.\n\nErroneous code example:\n\n```compile_fail,E0282\nlet x = Vec::new();\n```\n\nThis error indicates that type inference did not result in one unique possible\ntype, and extra information is required. In most cases this can be provided\nby adding a type annotation. Sometimes you need to specify a generic type\nparameter manually.\n\nIn the example above, type `Vec` has a type parameter `T`. When calling\n`Vec::new`, barring any other later usage of the variable `x` that allows the\ncompiler to infer what type `T` is, the compiler needs to be told what it is.\n\nThe type can be specified on the variable:\n\n```\nlet x: Vec<i32> = Vec::new();\n```\n\nThe type can also be specified in the path of the expression:\n\n```\nlet x = Vec::<i32>::new();\n```\n\nIn cases with more complex types, it is not necessary to annotate the full\ntype. Once the ambiguity is resolved, the compiler can infer the rest:\n\n```\nlet x: Vec<_> = \"hello\".chars().rev().collect();\n```\n\nAnother way to provide the compiler with enough information, is to specify the\ngeneric type parameter:\n\n```\nlet x = \"hello\".chars().rev().collect::<Vec<char>>();\n```\n\nAgain, you need not specify the full type if the compiler can infer it:\n\n```\nlet x = \"hello\".chars().rev().collect::<Vec<_>>();\n```\n\nApart from a method or function with a generic type parameter, this error can\noccur when a type parameter of a struct or trait cannot be inferred. In that\ncase it is not always possible to use a type annotation, because all candidates\nhave the same return type. For instance:\n\n```compile_fail,E0282\nstruct Foo<T> {\n num: T,\n}\n\nimpl<T> Foo<T> {\n fn bar() -> i32 {\n 0\n }\n\n fn baz() {\n let number = Foo::bar();\n }\n}\n```\n\nThis will fail because the compiler does not know which instance of `Foo` to\ncall `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.\n"},"level":"error","spans":[{"file_name":"src/main.rs","byte_start":706,"byte_end":707,"line_start":32,"line_end":32,"column_start":37,"column_end":38,"is_primary":false,"text":[{"text":" .map_err(|e| DogError::Json(e.to_string()))","highlight_start":37,"highlight_end":38}],"label":"type must be known at this point","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"src/main.rs","byte_start":688,"byte_end":689,"line_start":32,"line_end":32,"column_start":19,"column_end":20,"is_primary":true,"text":[{"text":" .map_err(|e| DogError::Json(e.to_string()))","highlight_start":19,"highlight_end":20}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider giving this closure parameter an explicit type","code":null,"level":"help","spans":[{"file_name":"src/main.rs","byte_start":689,"byte_end":689,"line_start":32,"line_end":32,"column_start":20,"column_end":20,"is_primary":true,"text":[{"text":" .map_err(|e| DogError::Json(e.to_string()))","highlight_start":20,"highlight_end":20}],"label":null,"suggested_replacement":": /* Type */","suggestion_applicability":"HasPlaceholders","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[1m\u001b[91merror[E0282]\u001b[0m\u001b[1m: type annotations needed\u001b[0m\n \u001b[1m\u001b[94m--> \u001b[0msrc/main.rs:32:19\n \u001b[1m\u001b[94m|\u001b[0m\n\u001b[1m\u001b[94m32\u001b[0m \u001b[1m\u001b[94m|\u001b[0m .map_err(|e| DogError::Json(e.to_string()))\n \u001b[1m\u001b[94m|\u001b[0m \u001b[1m\u001b[91m^\u001b[0m \u001b[1m\u001b[94m-\u001b[0m \u001b[1m\u001b[94mtype must be known at this point\u001b[0m\n \u001b[1m\u001b[94m|\u001b[0m\n\u001b[1m\u001b[96mhelp\u001b[0m: consider giving this closure parameter an explicit type\n \u001b[1m\u001b[94m|\u001b[0m\n\u001b[1m\u001b[94m32\u001b[0m \u001b[1m\u001b[94m| \u001b[0m .map_err(|e\u001b[92m: /* Type */\u001b[0m| DogError::Json(e.to_string()))\n \u001b[1m\u001b[94m|\u001b[0m \u001b[92m++++++++++++\u001b[0m\n\n"}
{"$message_type":"diagnostic","message":"no method named `into_bytes` found for struct `Response<T>` in the current scope","code":{"code":"E0599","explanation":"This error occurs when a method is used on a type which doesn't implement it:\n\nErroneous code example:\n\n```compile_fail,E0599\nstruct Mouth;\n\nlet x = Mouth;\nx.chocolate(); // error: no method named `chocolate` found for type `Mouth`\n // in the current scope\n```\n\nIn this case, you need to implement the `chocolate` method to fix the error:\n\n```\nstruct Mouth;\n\nimpl Mouth {\n fn chocolate(&self) { // We implement the `chocolate` method here.\n println!(\"Hmmm! I love chocolate!\");\n }\n}\n\nlet x = Mouth;\nx.chocolate(); // ok!\n```\n"},"level":"error","spans":[{"file_name":"src/main.rs","byte_start":1020,"byte_end":1030,"line_start":43,"line_end":43,"column_start":22,"column_end":32,"is_primary":true,"text":[{"text":" let bytes = resp.into_bytes()","highlight_start":22,"highlight_end":32}],"label":"method not found in `Response<Body>`","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"\u001b[1m\u001b[91merror[E0599]\u001b[0m\u001b[1m: no method named `into_bytes` found for struct `Response<T>` in the current scope\u001b[0m\n \u001b[1m\u001b[94m--> \u001b[0msrc/main.rs:43:22\n \u001b[1m\u001b[94m|\u001b[0m\n\u001b[1m\u001b[94m43\u001b[0m \u001b[1m\u001b[94m|\u001b[0m let bytes = resp.into_bytes()\n \u001b[1m\u001b[94m|\u001b[0m \u001b[1m\u001b[91m^^^^^^^^^^\u001b[0m \u001b[1m\u001b[91mmethod not found in `Response<Body>`\u001b[0m\n\n"}
{"$message_type":"diagnostic","message":"type annotations needed","code":{"code":"E0282","explanation":"The compiler could not infer a type and asked for a type annotation.\n\nErroneous code example:\n\n```compile_fail,E0282\nlet x = Vec::new();\n```\n\nThis error indicates that type inference did not result in one unique possible\ntype, and extra information is required. In most cases this can be provided\nby adding a type annotation. Sometimes you need to specify a generic type\nparameter manually.\n\nIn the example above, type `Vec` has a type parameter `T`. When calling\n`Vec::new`, barring any other later usage of the variable `x` that allows the\ncompiler to infer what type `T` is, the compiler needs to be told what it is.\n\nThe type can be specified on the variable:\n\n```\nlet x: Vec<i32> = Vec::new();\n```\n\nThe type can also be specified in the path of the expression:\n\n```\nlet x = Vec::<i32>::new();\n```\n\nIn cases with more complex types, it is not necessary to annotate the full\ntype. Once the ambiguity is resolved, the compiler can infer the rest:\n\n```\nlet x: Vec<_> = \"hello\".chars().rev().collect();\n```\n\nAnother way to provide the compiler with enough information, is to specify the\ngeneric type parameter:\n\n```\nlet x = \"hello\".chars().rev().collect::<Vec<char>>();\n```\n\nAgain, you need not specify the full type if the compiler can infer it:\n\n```\nlet x = \"hello\".chars().rev().collect::<Vec<_>>();\n```\n\nApart from a method or function with a generic type parameter, this error can\noccur when a type parameter of a struct or trait cannot be inferred. In that\ncase it is not always possible to use a type annotation, because all candidates\nhave the same return type. For instance:\n\n```compile_fail,E0282\nstruct Foo<T> {\n num: T,\n}\n\nimpl<T> Foo<T> {\n fn bar() -> i32 {\n 0\n }\n\n fn baz() {\n let number = Foo::bar();\n }\n}\n```\n\nThis will fail because the compiler does not know which instance of `Foo` to\ncall `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.\n"},"level":"error","spans":[{"file_name":"src/main.rs","byte_start":1075,"byte_end":1076,"line_start":44,"line_end":44,"column_start":43,"column_end":44,"is_primary":false,"text":[{"text":" .map_err(|e| DogError::ImageBytes(e.to_string()))?;","highlight_start":43,"highlight_end":44}],"label":"type must be known at this point","suggested_replacement":null,"suggestion_applicability":null,"expansion":null},{"file_name":"src/main.rs","byte_start":1051,"byte_end":1052,"line_start":44,"line_end":44,"column_start":19,"column_end":20,"is_primary":true,"text":[{"text":" .map_err(|e| DogError::ImageBytes(e.to_string()))?;","highlight_start":19,"highlight_end":20}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider giving this closure parameter an explicit type","code":null,"level":"help","spans":[{"file_name":"src/main.rs","byte_start":1052,"byte_end":1052,"line_start":44,"line_end":44,"column_start":20,"column_end":20,"is_primary":true,"text":[{"text":" .map_err(|e| DogError::ImageBytes(e.to_string()))?;","highlight_start":20,"highlight_end":20}],"label":null,"suggested_replacement":": /* Type */","suggestion_applicability":"HasPlaceholders","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[1m\u001b[91merror[E0282]\u001b[0m\u001b[1m: type annotations needed\u001b[0m\n \u001b[1m\u001b[94m--> \u001b[0msrc/main.rs:44:19\n \u001b[1m\u001b[94m|\u001b[0m\n\u001b[1m\u001b[94m44\u001b[0m \u001b[1m\u001b[94m|\u001b[0m .map_err(|e| DogError::ImageBytes(e.to_string()))?;\n \u001b[1m\u001b[94m|\u001b[0m \u001b[1m\u001b[91m^\u001b[0m \u001b[1m\u001b[94m-\u001b[0m \u001b[1m\u001b[94mtype must be known at this point\u001b[0m\n \u001b[1m\u001b[94m|\u001b[0m\n\u001b[1m\u001b[96mhelp\u001b[0m: consider giving this closure parameter an explicit type\n \u001b[1m\u001b[94m|\u001b[0m\n\u001b[1m\u001b[94m44\u001b[0m \u001b[1m\u001b[94m| \u001b[0m .map_err(|e\u001b[92m: /* Type */\u001b[0m| DogError::ImageBytes(e.to_string()))?;\n \u001b[1m\u001b[94m|\u001b[0m \u001b[92m++++++++++++\u001b[0m\n\n"}
{"$message_type":"diagnostic","message":"aborting due to 4 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"\u001b[1m\u001b[91merror\u001b[0m\u001b[1m: aborting due to 4 previous errors\u001b[0m\n\n"}
{"$message_type":"diagnostic","message":"Some errors have detailed explanations: E0282, E0599.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"\u001b[1mSome errors have detailed explanations: E0282, E0599.\u001b[0m\n"}
{"$message_type":"diagnostic","message":"For more information about an error, try `rustc --explain E0282`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"\u001b[1mFor more information about an error, try `rustc --explain E0282`.\u001b[0m\n"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a7d6ed38d601e81f
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rustc":4758242423518056681,"features":"[]","declared_features":"[]","target":11594121305954317235,"profile":8731458305071235362,"path":4942398508502643691,"deps":[[274201027816063771,"ureq",false,12734319416104825687],[10681258086952200236,"image",false,10679758156299839824],[12832915883349295919,"serde_json",false,473165457653994493],[13548984313718623784,"serde",false,16946670578763066210]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/Rust_Enum-dba843948126e3bc/dep-bin-Rust_Enum","checksum":false}}],"rustflags":[],"config":2069994364910194474,"compile_kind":0}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file has an mtime of when this was started.
Loading