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

Reading single line from file #58

Closed
wildwestrom opened this issue Nov 26, 2022 · 5 comments
Closed

Reading single line from file #58

wildwestrom opened this issue Nov 26, 2022 · 5 comments

Comments

@wildwestrom
Copy link

wildwestrom commented Nov 26, 2022

Is it possible to do this with figment?
If so, where should I be looking?

@SergioBenitez
Copy link
Owner

Can you be more specific about what you want to do? An example would be great.

@wildwestrom
Copy link
Author

wildwestrom commented Nov 26, 2022

Something like this.

fn build_cli() -> Result<Cli> {
	let mut cli = Figment::new().merge(Serialized::defaults(Cli::parse()));
	if let Some(path) = CONFIG_FILE_PATH.as_ref() {
		cli = cli.merge(Data::<MyOwnProviderType>::file(path.as_path()));
	}
	Ok(cli.extract()?)
}

So what if I want a custom figment::providers::Data?

The file I would be looking at might look something like this:

65535

Or this:

long-option = 465
another-long-option = "Hello"

@SergioBenitez
Copy link
Owner

It's not clear from your example which line should be read, what should happen if the file is empty, and how the line should be parsed. But in any case, yes it would be straightforward to implement such a provider. Alternatively, you can simply read the line from the file using the lines() I/O reader and pass the string to any existing provider.

@SergioBenitez
Copy link
Owner

I see you've added to your previous comment. It sounds like you want a custom provider, since your file doesn't seem to have any obvious relation to existing formats. It's not clear what the file should be parsed as, or which line you want to keep and what that line means.

The figment docs are complete. See Format for what you probably need.

@wildwestrom
Copy link
Author

Yeah, I haven't figured out any specifics in terms of how things will be parsed. I just wanted to find out how to extend it. Thank you.

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