Access function from a different module in rust - rust

If I have the following:
├── Cargo.lock
├── Cargo.toml
├── main
│   ├── Cargo.toml
│   └── src
│   └── main.rs
├── module2
│   ├── Cargo.toml
│   └── src
│   ├── lib.rs
│   └── builder.rs
Where the Cargo.toml file in the root is the following:
[workspace]
members = [
"main",
]
I want to access a function from builder.rs in main when testing(i.e. cfg(test)), how can I do so?
Module2 is a library(it was created by running cargo new module2 --lib.
I tried the following:
// module2/builder.rs
pub fn build() { /*...*/ }
// module2/lib.rs
#[cfg(test)]
mod mock;
#[cfg(test)]
pub use mock::build;
// main/Cargo.toml
// ...
[dependencies]
module2 = { path = "../module2" }
// main.rs
#[cfg(test)]
use module2::build;
/*
...
*/
This doesn't work and I get the following error:
error[E0432]: unresolved import `module2::build`
--> main/src/main.rs:3:5
|
3 | use module2::build;
| ^^^^^^^^^^^^^^ no `build` in the root

test of module1 is not test of main: each crate gets cfg(test) turned on only when it itself is being tested, not when a dependency of it is being tested.
You can use cfg(debug_assertions) as an approximation or a custom feature.

Related

Use rust cargo to run tests in workspace root

I've got the following rust project layout:
project_name
├── crate_1
│ ├── src
│ │ ...
│ │ └── main.rs
│ └── Cargo.toml
├── crate_2
│ ├── src
│ │ ...
│ │ └── lib.rs
│ └── Cargo.toml
├── tests
│ └── tests.rs <-- run tests in here
└── Cargo.toml
I want to run the tests in the tests directory using cargo, however cargo can't seem to find them.
Is there a way to get cargo to run them?
tokio is a very good example.
Now you already have a tests directory, let's add it to the members in workspace Cargo.toml.
[workspace]
members = [
"crate1",
"crate2",
"tests"
]
We assume that there are two integration test files, test_crate1.rs and test_crate2.rs under the tests directory.
Create a Cargo.toml under the tests directory with these contents:
[package]
name = "tests"
version = "0.1.0"
edition = "2021"
publish = false
[dev-dependencies]
crate1 = { path = "../crate1" }
crate2 = { path = "../crate2" }
[[test]]
name = "test_crate1"
path = "test_crate1.rs"
[[test]]
name = "test_crate2"
path = "test_crate2.rs"
Run cargo test in workspace directory to check it.

VS code ignores index.d.ts file

I have two projects: my-lib a Vue 3 library and my-project a Vue 3 project that uses this library.
My-lib:
I can compile the library and compile its declaration files. This what I have in the dist folder of the library:
./dist
├── demo.html
├── index.common.js
├── index.common.js.map
├── index.umd.js
├── index.umd.js.map
├── index.umd.min.js
├── index.umd.min.js.map
├── src
│   ├── components
│   │   └── book
│   │   ├── BookToolBar.d.ts
│   │   └── BookToolBar.d.ts.map
│   ├── index.common.d.ts
│   ├── index.common.d.ts.map
│   └── shared
│   └── security
│   ├── AuthenticationResult.d.ts
│   └── AuthenticationResult.d.ts.map
└── tsconfig.tsbuildinfo
This is a piece from package.json of the library:
"name": "my-lib",
"version": "0.1.0",
"private": true,
"files": [
"dist/**.*"
],
"main": "dist/index.common.js",
"unpkg": "dist/index.umd.min.js",
"types": "dist/src/index.common.d.ts",
This is a dist/src/index.common.d.ts file:
export { BookToolBar } from "#/components/book/BookToolBar";
export { AuthenticationResult } from "#/shared/security/AuthenticationResult";
My-project:
The problem is that VS code ignores my declaration files in the following code:
import { AuthenticationResult } from "my-lib";
class AuthenticationResultChild extends AuthenticationResult {...}
However, the following code works fine:
import { AuthenticationResult } from "my-lib/dist/src/shared/security/AuthenticationResult";
class AuthenticationResultChild extends AuthenticationResult {...}
Could anyone say, how to make VS code work with declarations using the first variant (import { AuthenticationResult } from "my-lib")?

Undefined modules inside require() for requirejs

Below are my codes
// config
requirejs.config({
paths: {
jquery: 'library/jquery',
jsBarcode: 'library/jsBarcode.all.min',
q: 'library/q.min',
},
shim: {
jsBarcode: {
deps: ['jquery'],
export: 'JsBarcode',
},
}
});
// Main entry
require(['jquery', 'q', 'jsBarcode'], function (j, q, barcode) {
window.Q = q;
console.log(barcode); // get undefined
});
Directory layout
└── webcontroller
├── bootstrap.min.css
├── image
│   └── load_trans.gif
├── scripts
│   ├── library
│   │   ├── jquery.js
│   │   ├── jsBarcode.all.min.js
│   │   └── q.min.js
│   ├── main.js
│   ├── promise_factory.js
│   ├── require.js
│   └── view.js
└── style.css
Loading sequences
The order for loading scripts is under my expectations.
Problems
However, barcode is undefined all the time.
Any one have ideas about this problem?
Updated
However, below codes can dump something out....
console.log(JsBarcode);
Fix the typo export -> exports, it must be the root cause. Also you have three excessive commas although it seems not to cause troubles. Finally there must be some global JsBarcode defined in jsBarcode.all.min.js that's why your console.log dumps it.
jsBarcode: {
deps: ['jquery'],
exports: 'JsBarcode'
}

CMake: fail import external library

I've this error that I receive when I try to compile:
Linking CXX executable /home/atv/catkin_ws/devel/lib/atvAcrosser/main**
CMakeFiles/main.dir/src/sendToCAN.cpp.o: In function `sendCAN()':
sendToCAN.cpp:(.text+0x432): undefined reference to `sendCanMessage(CanMsg*)'
collect2: ld returned 1 exit status
make[2]: *** [/home/atv/catkin_ws/devel/lib/atvAcrosser/main] Error 1
make[1]: *** [atvAcrosser/CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
Invoking "make" failed
Main call the thread sendCAN [inside sendToCAN.cpp] who calls the function sendCanMessage in the external library libARV6005.a.
This is the CMakeList.txt:
include_directories(include ${catkin_INCLUDE_DIRS})
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/atvAcrosser/lib/)
add_executable(main src/main.cpp src/setupPacketProtocol.cpp src/sendToCAN.cpp)
target_link_libraries(main ${catkin_LIBRARIES} ARV6005)
This is the tree:
src
├── atvAcrosser
│   ├── CMakeLists.txt
│   ├── include
│   │   └── atvAcrosser
│   │   ├── ARV6005Lib.h
│   │   ├── localPlannerCommunication.h
│   │   ├── receiveFromCAN.h
│   │   └── setupPacketProtocol.h
│   ├── lib
│   │   └── libARV6005.a
│   ├── package.xml
│   ├── src
│   │   ├── localPlannerCommunication.cpp
│   │   ├── main.cpp
│   │   ├── sendToCAN.cpp
│   │   ├── setupPacketProtocol.cpp
sendToCAN.h
// included dependencies
#include "ARV6005Lib.h"
//=================================
// function
void sendCAN();
sendToCAN.cpp
#include "../include/atvAcrosser/sendToCAN.h"
...
void sendCAN()
{
struct CanMsg msg;
struct CanMsg msg;
int i, result;
memset((void *)&msg, 0, sizeof msg);
msg.id = 0x33;
msg.id_type = STD_ID;
msg.length = 8;
for(i=0; i<8; i++) {
msg.data[i] = i;
}
result = sendCanMessage(&msg);
}
but with this Makefile in a tutorial example it works
all: main.c
gcc -w -o TestUtility -D_REENTRANT -I../Library -pthread ./main.c ./libARV6005.a
Check that sendCanMessage(CanMsg*) is declared in ARV6005Lib.h
Check that libARV6005.a contains symbol sendCanMessage
If symbol sendCanMessage is not in libARV6005.a, find where it is and link your executable with it.
Run make VERBOSE=1, extract link command and post it here
In your CMakeLists.txt you are missing _REENTRANT and pthread. Use target_compile_definitions and target_link_libraries to add it.
The ARV6005Lib.a was written in C.
The sendToCAN.cpp has been written in Cpp.
So was sufficient
edit the file that include ARV6005Lib.h:
extern "C" {
#include "../include/atvAcrosser/ARV6005Lib.h"
}

rust file in package cannot find package name when using 'use extern PACKAGE_NAME'

├── Cargo.lock
├── Cargo.toml
├── src
│   ├── model.rs
│   └── lib.rs
└── examples
└── client.rs
What my cargo.toml looks like:
[package]
name = "swagger_client"
version = "1.0.0"
authors = []
description = "example test"
In client.rs I try to say:
extern crate swagger_client;
use swagger_client::{testing_struct,alpha,beta};
However "extern crate swagger_client;" causes an error.
src/lib.rs
can't find crate for `swagger_client`
can't find crate rustc(E0463)
client.rs(3, 1): can't find crate

Resources