Skip to content

mauro-balades/Confy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Confy Configure projects effortlessly! 💻

This a project configuration file parser and system originally created for the Snowball Programming Language!

Issues

Features

  • Header only C++ library (bringing fast parsing times)
  • Statically types (with syntax previously defined by the project executor)
  • Enables efficient memory management through smart pointers
  • Cross-platform compatibility for versatile deployment
  • Comprehensive error handling for robust development

Show Me The Syntax!

  auto root = confy::Interface::create({
    {"project", confy::Type::Object({
        {"name", confy::Type::String},
        {"version", confy::Type::String},
        {"authors", confy::Type::Array(confy::Type::String)},
        {"description", confy::Type::String},
    })},
  });

  auto result = confy::parse_file(root, "./project.confy");
  // { errors: {}, root: ... }

Will be able to parse:

# haha, you cant see me!

project {
    name: "MyLib";
    version: "1.0.0";
    author: ["Your Name"];
    description: "A project created with Confy!";
}

Data Types

Object

  • A set of values of values that can be accessed by name
Types::Object({
  {"name": Type},
  ...
});
  myObject: {
    name: type
  };

Array

  • A set of values with the same type that can contain an infinite ammount of elements (from 0 to infinity)
Types::Array(Type);
  myArray: ["Hello", "Adios", "Ñog"];

String

  • An array of characters (it's just a string)
Types::String;
  myName: "mauro!";

Number

  • A number from -inf to inf that can contain decimal places (represented as a double in the backend)
Types::Number;
  myNumber: 25;

Utility Types

  • Utility types are used to validate the given data.
  • To enable the utility classes, define the CONFY_USE_UTILS macro before including confy.hpp
  • If the validation returns an error, it will be thrown and the parsing will fail

MinNumType

  • A number that must be bigger than N1
MinNumType<10>::create();
  myNumber: 12;

MaxNumType

  • A number that must be less than N1
MaxNumType<10>::create();
  myNumber: 6;

RangeNumType<int N1, int N2>

  • A number that must be bigger than N1and less than N2
RangeNumType<10, 20>::create();
  myNumber: 12;

MinStrType

  • The parsed string's length must be bigger than S1
MinStrType<3>::create();
  myString: "hello there";

StrRegexType

  • The parsed string's must satisfy the given regex
StrRegexType::create("*");
  myString: "I dont know how regex works";

Custom Validation Types

  • To create your own validation type, create a new class inheriting from a primitive type (like NumType or StringType).
  • Override the validate method where it's value may depend from which type is being inherited from.

Example of a class that only accepts if a string starts with "hello".

class MyCustomType : public confy::StringType {
public:
  // Validate takes a `double` if it inherits from `confy::NumType`!
  std::optional<std::string> validate(const std::string& value) const override {
    if (s.rfind("hello", 0) == 0) { 
      return std::nullopt;
    }
    return "String must start with 'hello'!";
  }

  // Optional but it's nice to have a generator function
  static std::shared_ptr<MyCustomType> create() {
    return std::make_shared<MyCustomType>();
  }
}

Usage

  {"myStr": MyCustomType::create()}

This will work as (ignore the duplicate name error):

  myStr: "hello world";

  myStr: "hello mauro";

  myStr: "goodbye :("; # error!

Releases

No releases published

Packages

No packages published