2

I'm very new to rust's syntax of using libraries. Well, mostly new to rust overall.

I've included a library, that is unfinished, and seemingly doesn't work. The library is called "hours", the lib.rs contains the following:

// #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Hours {
    pub rules: Vec<types::RuleSequence>,
    pub tz: Tz,
}

impl Hours {
    pub fn from(s: &str, tz: Tz) -> Result<Self, String> {
        //... abbreviated
    }

    pub fn at(self: &Self, dt: &DateTime<Tz>) -> types::Modifier {
        //... abbreviated
    }
}

It is included in Cargo.toml: relevant lines:

edition = "2018"

[dependencies]
hours = "0.0.1"

I want to know if it is possible to include and use the from() function, so far, I'm without luck. Here's what I tried:

use hours;
fn main() {
   //... abbreviated
   let hours = Hours::from(example_hrs, Amsterdam).unwrap();
}

Gives compile error: Hours::from(example_hrs, Amsterdam).unwrap(); ^^^^^ use of undeclared type or moduleHours``

use hours::Hours;
fn main() {
   //... abbreviated
   let hours = Hours::from(example_hrs, Amsterdam).unwrap();
}

Gives compile error: use hours::Hours; ^^^^^^^^^^^^ no ``Hours`` in the root

use hours;
fn main() {
   //... abbreviated
   let hours = hours::Hours::from(example_hrs, Amsterdam).unwrap();
}

Gives compile error: hours::Hours::from(example_hrs, Amsterdam).unwrap(); ^^^^^ could not findHoursinhours``

Is there any way to include and use this library? Do I need to change the library, or am I simply using it wrong?

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
berkes
  • 26,996
  • 27
  • 115
  • 206
  • 1
    How are you adding that library to your project? Are you adding it to `Cargo.toml`? If so, can you post the relevant lines? If instead you are adding the source directly to your crate, can you post your directory tree structure? – rodrigo Mar 11 '20 at 09:50
  • As included in the edit, it is defined in Cargo.toml as dependency, @rodrigo. – berkes Mar 11 '20 at 10:07
  • 2
    @berkes https://docs.rs/crate/hours/0.0.1/source/ this is the source of `hours` in crates.io, looks like the code in the gitlab has not published. So either you can use alternative crate or download the code from the gitlab to use it as local crate, this might help: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#testing-a-bugfix – Ömer Erden Mar 11 '20 at 10:15
  • @ÖmerErden: I'm not sure if I understand you correct. Are you saying that the **version** recieved through crates.io is not the same as on gitlab? Because I did find the gitlab url through crates.io/create/hours, so it is the same project. – berkes Mar 11 '20 at 10:51
  • @berkes I haven't published any crate to crates.io but according to doc, `repository` is specified in here https://doc.rust-lang.org/cargo/reference/publishing.html#before-publishing-a-new-crate, – Ömer Erden Mar 11 '20 at 11:00
  • And here https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata, you can find the explanation of how these metadatas will be used. _not necessarily compatible # with VCS tools and the like._ this part is important. You can see popular crates as well, they point their github main page which points master(not the current version's tag). – Ömer Erden Mar 11 '20 at 11:03
  • @ÖmerErden you pointed me in the right direction: indeed the version deployed to crates.io was simply empty. Using `hours = { git = "https://gitlab.com/alantrick/hours.git" }` solved the issue and allows me to use the library. Could you make this into an answer so I can accept it? – berkes Mar 11 '20 at 11:37

1 Answers1

3

Problem in here, the code in repository link you've shared doesn't match with the dependency in crates.io and naturally Rust can't find the required api components. In this case owner of the crate hasn't published the code in gitlab yet.

To see that you can quickly check the source from the docs.rs. This is the link for the required dependency docs.rs/crate/hours/0.0.1/source/.

If you want to use the current code in the repository

  • you may have it locally by downloading(or using git clone) then you can use it by specifying the path in cargo.toml
  • Or define git repository directly in cargo toml.
hours = { git = "https://gitlab.com/alantrick/hours.git", rev="7b7d369796c209db7b61db71aa7396f2ec59f942"}

Adding revision number or tag might help since the updates on master branch may break the compatibility.


Why this source in docs.rs is accurate with crates.io ?

Please check the about section in docs.rs :

Docs.rs automatically builds crates' documentation released on crates.io using the nightly release of the Rust compiler

This means it is synchronized with crates.io.

To be sure you can also check the crate's source from a local repository cache.

## Note that this path is built with default cargo settings
$HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/hours-0.0.1
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^ github reigstry for crates io 

Why crate's repository link in crates.io doesn't match with the crate's source

Please check the publishing crate reference, repository information is specified in package's metadata(in cargo.toml).

According to package metadata reference these information are used for :

These URLs point to more information about the package. These are intended to be webviews of the relevant data, not necessarily compatible with VCS tools and the like.

documentation = "..."
homepage = "..."
repository = "..."

You may also check the popular crates as well, they point their github(usually) main page which points master branch, not a tag of the current version.

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
  • Really good answer! If you want to use a tag would you specify that under rev in the cargo.toml ? – Peter I Dec 16 '22 at 13:58
  • @PeterI `tag` is the correct key for the job. Please follow the link for more information : https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories – Ömer Erden Dec 16 '22 at 20:08