Repository: Orange-OpenSource/hurl
File: packages/hurlfmt/src/curl/commands.rs (Rust)

Code before:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
use clap::{value_parser, ArgAction};
use cookie::Cookie;

pub fn compressed() -> clap::Arg {
    clap::Arg::new("compressed").long("compressed").num_args(0)
}

pub fn cookies() -> clap::Arg {
    clap::Arg::new("cookies")
        .long("cookie")
        .short('b')
        .value_name("NAME1=VALUE1; NAME2=VALUE2")
        .value_parser(|value: &str| {
            if value.trim().is_empty() {
                return Err("cookie is empty".to_string());
            }

            let mut cookies = Vec::new();
            let mut errors = Vec::new();

            for (i, result) in Cookie::split_parse(value).enumerate() {
                match result {
                    Ok(c) => cookies.push(c),
                    Err(e) => errors.push((i, e)),
                }
            }

            if errors.is_empty() {
                let cookies_str = cookies
                    .into_iter()
                    .map(|c| format!("{}={}", c.name(), c.value()))
                    .collect::<Vec<_>>()
                    .join("; ");
                Ok(cookies_str)
            } else {
                match errors.as_slice() {
                    [(_, e)] => Err(e.to_string()),
                    _ => {

Review comment (suggestion):
Just to note: since we only want to support the `name=value` variant of `curl`'s cookie flag, I've made empty cookies invalid.

Code after:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
use clap::{value_parser, ArgAction};

pub fn compressed() -> clap::Arg {
    clap::Arg::new("compressed").long("compressed").num_args(0)
}

pub fn cookies() -> clap::Arg {
    clap::Arg::new("cookies")
        .long("cookie")
        .short('b')
        .value_name("NAME1=VALUE1; NAME2=VALUE2")
        .value_parser(|value: &str| {
            if value.trim().is_empty() {
                return Err("empty value provided".to_string());
            }

            let (valid_cookies, invalid_cookies): (Vec<_>, Vec<_>) = value
                .split(';')
                .map(str::trim)
                .filter(|c| !c.is_empty())
                .partition(|c| c.contains('='));

            if invalid_cookies.is_empty() {
                Ok(valid_cookies.join("; "))
            } else {
                match invalid_cookies.as_slice() {
                    [_] => Err("invalid cookie pair provided".to_string()),
                    _ => Err(format!(
                        "invalid cookie pairs provided: [{}]",
                        invalid_cookies.join(", ")
                    )),
                }
            }
        })
        .action(ArgAction::Append)
        .num_args(1)
}