diff --git a/README.md b/README.md index a8fa777..06ce140 100644 --- a/README.md +++ b/README.md @@ -530,7 +530,12 @@ vector_store.add_item( ) ``` -#### Method 2: Adding Multiple Examples from JSON Files +### Method 2: Adding Multiple Examples from JSON Files + +First, ensure you have the required dependencies: +```bash +pip install qdrant-client openai +``` Place JSON files in the appropriate directories: @@ -551,8 +556,7 @@ Format for project examples (with optional `project_files` field): ``` Format for error examples: - -``` +```json { "error": "Rust compiler error message", "solution": "How to fix the error", @@ -620,3 +624,5 @@ Licensed under [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html). + + diff --git a/data/error_examples/borrow_checker_error.json b/data/error_examples/borrow_checker_error.json new file mode 100644 index 0000000..6b0a519 --- /dev/null +++ b/data/error_examples/borrow_checker_error.json @@ -0,0 +1,6 @@ +{ + "error": "error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable", + "solution": "Move the immutable borrow into a separate scope to ensure it ends before the mutable borrow begins", + "context": "This error occurs when trying to mutably borrow a value while an immutable borrow is still active. The Rust borrow checker prevents this to ensure memory safety.", + "example": "// Before (error)\nfn main() {\n let mut v = vec![1, 2, 3];\n let first = &v[0];\n v.push(4); // Error: cannot borrow `v` as mutable\n println!(\"{}\", first);\n}\n\n// After (fixed)\nfn main() {\n let mut v = vec![1, 2, 3];\n {\n let first = &v[0];\n println!(\"{}\", first);\n } // immutable borrow ends here\n v.push(4); // Now it's safe to borrow mutably\n}" +} \ No newline at end of file diff --git a/data/error_examples/missing_lifetime.json b/data/error_examples/missing_lifetime.json new file mode 100644 index 0000000..b901f8f --- /dev/null +++ b/data/error_examples/missing_lifetime.json @@ -0,0 +1,6 @@ +{ + "error": "error[E0106]: missing lifetime specifier", + "context": "This error occurs when you return a reference from a function without specifying a lifetime parameter", + "solution": "Add a lifetime parameter to the function signature and any references that need it", + "example": "// Before (error)\nfn longest(x: &str, y: &str) -> &str {\n if x.len() > y.len() { x } else { y }\n}\n\n// After (fixed)\nfn longest<'a>(x: &'a str, y: &'a str) -> &'a str {\n if x.len() > y.len() { x } else { y }\n}" +} \ No newline at end of file diff --git a/data/error_examples/missing_trait_bound.json b/data/error_examples/missing_trait_bound.json new file mode 100644 index 0000000..a3ab1da --- /dev/null +++ b/data/error_examples/missing_trait_bound.json @@ -0,0 +1,6 @@ +{ + "error": "error[E0277]: the trait bound `T: std::fmt::Display` is not satisfied", + "solution": "Add a trait bound to the generic parameter to ensure it implements the required trait", + "context": "This error occurs when using a generic type with a function that requires specific traits, but the generic parameter doesn't specify those trait bounds.", + "example": "// Before (error)\nfn print_item(item: T) {\n println!(\"{}\", item); // Error: `T` might not implement `Display`\n}\n\n// After (fixed)\nfn print_item(item: T) {\n println!(\"{}\", item); // Now we know T implements Display\n}\n\n// Alternative fix using where clause\nfn print_item(item: T) \nwhere\n T: std::fmt::Display,\n{\n println!(\"{}\", item);\n}" +} \ No newline at end of file diff --git a/data/error_examples/trait_bound.json b/data/error_examples/trait_bound.json new file mode 100644 index 0000000..edd392c --- /dev/null +++ b/data/error_examples/trait_bound.json @@ -0,0 +1,6 @@ +{ + "error": "error[E0277]: the trait bound `T: std::fmt::Display` is not satisfied", + "context": "This error occurs when trying to use a generic type without specifying that it needs to implement a required trait", + "solution": "Add the necessary trait bound to the generic type parameter", + "example": "// Before (error)\nfn print_item(item: T) {\n println!(\"{}\", item);\n}\n\n// After (fixed)\nfn print_item(item: T) {\n println!(\"{}\", item);\n}" +} \ No newline at end of file diff --git a/data/project_examples/calculator.json b/data/project_examples/calculator.json new file mode 100644 index 0000000..6c69df5 --- /dev/null +++ b/data/project_examples/calculator.json @@ -0,0 +1,9 @@ +{ + "query": "Create a command-line calculator in Rust", + "example": "A simple calculator that can add, subtract, multiply and divide numbers", + "project_files": { + "src/main.rs": "use std::io;\n\nfn main() {\n println!(\"Simple Calculator\");\n println!(\"Enter an expression (e.g., 5 + 3):\");\n \n let mut input = String::new();\n io::stdin().read_line(&mut input).expect(\"Failed to read line\");\n \n let parts: Vec<&str> = input.trim().split_whitespace().collect();\n \n if parts.len() != 3 {\n println!(\"Invalid expression! Please use format: number operator number\");\n return;\n }\n \n let a: f64 = match parts[0].parse() {\n Ok(num) => num,\n Err(_) => {\n println!(\"First value is not a valid number\");\n return;\n }\n };\n \n let operator = parts[1];\n \n let b: f64 = match parts[2].parse() {\n Ok(num) => num,\n Err(_) => {\n println!(\"Second value is not a valid number\");\n return;\n }\n };\n \n let result = match operator {\n \"+\" => a + b,\n \"-\" => a - b,\n \"*\" => a * b,\n \"/\" => {\n if b == 0.0 {\n println!(\"Error: Division by zero\");\n return;\n }\n a / b\n },\n _ => {\n println!(\"Unknown operator: {}\", operator);\n return;\n }\n };\n \n println!(\"Result: {} {} {} = {}\", a, operator, b, result);\n}", + "Cargo.toml": "[package]\nname = \"calculator\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n", + "README.md": "# Command-Line Calculator\n\nA simple Rust command-line calculator that supports basic arithmetic operations.\n\n## Usage\n\nRun the program and enter expressions in the format `number operator number`, such as:\n\n```\n5 + 3\n10 - 2\n4 * 7\n9 / 3\n```\n\nSupported operators: +, -, *, /" + } +} \ No newline at end of file diff --git a/data/project_examples/markdown_parser.json b/data/project_examples/markdown_parser.json new file mode 100644 index 0000000..4cc035f --- /dev/null +++ b/data/project_examples/markdown_parser.json @@ -0,0 +1,8 @@ +{ + "query": "How to build a markdown parser in Rust", + "example": "A Rust implementation of a basic markdown parser that handles headers, bold, italic, and links", + "project_files": { + "Cargo.toml": "[package]\nname = \"rust-markdown-parser\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\nregex = \"1.5\"", + "src/main.rs": "use std::fs;\nuse regex::Regex;\n\nstruct MarkdownParser {\n input: String,\n}\n\nimpl MarkdownParser {\n fn new(input: String) -> Self {\n MarkdownParser { input }\n }\n\n fn parse_headers(&self) -> String {\n let header_regex = Regex::new(r\"^(#{1,6})\\s+(.+)$\").unwrap();\n let mut output = self.input.clone();\n\n for line in self.input.lines() {\n if let Some(captures) = header_regex.captures(line) {\n let level = captures.get(1).unwrap().as_str().len();\n let text = captures.get(2).unwrap().as_str();\n let html_header = format!(\"{}\", level, text, level);\n output = output.replace(line, &html_header);\n }\n }\n output\n }\n\n fn parse_bold_italic(&self, html: String) -> String {\n let bold_regex = Regex::new(r\"\\*\\*(.*?)\\*\\*\").unwrap();\n let italic_regex = Regex::new(r\"\\*(.*?)\\*\").unwrap();\n\n let bold_html = bold_regex.replace_all(&html, \"$1\");\n let italic_html = italic_regex.replace_all(&bold_html, \"$1\");\n \n italic_html.into()\n }\n\n fn parse_links(&self, html: String) -> String {\n let link_regex = Regex::new(r\"\\[([^\\]]+)\\]\\(([^\\)]+)\\)\").unwrap();\n link_regex.replace_all(&html, \"$1\").into()\n }\n\n fn parse(&self) -> String {\n let headers_html = self.parse_headers();\n let formatted_html = self.parse_bold_italic(headers_html);\n let final_html = self.parse_links(formatted_html);\n final_html\n }\n}\n\nfn main() {\n let markdown = fs::read_to_string(\"input.md\").expect(\"Could not read file\");\n let parser = MarkdownParser::new(markdown);\n let html = parser.parse();\n fs::write(\"output.html\", html).expect(\"Could not write file\");\n}" + } +} \ No newline at end of file diff --git a/data/project_examples/web_server.json b/data/project_examples/web_server.json new file mode 100644 index 0000000..84785cd --- /dev/null +++ b/data/project_examples/web_server.json @@ -0,0 +1,8 @@ +{ + "query": "How to build a basic HTTP server in Rust", + "example": "A simple Rust HTTP server that can serve static files and handle basic routes", + "project_files": { + "Cargo.toml": "[package]\nname = \"rust-http-server\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\ntokio = { version = \"1\", features = [\"full\"] }\nhyper = { version = \"0.14\", features = [\"full\"] }\nhttp-body = \"0.4\"\nmime = \"0.3\"", + "src/main.rs": "use hyper::{Body, Request, Response, Server, Method, StatusCode};\nuse hyper::service::{make_service_fn, service_fn};\nuse std::convert::Infallible;\nuse std::net::SocketAddr;\nuse std::fs;\n\nasync fn handle_request(req: Request) -> Result, Infallible> {\n match (req.method(), req.uri().path()) {\n (&Method::GET, \"/\") => {\n let content = fs::read_to_string(\"index.html\")\n .unwrap_or_else(|_| \"Welcome to Rust HTTP Server\".to_string());\n Ok(Response::new(Body::from(content)))\n },\n (&Method::GET, \"/hello\") => {\n Ok(Response::new(Body::from(\"Hello from Rust HTTP Server!\")))\n },\n (&Method::GET, \"/files\") => {\n let files: Vec = fs::read_dir(\".\")\n .unwrap()\n .filter_map(|entry| {\n entry.ok().and_then(|e| \n e.file_name().into_string().ok()\n )\n })\n .collect();\n \n let response = serde_json::to_string(&files).unwrap();\n \n Ok(Response::new(Body::from(response)))\n },\n _ => {\n let mut response = Response::new(Body::from(\"Not Found\"));\n *response.status_mut() = StatusCode::NOT_FOUND;\n Ok(response)\n }\n }\n}\n\n#[tokio::main]\nasync fn main() {\n let addr = SocketAddr::from(([127, 0, 0, 1], 8080));\n let make_svc = make_service_fn(|_conn| async {\n Ok::<_, Infallible>(service_fn(handle_request))\n });\n\n let server = Server::bind(&addr).serve(make_svc);\n\n println!(\"Server running on http://{}\", addr);\n\n if let Err(e) = server.await {\n eprintln!(\"server error: {}\", e);\n }\n}" + } +} \ No newline at end of file