Giter VIP home page Giter VIP logo

config.cr's Introduction

config.cr

Docs GitHub release Build Status

config.cr is a parser for a configuration format designed to be more effective than JSON, YAML, INI, and HJSON. It also inherits aspects from 'OpenBSD style' configs like macros.

# TL;DR
$self = config.cr

json:    Too many quotes
yaml:    Pays attention to whitespace
ini:     No nesting
hjson:   Data oriented. Support?
openbsd: Macros are awesome

/* If only something combined the good
   parts and got rid of the bad */

answer: $self

Installation

Add this to your application's shard.yml:

dependencies:
  config:
    github: chris-huxtable/config.cr

Usage

require "config"

You can parse a file with:

config = Config.file("/path/to/file.conf")

You can access the entries via the Config::Any objects.

config = Config.parse("username: chris, link: https://github.com/chris-huxtable/config.cr")

username = config.as_s("username")
link = config.as_a("link")

Format

Root Object:

The root is considered an object. That is to say you must provide a key for any root entry and you should not wrap entries in {}.

key: "value"

Keys:

Unlike JSON, object keys can be specified without quotes.

rate: 1000

Strings:

Unlike JSON, strings can be specified without quotes as long as it doesn't contain special characters like :, =, {, }, [, ], or ,. Though, :, = are allowed if they are not a key.

rate: This is a string.

Separators:

Unlike JSON, entries can be separated with commas or newlines.

first: 1
second: 2, third: 3

Comments:

There are three types of comments to support the common commenting styles.

Hash Comments:

A to-end-of-line comment initiated by a # symbol. As common in Crystal, Ruby, Python, Shell, Perl, et al.

# hash style comments
# (because it's just one character)

Line Comments:

A to-end-of-line comment initiated by a //. As common in C, Objective-C, C++, D, Swift, PHP, JavaScript, et al.

// line style comments
// (because it's like C/Objective-C/...)

Block Comments:

A block comment initiated by a /* and terminated by */. As common in C, Objective-C, C++, D, Swift, PHP, et al.

/* block style comments because
   it allows you to comment out a block */

Note: This does not yet support nested comments.

Macros:

A macro is a saved value which can be referenced in the rest fo the document. The key must be prefixed with a $ and assigned with a = as opposed to a :.

$defaultName = "Default Name"
$nameKey = name

$defaultObject = {
	first: 1
	second: 2
}

$nameKey: $defaultName
object: $defaultObject

Note: Macro's cannot yet be inserted into strings.

Fault Tolerance:

Recovers from easy to make mistakes. Like useless commas.

first: 1
second: 2,
third: 3,,,

Contributing

  1. Fork it ( https://github.com/chris-huxtable/config.cr/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

config.cr's People

Contributors

chris-huxtable avatar s-gerber avatar

Stargazers

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

Watchers

 avatar  avatar

Forkers

awigg chipper1

config.cr's Issues

The sample from the front page fails

this script

require "config"

config = Config.parse("username: chris, link: https://github.com/chris-huxtable/config.cr")

username = config.as_s("username")
link = config.as_a("link")

fails with the message

Unhandled exception: Cast from String to Array(T) failed, at /Users/emile/github/unicode2latex/lib/config/src/config/any.cr:223:10:223 (TypeCastError)
  from lib/config/src/config/any.cr:223:10 in 'as_a'
  from lib/config/src/config/any.cr:240:10 in 'as_a'
  from tt.cr:6:1 in '__crystal_main'
  from /opt/homebrew/Cellar/crystal/1.10.1/share/crystal/src/crystal/main.cr:129:5 in 'main_user_code'
  from /opt/homebrew/Cellar/crystal/1.10.1/share/crystal/src/crystal/main.cr:115:7 in 'main'
  from /opt/homebrew/Cellar/crystal/1.10.1/share/crystal/src/crystal/main.cr:141:3 in 'main'

using Crystal 1.10.1 (2023-10-13)

Implement Simplified Grammar

Possibly add a new grammar which removes the necessity of { and } where the line is like a path.

Example:

pass: out: quick: on: "em0" from: "192.168.0.2" to: "any"

Instead of:

pass: { 
     out { 
        quick {
            on: "em0"
            from: "192.168.0.2"
            to: "any"
        }
    }
}

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.