To start this thread - I recently started learning Rust. I am coming from Java, so well ... it is something different.
So currently I am trying to build an web app (for displaying the stuff I am retrieving from an API) and I am using leptos. So the 'lib part' of it I moved into another workspace and it compiles but I am not soo sure if I am not doing something wrong with the mod thingy in Rust. So I want to use the lib crate in another workspace from which a binary should be created.
It says 'use of undeclared crate or module 'xy''
So the general structure is the following:
/home/wuel/Development/Rust
βββ xy (Library project)
βββ Cargo.toml
βββ Cargo.lock
βββ src
βββ app
β βββ mod.rs
βββ lib.rs
βββ xz (Binary project)
βββ Cargo.toml
βββ Cargo.lock
βββ src
βββ main.rs
/home/wuel/Development/Rust/craftlib/src/app/
app.rs
use leptos::*;
use leptos_dom::helpers::event_target_value;
use wasm_bindgen_futures::spawn_local;
use gloo_net::http::Request;
use leptos::prelude::signal;
use leptos::prelude::Get;
use leptos::prelude::Set;
use leptos::prelude::ElementChild;
use leptos::prelude::ClassAttribute;
use leptos::prelude::OnAttribute;
#[component]
pub fn App() -> impl IntoView {
// ZustΓ€nde fΓΌr Eingabe und Ausgabe
let (sheet, set_sheet) = signal("Item".to_string());
let (fields, set_fields) = signal("ID,Name".to_string());
let (response, set_response) = signal("".to_string());
let fetch_data = move |_| {
let sheet_value = sheet.get();
let fields_value = fields.get();
spawn_local(async move {
let url = format!(
"https://v2.xivapi.com/api/sheet/{}?fields={}",
sheet_value, fields_value
);
// Using gloo-net instead of reqwest
match Request::get(&url).send().await {
Ok(resp) => {
let text = resp.text().await.unwrap_or_else(|_| "Fehler beim Abrufen".to_string());
set_response.set(text);
}
Err(_) => set_response.set("Fehler beim Abrufen".to_string()),
}
});
};
view! {
<div class="container">
<h1>"XIVAPI Tester"</h1>
<label>"Sheet Name:"</label>
<input type="text"
value={move || sheet.get()}
on:input=move |e| {
set_sheet.set(event_target_value(&e))
} />
<label>"Fields (optional):"</label>
<input type="text"
value={move || fields.get()}
on:input=move |e| {
set_fields.set(event_target_value(&e))
} />
<button on:click=fetch_data>"Abrufen"</button>
<pre>{ move || response.get() }</pre>
</div>
}
}
mod.rs:
pub mod app;
Crago.toml (craftlib):
[package]
name = "craftlib"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/app/app.rs"
crate-type = ["cdylib"]
[dependencies]
mio = { version = "1.0.3" }
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
wasm-bindgen-futures = "0.4"
leptos = { version = "0.7.8", features = ["csr"] }
gloo-net = "0.6.0"
/home/wuel/Development/Rust/crafting/src/
use leptos::*;
use leptos::mount::mount_to_body;
use craftlib::app::App;
fn main() {
mount_to_body(|| view! { <App/> });
}
Cargo.toml (crafting):
[package]
name = "crafting"
version = "0.1.0"
edition = "2021"
[package.metadata.leptos]
bin-package = "crafting"
lib-package = "craftlib"
style-file = "style/main.css"
assets-dir = "assets"
[dependencies]
craftlib = { path = "/home/wuel/Development/Rust/craftlib" }
mio = { version = "1.0.3" }
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
wasm-bindgen-futures = "0.4"
leptos = { version = "0.7.8", features = ["csr"] }
gloo-net = "0.6.0"
[[bin]]
name = "crafting"
path = "src/main.rs"
Output on cargo build:
wuel@localhost:~/Development/Rust/crafting> cargo build
Compiling crafting v0.1.0 (/home/wuel/Development/Rust/crafting)
error[E0433]: failed to resolve: use of undeclared crate or module `craftlib`
--> src/main.rs:3:5
|
3 | use craftlib::app::App;
| ^^^^^^^^ use of undeclared crate or module `craftlib`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `crafting` (bin "crafting") due to 1 previous error
wuel@localhost:~/Development/Rust/crafting>
'
When I do cargo build after a clean it also say Compiling craftlib before the Compiling of crafting fails.
I already spent some hours of trying stuff but I have no clue really - I wanted not to make an own crate for it but well leptos forced me a bit since a project can't be the bin and lib at the same time.
Thanks in advance