#Confusion over Modules

6 messages · Page 1 of 1 (latest)

calm ermine
#

Hi all, I'm a little bit confused about how the module system works. I have a relatively default Tauri 2.0 project. In the src-tauri/src dir I have:
src
├── config.rs
├── lib.rs
├── main.rs
└── page_sources
├── mock_serial.rs
└── mod.rs

In my lib.rs, I have a public struct:

#[derive(Default)]
pub struct CurrentPages {
    pub pages: Vec<Page>,
}

In my config.rs, I have another public struct:

use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Config {
    pub blahblahblah: String,
}

In my mod.rs:

pub mod mock_serial;

use serde::Serialize;
use std::time::SystemTime;

pub use crate::page_sources::mock_serial::*;

pub trait PageProvider {
    fn start(&self, app_handle: tauri::AppHandle) -> ();
}

#[derive(Clone, Serialize)]
pub struct Page {
  pub blahblahblah: String,
}
#

Now, I thought that I could use my config struct in the mock_serial.rs file by using the following:

use crate::config;

... which works! I get an error for not having imported CurrentPages from my lib.rs which my mock_serial.rs file needs to use. The cargo check output:

❯ cargo check
...
error[E0412]: cannot find type `CurrentPages` in this scope
  --> src/page_sources/mock_serial.rs:48:54
   |
48 |             let page_lock = app_handle.state::<Mutex<CurrentPages>>();
   |                                                      ^^^^^^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
 1 + use crate::CurrentPages;

Excellent, so I follow the compiler suggestion and add use crate::CurrentPages;

Suddenly, the config import breaks:

error[E0432]: unresolved import `crate::config`
 --> src/page_sources/mock_serial.rs:7:5
  |
7 | use crate::config;
  |     ^^^^^^^^^^^^^ no `config` in the root

error[E0432]: unresolved import `crate::CurrentPages`
 --> src/page_sources/mock_serial.rs:8:5
  |
8 | use crate::CurrentPages;
  |     ^^^^^^^^^^^^^^^^^^^ no `CurrentPages` in the root
  |
help: consider importing this struct instead
  |
8 - use crate::CurrentPages;
8 + use projectname_lib::CurrentPages;
  |

If I were to follow it's suggestion again:

error[E0432]: unresolved import `projname_lib`
 --> src/page_sources/mock_serial.rs:8:5
  |
8 | use projname_lib::CurrentPages;
  |     ^^^^^^^^^^^ use of unresolved module or unlinked crate `projname_lib`

This has really confused me on how the module system works.

glad stag
#

You probably added a mod for the same file in both lib.rs and main.rs

#

Every module should have only one mod statement for it

calm ermine
#

Wow that's it... out of curiosity, would it be better practice to have the mod statement in main.rs or lib.rs?

glad stag