Coder Social home page Coder Social logo

qedk / configparser-rs Goto Github PK

View Code? Open in Web Editor NEW
66.0 66.0 21.0 480 KB

A simple configuration parsing utility with no dependencies built on Rust.

Home Page: https://crates.io/crates/configparser

License: Other

Rust 100.00%
config configparser configuration hacktoberfest ini ini-parser parser rust settings

configparser-rs's Introduction

Hi there, I'm QEDK ๐Ÿ‘‹

Twitter Follow GitHub User's stars

  • ๐Ÿ’ป Senior R&D Engineer at Avail
  • ๐ŸŒฑ Iโ€™m currently learning Circom and Noir โŸ 
  • ๐Ÿ’ฌ Ask me about anything!
  • ๐Ÿ“ซ You can reach me via email or twitter
  • ๐Ÿ“– Read my stuff at qedk.xyz
  • โ˜• Buy me a coffee
  • ๐Ÿ˜„ he/him ๐Ÿ•บ
  • โšก Chelsea ๐Ÿ”ต and Jeremy Zucker fan for life!

configparser-rs's People

Contributors

belak avatar charliermarsh avatar grahamking avatar qedk avatar relausen-tlg avatar scottcusa avatar skystar-p avatar swivelgames avatar virxec avatar vthib avatar xboard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

configparser-rs's Issues

2.0.0 crate contains files with a UNIX timestamp of 0, and some files are marked executable

The files in the configparser 2.0.0 crate tarball appear to have a UNIX timestamp of 0:

$ wget -q -O- https://crates.io/api/v1/crates/configparser/2.0.0/download | TZ=UTC tar xvvvz
-rw-r--r-- 0/0              74 1970-01-01 00:00 configparser-2.0.0/.cargo_vcs_info.json
-rwxr-xr-x 0/0              62 1970-01-01 00:00 configparser-2.0.0/.gitignore
-rwxr-xr-x 0/0             262 1970-01-01 00:00 configparser-2.0.0/.travis.yml
-rwxr-xr-x 0/0            2768 1970-01-01 00:00 configparser-2.0.0/CHANGELOG.md
-rw-r--r-- 0/0            1312 1970-01-01 00:00 configparser-2.0.0/Cargo.toml
-rwxr-xr-x 0/0             889 1970-01-01 00:00 configparser-2.0.0/Cargo.toml.orig
-rw-r--r-- 0/0            7650 1970-01-01 00:00 configparser-2.0.0/LICENSE-LGPL
-rw-r--r-- 0/0            1060 1970-01-01 00:00 configparser-2.0.0/LICENSE-MIT
-rwxr-xr-x 0/0            9021 1970-01-01 00:00 configparser-2.0.0/README.md
-rwxr-xr-x 0/0           27086 1970-01-01 00:00 configparser-2.0.0/src/ini.rs
-rwxr-xr-x 0/0            6830 1970-01-01 00:00 configparser-2.0.0/src/lib.rs
-rw-r--r-- 0/0             264 1970-01-01 00:00 configparser-2.0.0/tests/test.ini
-rwxr-xr-x 0/0            7198 1970-01-01 00:00 configparser-2.0.0/tests/test.rs
$

I'm trying to package configparser for debian and this makes the packaging run into the lintian error package-contains-ancient-file. I can work around it in the debian packaging, but I think the better thing would be for the crate itself to set appropriate timestamps internally.

Ini::load should accept a Path instead of str

Hi,
I find it odd that I have to pass an str to Ini::load when I already have a Path, only for configparser to convert the str back to a Path.

While in practice I can work around the limitation, this might create problems in theory since not every Path can be converted to str.

Configparser handles CLI password managers for secure secret's storage?

Hello Sorry this is a question regarding these libraries but also in general, we all know storing secrets on config files is a big NO, I come from C and GO thinking in porting some of my CLI tools to Rust, and all CLI tools I use, and my community demands all have support like for example go (init parser) to call Unix pass or gopass or bitwarden-cli etc. So, your password is never locally in plain text because if your system/server etc. gets an LFI or a RCE or your laptops gets stolen and is not encrypted, or even a half shell, any credentials on your configs will just open the door for laterally or vertically access to other places, so I am looking to see if configparser allows for me to be able to port my GO CLI programs to Rust with secrets security, allowing my users to be able to call password managers from these libs or others, I have been looking around and can't find this project seems the most promising since it has many similarities to the go init parser.
Thanks in advance.

Add a function to append section

Similar to configparser in Python, we should have an option to append sections using a direct hashmap and string.

  • append(Some(sectionname), HashMap to append) - If None, we append the section as-is (validate that it's a two-level hashmap), if Some(...), we create a hashmap with that section and add the entire section (need an error-check to ensure it's a flat hashmap).
  • append_str(...) - Same behaviour as above, we run the underlying stuff through parse() and add some error checks.

Panic when multiline enabled due to unwrap

configparser can cause a panic on write if multiline is enabled and value is an empty string.

thread 'main' panicked at 'called Option::unwrap() on a None value', .../.cargo/registry/src/index.crates.io-6f17d22bba15001f/configparser-3.0.2/src/ini.rs:421:51

How to fetch duplicate keys in section?

Hello, Guys. I have a ini format in this sample:

[my section]
key=A1
key=A2
Key=A3

I want to get section value with duplicate keys like:

config.get("my section","key") will return result with :

{"key":["A1","A2","A3"]},

How can i do it? Thanks.

Add options to keep sections and keys case sensitive.

I have a use case where sections have regular expressions describing file patterns, as such, preserving case sensitivity of sections values is crucial. It would be great if configparser had a configuration option to work in case sensitive mode.

Ini sample:

[([A-Z])\w+Factory.java]
    should_be_singleton = true
    abstract = false

Today I've no need for key values to be case sensitive but this may be useful for others.

Support for a "print()" function?

Hello,

Thank you for this crate! It's been a big help. That said, I was finding myself in the position of needing to extract some sections of an Ini and create a new collection of them, but there didn't appear to be any straightforward way to do so within this crate. So, I took to making my own Print trait and function that accomplished what I was looking for, that ended up looking like:

pub trait Print {
    fn print(&self, header: &String) -> String;
}

impl Print for Ini {
    fn print(&self, header: &String) -> String {
        let mut section = String::new();
        let map = self.get_map_ref();
        let defaults = self.defaults();
        section = format!("[{}]", header);
        for (k, v) in map.get(header).unwrap() {
            section = format!("{}\n{}{}", section, k, v.clone().map(|s| String::from(format!("{}{}", defaults.delimiters[0], s))).unwrap());
        }
        return section;
    }
}

With this, I was able to assemble a new Ini section collection from a larger one like:

let mut i = Ini::new();
for section in data.sections() {
    if ...some conditions... {
                i.read_and_append(data.print(&section));
    }
}

This of course also allows me to print the contents of a specific section to stdout (or wherever). I'm sure this can be done with much better-looking code than I can come up with, but it seems to be doing the job presently. I think, as a feature request, it would be nice for this crate to support this kind of use-case natively.

Thanks!

Support for multi-line values

Enhancement Request

Multi-line value support.

Either the python style ConfigParser:

[Section]
Key1: Value1
Key2: Value Two
Key3: this is a haiku
    spread across separate lines
    a single value
Key4: Four

Or the backslash + linebreak:

[Section]
Key1: Value1
Key2: Value Two
Key3: this is a haiku \
    spread across separate lines \
    a single value
Key4: Four

WikiPedia: Comparison of INI parsers which breaks down which parsers support which style of multi-line support

Add function to set boolean defaults

Currently we have a method getboolcoerce() that allows coercing certain values into bool. We should add a helper function to modify what those values can be.

opening bracket should be valid in non sections ( e.g. passwords)

In general, there is the possibility that passwords could contain some special characters like [ and ]. Therefore you will get an error, if the value contains [. Error: Found opening bracket for section name but no closing bracket.
Would be great to handle such cases.
on line 259 in ini.rs : match trimmed.find('[') {
Wouldnt it be enough to just check the position of the first [ ? Since you trim the line anyway, only check for a closing bracket if it is at position 0.
Or was there a different usecase in mind i havent thought off?

Configurable line endings when serializing

Hello,

Currently, when serializing, configparser-rs always uses unix line endings ('\n', lf). On the python side, configparser will use the appropriate line ending depending on the platform it's currently running on (crlf on windows, lf everywhere else).

I think it'd be nice if configparser would allow configuring the line endings being used, e.g. add an enum to IniDefaults to let the user chose between Lf and Crlf line endings.

Comments are discarded

Hi ๐Ÿ‘‹. I'd like for the config parser to be fully idempotent meaning when I parse a config and immediately write it out again, I'd expect to get the same exact document (with the possible exception of whitespace). Right now, however, comments are ignored so when I read in a config file, make a change, and write it out again, those comments are lost.

Add spacing between sections and option to add space between key and value

As it stands now (unless i'm missing an option somewhere), all sections written to a file are smashed together without the option to add a buffer/space line between them which would be nice.

Additionally, it would be nice to have an option to add a space between the key, the delimiter and the value. For examples...instead of being forced to have it written as:

key=value

have an option to do:

key = value

Both of these are aesthetic features but would be nice for writing more user-friendly files.

Keys are shuffled when loading and saving a INI file

Hello, thanks for your work on this crate.

The sections and the keys are stored in a HashMap, in which ordering is not guaranteed. That causes sections and keys to be shuffled around when saving an INI file that has been loaded, which isn't ideal.

The Python configparser module uses an OrderedDict to avoid this issue. In Rust, an easy solution would be to replace the HashMaps with BTreeMaps to guarantee an ordering, without depending on anything but standard Rust. That wouldn't necessarily be in the same order as loaded, but it would at least prevent a shuffle of the keys on every load/write.

If you are ok with this change, I can write a PR for it.

Add support for `load()`ing multiple files

It would be great to be able to apply files on top of each other, similar to many hierarchical config stacks.

It might look something like:

config.load("/usr/local/software/defaults.ini").unwrap();
config.load_more("/etc/software/global.ini").ok();
config.load_more("~/.config/software/user.ini").ok();

At that point, it wouldn't necessarily matter if one of the files failed to load, because you could rely on the others to be there.

This would make it easy to sort of build a complete configuration.

Currently, I have mechanisms in place to basically load each file individually and then manually aggregate the settings together, which is a bit of a pain to do manually.

Iterate over the keys of a section request.

I have a set of keys in an ini file in once section that I need to apply the same rules to and update the data structure, in memory.

With the current code base I need to know the keys in advance. I would by much nicer if I could do something like:

for key in config.get_keys("section_name") {
//do something here.
}

Then, with the sections() function dumping the data while debugging is also trivial.

`load` or `read` without creating a clone?

Whenever data is read via load or read, the data Ini instance is mutated in place, but a clone of the inner data is returned too. So I end up with two copies of the data.

Why return a clone of the data? HashMap's Clone implementation is recursive, so this ends up copying the entire structure. Am I missing something here?

Is it somehow possible to do something like Init::read, but only get one copy of the data in memory? I don't need two copies. I know that I can just drop the clone, but I don't see the point it copying data just to drop it.

Support sections with no keys

Currently when I read this file:

[section0]

[section1]
key = value

the resulting map (and conf.sections() too) will only contain section1.
section0 will be silently dropped.

I didn't find any way to configure this crate so that sections with no keys are not skipped.

There are some use-cases where one wants to know if section is present (even if empty).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.