Skip to content

Commit

Permalink
Change FileExtensions signature
Browse files Browse the repository at this point in the history
  • Loading branch information
szarykott committed Sep 30, 2021
1 parent c9178cb commit fad912e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
10 changes: 5 additions & 5 deletions examples/async_source/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, error::Error};
use std::{collections::HashMap, error::Error, fmt::Debug};

use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat};
use config::{AsyncSource, ConfigBuilder, ConfigError, FileFormat, Format, builder::AsyncState};

use async_trait::async_trait;
use futures::{select, FutureExt};
Expand Down Expand Up @@ -49,13 +49,13 @@ async fn run_client() -> Result<(), Box<dyn Error>> {
// Actual implementation of AsyncSource can be found below

#[derive(Debug)]
struct HttpSource {
struct HttpSource<F : Format> {
uri: String,
format: FileFormat,
format: F,
}

#[async_trait]
impl AsyncSource for HttpSource {
impl<F : Format + Send + Sync + Debug> AsyncSource for HttpSource<F> {
async fn collect(&self) -> Result<HashMap<String, config::Value>, ConfigError> {
reqwest::get(&self.uri)
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_format/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Format for MyFormat {
// It is only required for File source, custom sources can use Format without caring for extensions
static MY_FORMAT_EXT: Vec<&'static str> = vec![];
impl FileExtensions for MyFormat {
fn extensions(&self) -> &Vec<&'static str> {
fn extensions(&self) -> &'static [&'static str] {
&MY_FORMAT_EXT
}
}
4 changes: 2 additions & 2 deletions src/file/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
/// Since [`Format`](crate::Format) only describes certain internal encoding, for instance JSON or Yaml
/// it is not necessarily bound to file extension name.
///
///In networking context JSONs are used without file extensions.
/// In networking context JSONs are used without file extensions.
/// One can also imagine some encodings that do not necessarily have file extension associated, for instance
/// MessagePack or bincode.
/// Hence the decision to have extensions separated from [`Format`](crate::Format).
pub trait FileExtensions {
/// Returns a vector of file extensions, for instance `[yml, yaml]`.
fn extensions(&self) -> &Vec<&'static str>;
fn extensions(&self) -> &'static [&'static str];
}
11 changes: 3 additions & 8 deletions src/file/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,14 @@ lazy_static! {
}

impl FileFormat {
// TODO: pub(crate)
#[doc(hidden)]
pub fn extensions(&self) -> &'static Vec<&'static str> {
pub (crate) fn extensions(&self) -> &'static [&'static str] {
// It should not be possible for this to fail
// A FileFormat would need to be declared without being added to the
// ALL_EXTENSIONS map.
ALL_EXTENSIONS.get(self).unwrap()
}

// TODO: pub(crate)
#[doc(hidden)]
#[allow(unused_variables)]
pub fn parse(
pub (crate) fn parse(
&self,
uri: Option<&String>,
text: &str,
Expand Down Expand Up @@ -147,7 +142,7 @@ impl Format for FileFormat {
}

impl FileExtensions for FileFormat {
fn extensions(&self) -> &Vec<&'static str> {
fn extensions(&self) -> &'static [&'static str] {
self.extensions()
}
}

0 comments on commit fad912e

Please sign in to comment.