Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors on config.rs lastest config version: 0.12 #10

Open
igotfr opened this issue Mar 15, 2022 · 3 comments
Open

Errors on config.rs lastest config version: 0.12 #10

igotfr opened this issue Mar 15, 2022 · 3 comments

Comments

@igotfr
Copy link

igotfr commented Mar 15, 2022

config.rs

pub use config::ConfigError;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct ServerConfig {
  pub host: String,
  pub port: i32,
}

#[derive(Deserialize)]
pub struct Config {
  pub server: ServerConfig
}

impl Config {
  pub fn from_env() -> Result<Self, ConfigError> {
    let mut cfg = config::Config::new();
    cfg.merge(config::Environment::new())?;
    cfg.try_into()
  }
}

Cargo.toml

[dependencies]
actix-web = "^4"
actix-rt = "^2"
serde = { version = "^1", features = ["derive"] }
dotenv = "^0.15"
config = "^0.12"
$ cargo check
    Checking todo-actix v0.1.0 (/home/notecomm/prust/todo-actix)
error[E0624]: associated function `new` is private
  --> src/config.rs:22:35
   |
22 |     let mut cfg = config::Config::new();
   |                                   ^^^ private associated function
   |
  ::: /home/notecomm/.cargo/registry/src/github.com-1ecc6299db9ec823/config-0.12.0/src/config.rs:39:5
   |
39 |     pub(crate) fn new(value: Value) -> Self {
   |     --------------------------------------- private associated function defined here

error[E0061]: this function takes 1 argument but 0 arguments were supplied
  --> src/config.rs:22:19
   |
22 |     let mut cfg = config::Config::new();
   |                   ^^^^^^^^^^^^^^^^^^^-- supplied 0 arguments
   |                   |
   |                   expected 1 argument
   |
note: associated function defined here
  --> /home/notecomm/.cargo/registry/src/github.com-1ecc6299db9ec823/config-0.12.0/src/config.rs:39:19
   |
39 |     pub(crate) fn new(value: Value) -> Self {
   |                   ^^^

warning: use of deprecated associated function `config::Config::merge`: please use 'ConfigBuilder' instead
  --> src/config.rs:23:9
   |
23 |     cfg.merge(config::Environment::new())?;
   |         ^^^^^
   |
   = note: `#[warn(deprecated)]` on by default

warning: use of deprecated associated function `config::Environment::new`: please use 'Environment::default' instead
  --> src/config.rs:23:36
   |
23 |     cfg.merge(config::Environment::new())?;
   |                                    ^^^

error[E0271]: type mismatch resolving `<config::Config as TryFrom<config::Config>>::Error == ConfigError`
  --> src/config.rs:24:9
   |
24 |     cfg.try_into()
   |         ^^^^^^^^ expected enum `Infallible`, found enum `ConfigError`

error[E0277]: the trait bound `config::Config: std::convert::From<config::Config>` is not satisfied
  --> src/config.rs:24:9
   |
24 |     cfg.try_into()
   |         ^^^^^^^^ the trait `std::convert::From<config::Config>` is not implemented for `config::Config`
   |
   = note: required because of the requirements on the impl of `Into<config::Config>` for `config::Config`
   = note: required because of the requirements on the impl of `TryFrom<config::Config>` for `config::Config`
   = note: required because of the requirements on the impl of `TryInto<config::Config>` for `config::Config`

Some errors have detailed explanations: E0061, E0271, E0277, E0624.
For more information about an error, try `rustc --explain E0061`.
warning: `todo-actix` (bin "todo-actix") generated 2 warnings
error: could not compile `todo-actix` due to 4 previous errors; 2 warnings emitted
@VibhavSurve09
Copy link

Associated functions you are trying to access are deprecated since 0.12, maybe try using something like below code

use config::Config;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
}

impl ServerConfig {
    pub fn from_env() -> Self {
        let _config = Config::builder()
            .add_source(::config::Environment::default())
            .build()
            .unwrap();
        let config: ServerConfig = _config.try_deserialize().unwrap();
        config
    }
}
[dependencies]
actix-web = "4"
config = "0.13.1"

Reference :
Config Doc https://docs.rs/config/0.13.1/config/index.html,
Official example from actix web https://github.com/actix/examples/blob/master/databases/postgres/src/main.rs

@igotfr
Copy link
Author

igotfr commented Jun 15, 2022

@igotfr
Copy link
Author

igotfr commented Jun 21, 2022

actix/examples#537

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants