Skip to content

Commit

Permalink
Derive UnpackValue for StringOrTuple
Browse files Browse the repository at this point in the history
Summary: Less code. Also more precise type for functions like `str.startswith`.

Reviewed By: JakobDegen

Differential Revision: D59378792

fbshipit-source-id: 299eac1f705da662bb65b5e36b8dfabede588643
  • Loading branch information
stepancheg authored and facebook-github-bot committed Jul 5, 2024
1 parent 0ea9228 commit 4f472bc
Showing 1 changed file with 5 additions and 30 deletions.
35 changes: 5 additions & 30 deletions starlark/src/stdlib/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ use crate as starlark;
use crate::environment::MethodsBuilder;
use crate::eval::Arguments;
use crate::eval::Evaluator;
use crate::typing::Ty;
use crate::values::list::ListOf;
use crate::values::none::NoneOr;
use crate::values::string::dot_format;
use crate::values::tuple::UnpackTuple;
use crate::values::type_repr::StarlarkTypeRepr;
use crate::values::types::string::iter::iterate_chars;
use crate::values::types::string::iter::iterate_codepoints;
use crate::values::types::tuple::value::Tuple;
use crate::values::typing::iter::StarlarkIter;
use crate::values::Heap;
use crate::values::StringValue;
Expand Down Expand Up @@ -99,34 +98,10 @@ fn rsplitn_whitespace(s: &str, maxsplit: usize) -> Vec<String> {
v
}

#[derive(StarlarkTypeRepr, UnpackValue)]
enum StringOrTuple<'v> {
String(&'v str),
Tuple(Vec<&'v str>),
}

impl<'v> StarlarkTypeRepr for StringOrTuple<'v> {
fn starlark_type_repr() -> Ty {
Ty::union2(String::starlark_type_repr(), Tuple::starlark_type_repr())
}
}

impl<'v> UnpackValue<'v> for StringOrTuple<'v> {
fn expected() -> String {
"str or tuple".to_owned()
}

fn unpack_value(value: Value<'v>) -> Option<Self> {
if let Some(s) = value.unpack_str() {
Some(Self::String(s))
} else {
Some(Self::Tuple(
Tuple::from_value(value)?
.iter()
.map(|x| x.unpack_str())
.collect::<Option<_>>()?,
))
}
}
Tuple(UnpackTuple<&'v str>),
}

#[starlark_module]
Expand Down Expand Up @@ -261,7 +236,7 @@ pub(crate) fn string_methods(builder: &mut MethodsBuilder) {
) -> anyhow::Result<bool> {
match suffix {
StringOrTuple::String(x) => Ok(this.ends_with(x)),
StringOrTuple::Tuple(xs) => Ok(xs.iter().any(|x| this.ends_with(x))),
StringOrTuple::Tuple(xs) => Ok(xs.items.iter().any(|x| this.ends_with(x))),
}
}

Expand Down Expand Up @@ -1163,7 +1138,7 @@ pub(crate) fn string_methods(builder: &mut MethodsBuilder) {
) -> anyhow::Result<bool> {
match prefix {
StringOrTuple::String(x) => Ok(this.starts_with(x)),
StringOrTuple::Tuple(xs) => Ok(xs.iter().any(|x| this.starts_with(x))),
StringOrTuple::Tuple(xs) => Ok(xs.items.iter().any(|x| this.starts_with(x))),
}
}

Expand Down

0 comments on commit 4f472bc

Please sign in to comment.