Skip to content

Commit

Permalink
Refactor MatchSpec filename handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Feb 27, 2024
1 parent afb6ced commit 5d9e022
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 26 deletions.
10 changes: 8 additions & 2 deletions libmamba/include/mamba/specs/match_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,19 @@ namespace mamba::specs
[[nodiscard]] auto filename() const -> std::string_view;
void set_filename(std::string val);

[[nodiscard]] auto is_file() const -> bool;

[[nodiscard]] auto optional() const -> bool;
void set_optional(bool opt);

// TODO as string_view conditional on channel type
[[nodiscard]] auto url() const -> const std::string&;

[[nodiscard]] auto conda_build_form() const -> std::string;
[[nodiscard]] auto str() const -> std::string;

[[nodiscard]] auto is_simple() const -> bool;

[[nodiscard]] auto is_file() const -> bool;

private:

struct ExtraMembers
Expand All @@ -118,6 +119,11 @@ namespace mamba::specs
std::string m_url;

auto extra() -> ExtraMembers&;
[[nodiscard]] auto channel_is_file() const -> bool;
[[nodiscard]] auto channel_filename() const -> std::string_view;
void set_channel_filename(std::string val);
[[nodiscard]] auto extra_filename() const -> std::string_view;
void set_extra_filename(std::string val);
};

namespace match_spec_literals
Expand Down
112 changes: 88 additions & 24 deletions libmamba/src/specs/match_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ namespace mamba::specs
};

auto out = MatchSpec();
// Channel is also read for the filename so no need to set it.
out.m_channel = UnresolvedChannel::parse(spec)
.or_else([](specs::ParseError&& error) { throw std::move(error); })
.value();
auto [_, pkg] = util::rsplit_once(out.m_channel->location(), '/');
out.m_filename = std::string(pkg);
out.m_url = util::path_or_url_to_url(spec);

// Build string
Expand Down Expand Up @@ -410,7 +410,7 @@ namespace mamba::specs
}
if (const auto& val = at_or(extra, "fn", ""); !val.empty())
{
out.m_filename = val;
out.set_filename(val);
}
if (const auto& val = at_or(extra, "md5", ""); !val.empty())
{
Expand Down Expand Up @@ -440,6 +440,44 @@ namespace mamba::specs
return out;
}

auto MatchSpec::channel_is_file() const -> bool
{
if (const auto& chan = channel(); chan.has_value())
{
auto type = chan->type();
using Type = typename UnresolvedChannel::Type;
return (type == Type::PackageURL) || (type == Type::PackagePath);
}
return false;
}

auto MatchSpec::channel_filename() const -> std::string_view
{
if (channel_is_file())
{
assert(channel().has_value());
auto [_, pkg] = util::rsplit_once(channel()->location(), '/');
return pkg;
}
return {};
}

void MatchSpec::set_channel_filename(std::string val)
{
assert(channel().has_value());
assert(channel_is_file());
auto location = m_channel->clear_location();
auto [base, pkg] = util::rsplit_once(location, '/');
assert(base.has_value());
location = base.value_or("");
location += val;
set_channel({ UnresolvedChannel(
std::move(location),
m_channel->clear_platform_filters(),
m_channel->type()
) });
}

auto MatchSpec::channel() const -> const std::optional<UnresolvedChannel>&
{
return m_channel;
Expand All @@ -448,6 +486,54 @@ namespace mamba::specs
void MatchSpec::set_channel(std::optional<UnresolvedChannel> chan)
{
m_channel = std::move(chan);
// Channel filename take precedence
if (channel_is_file() && !extra_filename().empty())
{
set_extra_filename({});
}
}

auto MatchSpec::extra_filename() const -> std::string_view
{
if (m_extra.has_value())
{
return m_extra->filename;
}
return {};
}

void MatchSpec::set_extra_filename(std::string val)
{
if (val != filename()) // Avoid allocating extra to set the default value
{
extra().filename = std::move(val);
}
}

auto MatchSpec::filename() const -> std::string_view
{
if (channel_is_file())
{
return channel_filename();
}
return extra_filename();
}

void MatchSpec::set_filename(std::string val)
{
if (channel_is_file())
{
set_channel_filename(std::move(val));
}
else
{
set_extra_filename(std::move(val));
}
}

auto MatchSpec::is_file() const -> bool
{
return (!filename().empty()) || (!m_url.empty());
}

auto MatchSpec::name_space() const -> const std::string&
Expand Down Expand Up @@ -602,23 +688,6 @@ namespace mamba::specs
}
}

auto MatchSpec::filename() const -> std::string_view
{
if (m_extra.has_value())
{
return m_extra->filename;
}
return "";
}

void MatchSpec::set_filename(std::string val)
{
if (val != filename()) // Avoid allocating extra to set the default value
{
extra().filename = std::move(val);
}
}

auto MatchSpec::optional() const -> bool
{
return m_extra.has_value() && m_extra->optional;
Expand Down Expand Up @@ -792,11 +861,6 @@ namespace mamba::specs
&& m_build_number.is_explicitly_free();
}

auto MatchSpec::is_file() const -> bool
{
return (!m_filename.empty()) || (!m_url.empty());
}

auto MatchSpec::extra() -> ExtraMembers&
{
if (!m_extra.has_value())
Expand Down

0 comments on commit 5d9e022

Please sign in to comment.