rustsn - This Rust-based tool generates, compiles, and tests code using LLMs, resolves dependencies, and provides explanations of existing code through embeddings.
- generate function command is used to generate code snippets based on user-provided explanations.
- TODO: generate application command is used to generate seed project code based on user-provided explanations.
- ask command is used to get explanation by existing codes of your project based on user-provided question.
language | generate function | generate application | ask |
---|---|---|---|
Rust | + | - | + |
JavaScript | + | - | + |
C# | - | - | + |
Python | + | - | - |
TypeScript | + | - | - |
Java | + | - | - |
Kotlin | + | - | - |
Swift | + | - | - |
PHP | + | - | - |
Scala | + | - | - |
Project name "rustsn" is a combination of "Rust" and "Snippet" words. Code snippets are generated by the tool written in Rust language.
- Rust: Ensure you have Rust installed. You can install it from here.
- Make a decision: Use Ollama (free and launched on your machine) or the OpenAI API (paid and launched on OpenAI servers).
- If you choose Ollama: Required for LLM interactions. Install from Ollama's official site.
- Download Ollam models
ollama pull qwen2.5-coder:7b ollama pull bge-large # if your need "ask" command functionality for existed project code
- Set environment variable OLLAMA_NUM_PARALLEL_REQUESTS=2 if you plan launch gemma2:9b and bge-large models in parallel for "ask" command (do not forger to restart your PC)
- If you choose OpenAI API: Create file "token.txt" in the root folder and put your OpenAI API key there.
cargo install rustsn
This command will download the package source from crates.io, build it, and install the binary into the standard Cargo binaries directory ($HOME/.cargo/bin on Unix-like systems, or %USERPROFILE%.cargo\bin on Windows). If PATH variable is correctly configured, you can run the tool from any directory.
-
Start the Program
rustsn generate function --lang=rust
-
Provide an Explanation
The program will prompt:
Explain what the function should do:
Enter a detailed explanation of the function you want to generate.
parse json string and return struct User (age, name)
-
Completion
Once the code compiles and all tests pass, the final code and tests will be displayed and result of work will be saved in
sandbox
folder.
For example:
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)]
struct User {
name: String,
age: u32,
}
fn solution(json_string: &str) -> Result<User, serde_json::Error> {
let user: User = serde_json::from_str(json_string)?;
Ok(user)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_solution() {
let json_string = r#"{"name": "John Doe", "age": 30}"#;
let user = solution(json_string).unwrap();
assert_eq!(user.name, "John Doe");
assert_eq!(user.age, 30);
}
#[test]
fn test_solution_invalid_json() {
let json_string = r#"{"name": "John Doe", "age": }"#;
assert!(solution(json_string).is_err());
}
}
Finished
-
Start the Program
rustsn ask /path/to/your/project --lang=rust
-
Provide an Explanation
The program will prompt:
Enter the question about your project sources:
Enter a question about your project sources.
How work parse function for PDF files?
-
Completion
The program will return the explanation based on the existing code of your project.
Find closest files:
File: ../shiva/lib\src\pdf.rs
...
Answer: The `parse` function for PDF files in the provided Rust code is implemented as part of the `Transformer` struct in the `pdf.rs` file. This function is responsible for converting a PDF document into a `Document` object composed of various `Element` types. Here's a detailed breakdown of how it works:
1. **Load the PDF Document**:
- The function takes a reference to a `Bytes` object, which contains the PDF data.
- It uses the `lopdf` library to load the PDF document from memory using `PdfDocument::load_mem`.
2. **Iterate Through Pages**:
- The function retrieves the pages of the PDF using `pdf_document.get_pages()`.
- It iterates over each page to process its contents.
3. **Process Page Contents**:
- For each page, it retrieves the contents using `pdf_document.get_page_contents(page_id)`.
- It iterates over each content object in the page and calls the `parse_object` function to process it.
4. **Parse Individual Objects**:
- The `parse_object` function is responsible for interpreting the contents of each object in the PDF.
- It decodes text using the `PdfDocument::decode_text` method, manages element types like `List`, `Paragraph`, and `Text`, and handles operations associated with text positioning and font changes (e.g., "Tm", "Tf", "Tj", "TJ", "ET").
5. **Text Collection**:
- The function `collect_text` is used to gather and decode text from PDF objects, considering encoding and operand types.
- It adds decoded text to a string and determines when new elements like lists or paragraphs should be started based on the content.
6. **Construct Document Elements**:
- The function constructs `Element` types such as `Text`, `Paragraph`, and `List`, and adds them to a vector of elements.
- These elements are used to build the final `Document` object, representing the structure and content of the PDF.
7. **Return the Document**:
- After processing all pages and objects, the function returns a `Document` instance containing all the parsed elements.
In summary, the `parse` function for PDF files reads the PDF data, iterates through its pages and content objects, decodes text, and constructs a structured `Document` composed of various elements, which can then be used for further processing or transformation.
I would love to see contributions from the community. If you experience bugs, feel free to open an issue. If you would like to implement a new feature or bug fix, please follow the steps:
- Do fork
- Add comment to the issue that you are going to work on it
- Create pull request
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Rustsn by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
- Add "ask" command for C# and JavaScript languages
- Add "ask" command to get explanation by existing codes of your project based on user-provided question
- Add MIT or Apache-2.0 license
- Add TypeScript language support
- Add Swift language support
- Add Kotlin language support
- Add Python language support
- Add PHP language support
- Add JavaScript language support
- Add Scala language support
- Add Java language support
- Simplify state machine logic and remove logic.md file
- Simplify prompt.txt file
- Add --lang parameter to specify the language of the generated code (Rust, Python, C, JavaScript, Java, TypeScript, CPP, Scala, Kotlin, Swift)
- Make decision to support multi-language code generation: Python, C, JavaScript, Java, TypeScript, CPP, Scala, Kotlin, Swift
- Extract_code function replaced by extract_code, extract_dep, extract_test functions
- Support OpenAI API
- Moved prompts from code to "rust.prompt" file
- Moved logic from code to "logic.md" file
- Code Generation
- Automated Compilation
- Dependency Resolution
- Test Generation
- Error Correction
- Caching Mechanism