Skip to content

Commit

Permalink
[MISC] Remove dependency of seqan3::io.
Browse files Browse the repository at this point in the history
Signed-off-by: Lydia Buntrock <[email protected]>
  • Loading branch information
Irallia committed Jan 5, 2022
1 parent 27a1533 commit e1750fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 55 deletions.
43 changes: 1 addition & 42 deletions include/sharg/validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include <seqan3/core/debug_stream/detail/to_string.hpp>
#include <seqan3/core/debug_stream/range.hpp>
#include <seqan3/io/detail/misc.hpp>
#include <seqan3/utility/views/join_with.hpp>

#include <sharg/detail/safe_filesystem_entry.hpp>
Expand Down Expand Up @@ -260,7 +259,6 @@ value_list_validator(range_type && rng) -> value_list_validator<std::ranges::ran

/*!\brief An abstract base class for the file and directory validators.
* \ingroup argument_parser
* \tparam file_t The type of the file to get the valid extensions for; `void` on default.
*
* \details
*
Expand Down Expand Up @@ -438,7 +436,6 @@ class file_validator_base
/*!\brief A validator that checks if a given path is a valid input file.
* \ingroup argument_parser
* \implements sharg::validator
* \tparam file_t The type of the file to get the valid extensions for; `void` on default.
*
* \details
*
Expand All @@ -459,35 +456,17 @@ class file_validator_base
*
* \remark For a complete overview, take a look at \ref argument_parser
*/
template <typename file_t = void>
class input_file_validator : public file_validator_base
{
public:

static_assert(std::same_as<file_t, void> || seqan3::detail::has_type_valid_formats<file_t>,
"Expected either a template type with a static member called valid_formats (a file type) or void.");

// Import from base class.
using typename file_validator_base::option_value_type;

/*!\name Constructors, destructor and assignment
* \{
*/

/*!\brief Default constructor.
*
* \details
*
* If the class' template argument `file_t` names a valid seqan3 file type that contains a
* static member `valid_formats`, e.g. seqan3::sequence_input_file::valid_formats, then it generates the
* list of valid extensions from this file. Otherwise the extensions list is empty.
*/
input_file_validator()
{
if constexpr (!std::same_as<file_t, void>)
file_validator_base::extensions = seqan3::detail::valid_file_extensions<typename file_t::valid_formats>();
}

input_file_validator(input_file_validator const &) = default; //!< Defaulted.
input_file_validator(input_file_validator &&) = default; //!< Defaulted.
input_file_validator & operator=(input_file_validator const &) = default; //!< Defaulted.
Expand All @@ -496,17 +475,8 @@ class input_file_validator : public file_validator_base

/*!\brief Constructs from a given collection of valid extensions.
* \param[in] extensions The valid extensions to validate for.
*
* \details
*
* This constructor is only available if `file_t` does not name a valid seqan3 file type that contains a
* static member `valid_formats`.
*/
explicit input_file_validator(std::vector<std::string> extensions)
//!\cond
requires std::same_as<file_t, void>
//!\endcond
: file_validator_base{}
explicit input_file_validator(std::vector<std::string> extensions) : file_validator_base{}
{
file_validator_base::extensions = std::move(extensions);
}
Expand Down Expand Up @@ -568,7 +538,6 @@ enum class output_file_open_options
/*!\brief A validator that checks if a given path is a valid output file.
* \ingroup argument_parser
* \implements sharg::validator
* \tparam file_t The type of the file to get the valid extensions for; `void` on default.
*
* \details
*
Expand All @@ -593,12 +562,9 @@ enum class output_file_open_options
*
* \remark For a complete overview, take a look at \ref argument_parser
*/
template <typename file_t = void>
class output_file_validator : public file_validator_base
{
public:
static_assert(std::same_as<file_t, void> || seqan3::detail::has_type_valid_formats<file_t>,
"Expected either a template type with a static member called valid_formats (a file type) or void.");

// Import from base class.
using typename file_validator_base::option_value_type;
Expand Down Expand Up @@ -636,16 +602,9 @@ class output_file_validator : public file_validator_base

/*!\brief The default extensions of `file_t`.
* \returns A list of default extensions for `file_t`, will be empty if `file_t` is `void`.
*
* \details
*
* If `file_t` does name a valid seqan3 file type that contains a static member `valid_formats` returns the
* extensions of that `file_t` type. Otherwise returns an empty list.
*/
static std::vector<std::string> default_extensions()
{
if constexpr (!std::same_as<file_t, void>)
return seqan3::detail::valid_file_extensions<typename file_t::valid_formats>();
return {};
}

Expand Down
4 changes: 2 additions & 2 deletions test/snippet/validators_input_file_ext_from_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ int main()
sharg::input_file_validator validator2{std::vector{std::string{"exe"}, std::string{"fasta"}}};
std::cerr << validator2.get_help_page_message() << '\n';

// Give the seqan3 file type as a template argument to get all valid extensions for this file.
sharg::input_file_validator<seqan3::sequence_file_input<>> validator3{};
// Give the sharg file type as a template argument to get all valid extensions for this file.
sharg::input_file_validator validator3{seqan3::sequence_file_input<>::valid_formats}
std::cerr << validator3.get_help_page_message() << '\n';

return 0;
Expand Down
42 changes: 31 additions & 11 deletions test/unit/format_parse_validators_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ struct dummy_file
static inline std::vector<std::string> file_extensions{ {"sam"}, {"bam"}};
};

using valid_formats = seqan3::type_list<format1, format2>;
};

dummy_file::format1 f1;
dummy_file::format2 f2;
std::vector<std::string> A = f1.file_extensions;
std::vector<std::string> B = f2.file_extensions;
std::vector<std::string> dummy_file_extensions;

std::string const basic_options_str = "OPTIONS\n"
"\n"
" Basic options:\n"
Expand Down Expand Up @@ -73,8 +78,8 @@ TEST(validator_test, fullfill_concept)
EXPECT_TRUE(sharg::validator<sharg::arithmetic_range_validator<int>>);
EXPECT_TRUE(sharg::validator<sharg::value_list_validator<double>>);
EXPECT_TRUE(sharg::validator<sharg::value_list_validator<std::string>>);
EXPECT_TRUE(sharg::validator<sharg::input_file_validator<>>);
EXPECT_TRUE(sharg::validator<sharg::output_file_validator<>>);
EXPECT_TRUE(sharg::validator<sharg::input_file_validator>);
EXPECT_TRUE(sharg::validator<sharg::output_file_validator>);
EXPECT_TRUE(sharg::validator<sharg::input_directory_validator>);
EXPECT_TRUE(sharg::validator<sharg::output_directory_validator>);
EXPECT_TRUE(sharg::validator<sharg::regex_validator>);
Expand Down Expand Up @@ -133,7 +138,10 @@ TEST(validator_test, input_file)
}

{ // read from file
sharg::input_file_validator<dummy_file> my_validator{};
dummy_file_extensions.reserve( A.size() + B.size() ); // preallocate memory
dummy_file_extensions.insert( dummy_file_extensions.end(), A.begin(), A.end() );
dummy_file_extensions.insert( dummy_file_extensions.end(), B.begin(), B.end() );
sharg::input_file_validator my_validator{dummy_file_extensions};
EXPECT_NO_THROW(my_validator(tmp_name.get_path()));
}

Expand Down Expand Up @@ -197,7 +205,10 @@ TEST(validator_test, input_file)
TEST(validator_test, input_file_ext_from_file)
{
// Give as a template argument the seqan3 file type to get all valid extensions for this file.
sharg::input_file_validator<dummy_file> validator{};
dummy_file_extensions.reserve( A.size() + B.size() ); // preallocate memory
dummy_file_extensions.insert( dummy_file_extensions.end(), A.begin(), A.end() );
dummy_file_extensions.insert( dummy_file_extensions.end(), B.begin(), B.end() );
sharg::input_file_validator validator{dummy_file_extensions};
EXPECT_EQ(validator.get_help_page_message(), "The input file must exist and read permissions must be granted. "
"Valid file extensions are: [fa, fasta, sam, bam].");

Expand Down Expand Up @@ -264,8 +275,16 @@ TEST(validator_test, output_file)
}

{ // read from file
sharg::output_file_validator<dummy_file> my_validator{};
dummy_file_extensions.reserve( A.size() + B.size() ); // preallocate memory
dummy_file_extensions.insert( dummy_file_extensions.end(), A.begin(), A.end() );
dummy_file_extensions.insert( dummy_file_extensions.end(), B.begin(), B.end() );
sharg::output_file_validator my_validator{sharg::output_file_open_options::create_new,
dummy_file_extensions};
EXPECT_NO_THROW(my_validator(tmp_name.get_path()));

sharg::output_file_validator my_validator2{sharg::output_file_open_options::open_or_create,
dummy_file_extensions};
EXPECT_NO_THROW(my_validator2(tmp_name.get_path()));
}

std::filesystem::path file_out_path;
Expand Down Expand Up @@ -360,15 +379,16 @@ TEST(validator_test, output_file)
TEST(validator_test, output_file_ext_from_file)
{
// Give as a template argument the seqan3 file type to get all valid extensions for this file.
sharg::output_file_validator<dummy_file> validator1{};
EXPECT_EQ(validator1.get_help_page_message(), "The output file must not exist already and write permissions must "
"be granted. Valid file extensions are: [fa, fasta, sam, bam].");

sharg::output_file_validator<dummy_file> validator2{sharg::output_file_open_options::create_new};
dummy_file_extensions.reserve( A.size() + B.size() ); // preallocate memory
dummy_file_extensions.insert( dummy_file_extensions.end(), A.begin(), A.end() );
dummy_file_extensions.insert( dummy_file_extensions.end(), B.begin(), B.end() );

sharg::output_file_validator validator2{sharg::output_file_open_options::create_new, dummy_file_extensions};
EXPECT_EQ(validator2.get_help_page_message(), "The output file must not exist already and write permissions must "
"be granted. Valid file extensions are: [fa, fasta, sam, bam].");

sharg::output_file_validator<dummy_file> validator3{sharg::output_file_open_options::open_or_create};
sharg::output_file_validator validator3{sharg::output_file_open_options::open_or_create, dummy_file_extensions};
EXPECT_EQ(validator3.get_help_page_message(), "Write permissions must be granted. Valid file extensions are: [fa, "
"fasta, sam, bam].");

Expand Down

0 comments on commit e1750fc

Please sign in to comment.