Skip to content

Commit

Permalink
fixup! Implement FromStr on GeographicLocationConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed Jan 21, 2025
1 parent 29ed705 commit 088d27a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
2 changes: 1 addition & 1 deletion mullvad-cli/src/cmds/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ pub async fn resolve_location_constraint(
} else {
// The Constraint was not a relay, assuming it to be a location
let location_constraint: Constraint<GeographicLocationConstraint> =
Constraint::try_from(location_constraint_args)?;
Constraint::from(location_constraint_args);

// If the location constraint was not "any", then validate the country/city
if let Constraint::Only(constraint) = &location_constraint {
Expand Down
62 changes: 24 additions & 38 deletions mullvad-cli/src/cmds/relay_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,37 @@ pub struct LocationArgs {
pub hostname: Option<Hostname>,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to parse location constraint from input: TODO")]
Parse,
}

impl TryFrom<LocationArgs> for GeographicLocationConstraint {
type Error = Error;

fn try_from(value: LocationArgs) -> Result<Self, Self::Error> {
match (value.country, value.city, value.hostname) {
(country, None, None) => Ok(GeographicLocationConstraint::Country(country)),
(country, Some(city), None) => Ok(GeographicLocationConstraint::City(country, city)),
(country, Some(city), Some(hostname)) => Ok(GeographicLocationConstraint::Hostname(
country, city, hostname,
)),
_ => Err(Error::Parse),
//_ => unreachable!("invalid location arguments"),
impl From<LocationArgs> for Constraint<GeographicLocationConstraint> {
fn from(value: LocationArgs) -> Self {
if value.country.eq_ignore_ascii_case("any") {
return Constraint::Any;
}
}
}

impl TryFrom<LocationArgs> for LocationConstraint {
type Error = Error;

fn try_from(value: LocationArgs) -> Result<Self, Self::Error> {
GeographicLocationConstraint::try_from(value).map(LocationConstraint::from)
Constraint::Only(match (value.country, value.city, value.hostname) {
(country, None, None) => GeographicLocationConstraint::Country(country),
(country, Some(city), None) => GeographicLocationConstraint::City(country, city),
(country, Some(city), Some(hostname)) => {
GeographicLocationConstraint::Hostname(country, city, hostname)
}
_ => unreachable!("invalid location arguments"),
})
}
}

impl TryFrom<LocationArgs> for Constraint<GeographicLocationConstraint> {
type Error = Error;

fn try_from(value: LocationArgs) -> Result<Self, Self::Error> {
impl From<LocationArgs> for Constraint<LocationConstraint> {
fn from(value: LocationArgs) -> Self {
if value.country.eq_ignore_ascii_case("any") {
return Ok(Constraint::Any);
return Constraint::Any;
}
GeographicLocationConstraint::try_from(value).map(Constraint::Only)
}
}

impl TryFrom<LocationArgs> for Constraint<LocationConstraint> {
type Error = Error;

fn try_from(value: LocationArgs) -> Result<Self, Self::Error> {
LocationConstraint::try_from(value).map(Constraint::Only)
let location = match (value.country, value.city, value.hostname) {
(country, None, None) => GeographicLocationConstraint::Country(country),
(country, Some(city), None) => GeographicLocationConstraint::City(country, city),
(country, Some(city), Some(hostname)) => {
GeographicLocationConstraint::Hostname(country, city, hostname)
}
_ => unreachable!("invalid location arguments"),
};
Constraint::Only(LocationConstraint::Location(location))
}
}

0 comments on commit 088d27a

Please sign in to comment.