Skip to content

Commit

Permalink
Smart constructor for IconThemeReadOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Jun 4, 2016
1 parent ec77f4d commit c10de63
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 54 deletions.
2 changes: 1 addition & 1 deletion dub.selections.json
@@ -1,7 +1,7 @@
{
"fileVersion": 1,
"versions": {
"inilike": "1.0.0-beta",
"inilike": "1.0.0-beta2",
"isfreedesktop": "0.1.0",
"xdgpaths": "0.2.1"
}
Expand Down
4 changes: 2 additions & 2 deletions examples/describe/dub.selections.json
@@ -1,8 +1,8 @@
{
"fileVersion": 1,
"versions": {
"inilike": "1.0.0-beta",
"isfreedesktop": "0.1.0",
"inilike": "1.0.0-beta2",
"xdgpaths": "0.2.1"
}
}
}
2 changes: 1 addition & 1 deletion examples/describe/source/app.d
Expand Up @@ -22,7 +22,7 @@ int main(string[] args)
if (themePath.isDir) {
themePath = buildPath(themePath, "index.theme");
}
iconTheme = new IconThemeFile(themePath, readOptions);
iconTheme = new IconThemeFile(themePath);
} else {
static if (isFreedesktop) {
iconTheme = openIconTheme(themePath, baseIconDirs());
Expand Down
4 changes: 2 additions & 2 deletions examples/findicon/dub.selections.json
@@ -1,8 +1,8 @@
{
"fileVersion": 1,
"versions": {
"inilike": "1.0.0-beta",
"isfreedesktop": "0.1.0",
"inilike": "1.0.0-beta2",
"xdgpaths": "0.2.1"
}
}
}
2 changes: 1 addition & 1 deletion examples/print/dub.selections.json
@@ -1,7 +1,7 @@
{
"fileVersion": 1,
"versions": {
"inilike": "1.0.0-beta",
"inilike": "1.0.0-beta2",
"isfreedesktop": "0.1.0",
"xdgpaths": "0.2.1"
}
Expand Down
4 changes: 2 additions & 2 deletions examples/test/dub.selections.json
@@ -1,8 +1,8 @@
{
"fileVersion": 1,
"versions": {
"inilike": "1.0.0-beta",
"isfreedesktop": "0.1.0",
"inilike": "1.0.0-beta2",
"xdgpaths": "0.2.1"
}
}
}
118 changes: 73 additions & 45 deletions source/icontheme/file.d
Expand Up @@ -266,34 +266,31 @@ protected:
*/
final class IconThemeFile : IniLikeFile
{
/**
* Policy about reading extension groups (those start with 'X-').
*/
enum ExtensionGroupPolicy : ubyte {
skip, ///Don't save extension groups.
preserve ///Save extension groups.
}

/**
* Policy about reading groups with names which meaning is unknown, i.e. it's not extension nor relative directory path.
*/
enum UnknownGroupPolicy : ubyte {
skip, ///Don't save unknown groups.
preserve, ///Save unknown groups.
throwError ///Throw error when unknown group is encountered.
}

///Options to manage icon theme file reading
static struct IconThemeReadOptions
{
///Base $(B ReadOptions) of $(B IniLikeFile).
IniLikeFile.ReadOptions baseOptions = IniLikeFile.ReadOptions(
IniLikeFile.ReadOptions.DuplicatePolicy.preserve,
IniLikeFile.ReadOptions.DuplicatePolicy.throwError,
IniLikeGroup.InvalidKeyPolicy.throwError, true);
IniLikeFile.ReadOptions baseOptions = IniLikeFile.ReadOptions(IniLikeFile.DuplicateGroupPolicy.skip);

alias baseOptions this;

/**
* Policy about reading extension groups (those start with 'X-').
*/
enum ExtensionGroupPolicy : ubyte {
skip, ///Don't save extension groups.
preserve ///Save extension groups.
}

/**
* Policy about reading groups with names which meaning is unknown, i.e. it's not extension nor relative directory path.
*/
enum UnknownGroupPolicy : ubyte {
skip, ///Don't save unknown groups.
preserve, ///Save unknown groups.
throwError ///Throw error when unknown group is encountered.
}

/**
* Set policy about unknown groups. By default they are skipped without errors.
* Note that all groups still need to be preserved if desktop file must be rewritten.
Expand All @@ -306,8 +303,53 @@ final class IconThemeFile : IniLikeFile
* Note that all groups still need to be preserved if desktop file must be rewritten.
*/
ExtensionGroupPolicy extensionGroupPolicy = ExtensionGroupPolicy.preserve;

///Setting parameters in any order, leaving not mentioned ones in default state.
@nogc @safe this(Args...)(Args args) nothrow pure {
foreach(arg; args) {
alias Unqual!(typeof(arg)) ArgType;
static if (is(ArgType == DuplicateKeyPolicy)) {
baseOptions.duplicateKeyPolicy = arg;
} else static if (is(ArgType == DuplicateGroupPolicy)) {
baseOptions.duplicateGroupPolicy = arg;
} else static if (is(ArgType == Flag!"preserveComments")) {
baseOptions.preserveComments = arg;
} else static if (is(ArgType == IniLikeGroup.InvalidKeyPolicy)) {
baseOptions.invalidKeyPolicy = arg;
} else static if (is(ArgType == IniLikeFile.ReadOptions)) {
baseOptions = arg;
} else static if (is(ArgType == UnknownGroupPolicy)) {
unknownGroupPolicy = arg;
} else static if (is(ArgType == ExtensionGroupPolicy)) {
extensionGroupPolicy = arg;
} else {
static assert(false, "Unknown argument type " ~ typeof(arg).stringof);
}
}
}

///
unittest
{
IconThemeReadOptions options;

options = IconThemeReadOptions(
ExtensionGroupPolicy.skip,
UnknownGroupPolicy.preserve,
DuplicateKeyPolicy.skip,
DuplicateGroupPolicy.preserve,
No.preserveComments
);

assert(options.unknownGroupPolicy == UnknownGroupPolicy.preserve);
assert(options.extensionGroupPolicy == ExtensionGroupPolicy.skip);
assert(options.duplicateGroupPolicy == DuplicateGroupPolicy.preserve);
assert(options.duplicateKeyPolicy == DuplicateKeyPolicy.skip);
assert(!options.preserveComments);
}
}

///
unittest
{
string contents =
Expand All @@ -317,9 +359,8 @@ Name=Theme
Key=Value`;

alias IconThemeFile.IconThemeReadOptions IconThemeReadOptions;
IconThemeReadOptions readOptions;
readOptions.extensionGroupPolicy = IconThemeReadOptions.ExtensionGroupPolicy.skip;
auto iconTheme = new IconThemeFile(iniLikeStringReader(contents), readOptions);

auto iconTheme = new IconThemeFile(iniLikeStringReader(contents), IconThemeReadOptions(ExtensionGroupPolicy.skip));
assert(iconTheme.group("X-SomeGroup") is null);

contents =
Expand All @@ -328,11 +369,7 @@ Name=Theme
[/invalid group]
$=StrangeKey`;

readOptions = IconThemeReadOptions.init;
readOptions.unknownGroupPolicy = IconThemeReadOptions.UnknownGroupPolicy.preserve;
readOptions.invalidKeyPolicy = IniLikeGroup.InvalidKeyPolicy.save;

iconTheme = new IconThemeFile(iniLikeStringReader(contents), readOptions);
iconTheme = new IconThemeFile(iniLikeStringReader(contents), IconThemeReadOptions(UnknownGroupPolicy.preserve, IniLikeGroup.InvalidKeyPolicy.save));
assert(iconTheme.group("/invalid group") !is null);
assert(iconTheme.group("/invalid group").value("$") == "StrangeKey");

Expand All @@ -350,25 +387,16 @@ Valid=Key
$=Invalid`;

assertThrown(new IconThemeFile(iniLikeStringReader(contents)));

readOptions = IconThemeReadOptions.init;
readOptions.invalidKeyPolicy = IniLikeGroup.InvalidKeyPolicy.skip;

assertNotThrown(new IconThemeFile(iniLikeStringReader(contents), readOptions));
assertNotThrown(new IconThemeFile(iniLikeStringReader(contents), IconThemeReadOptions(IniLikeGroup.InvalidKeyPolicy.skip)));

contents =
`[Icon Theme]
Name=Name
[/invalidpath]
Key=Value`;

readOptions = IconThemeReadOptions.init;
readOptions.unknownGroupPolicy = IconThemeReadOptions.UnknownGroupPolicy.throwError;
assertThrown(new IconThemeFile(iniLikeStringReader(contents), readOptions));

readOptions = IconThemeReadOptions.init;
readOptions.unknownGroupPolicy = IconThemeReadOptions.UnknownGroupPolicy.preserve;
assertNotThrown(iconTheme = new IconThemeFile(iniLikeStringReader(contents), readOptions));
assertThrown(new IconThemeFile(iniLikeStringReader(contents), IconThemeReadOptions(UnknownGroupPolicy.throwError)));
assertNotThrown(iconTheme = new IconThemeFile(iniLikeStringReader(contents), IconThemeReadOptions(UnknownGroupPolicy.preserve)));
assert(iconTheme.cachePath().empty);
assert(iconTheme.group("/invalidpath") !is null);
}
Expand All @@ -384,7 +412,7 @@ protected:
_iconTheme = new IconThemeGroup();
return _iconTheme;
} else if (groupName.startsWith("X-")) {
if (_options.extensionGroupPolicy == IconThemeReadOptions.ExtensionGroupPolicy.skip) {
if (_options.extensionGroupPolicy == ExtensionGroupPolicy.skip) {
return null;
} else {
return createEmptyGroup(groupName);
Expand All @@ -393,11 +421,11 @@ protected:
return createEmptyGroup(groupName);
} else {
final switch(_options.unknownGroupPolicy) {
case IconThemeReadOptions.UnknownGroupPolicy.skip:
case UnknownGroupPolicy.skip:
return null;
case IconThemeReadOptions.UnknownGroupPolicy.preserve:
case UnknownGroupPolicy.preserve:
return createEmptyGroup(groupName);
case IconThemeReadOptions.UnknownGroupPolicy.throwError:
case UnknownGroupPolicy.throwError:
throw new IniLikeException("Invalid group name: '" ~ groupName ~ "'. Must be valid relative path or start with 'X-'");
}
}
Expand Down
1 change: 1 addition & 0 deletions travis-script.sh
Expand Up @@ -6,6 +6,7 @@ if [ "$USE_DOVERALLS" = "true" ]; then
wget -O doveralls "https://github.com/ColdenCullen/doveralls/releases/download/v1.2.0/doveralls_linux_travis"
chmod +x doveralls
dub test -b unittest-cov --compiler=${DC}
rm ..-*
./doveralls
else
dub test --compiler=${DC}
Expand Down

0 comments on commit c10de63

Please sign in to comment.