hypershell_components/providers/
string_arg.rs

1use alloc::borrow::Cow;
2use alloc::format;
3use alloc::string::{String, ToString};
4use core::fmt::Display;
5use core::marker::PhantomData;
6
7use cgp::prelude::*;
8
9use crate::components::{
10    CanExtractStringArg, CommandArgExtractor, CommandArgExtractorComponent, HasCommandArgType,
11    StringArgExtractor, StringArgExtractorComponent,
12};
13use crate::dsl::{FieldArg, JoinArgs, StaticArg};
14
15#[cgp_new_provider]
16impl<Context, Arg> CommandArgExtractor<Context, Arg> for ExtractStringCommandArg
17where
18    Context: HasCommandArgType + CanExtractStringArg<Arg>,
19    Context::CommandArg: From<String>,
20{
21    fn extract_command_arg(context: &Context, phantom: PhantomData<Arg>) -> Context::CommandArg {
22        context.extract_string_arg(phantom).into_owned().into()
23    }
24}
25
26#[cgp_new_provider]
27impl<Context, Arg> StringArgExtractor<Context, StaticArg<Arg>> for ExtractStaticArg
28where
29    Arg: Default + Display,
30{
31    fn extract_string_arg(
32        _context: &Context,
33        _phantom: PhantomData<StaticArg<Arg>>,
34    ) -> Cow<'_, str> {
35        Arg::default().to_string().into()
36    }
37}
38
39#[cgp_new_provider]
40impl<Context, Tag> StringArgExtractor<Context, FieldArg<Tag>> for ExtractFieldArg
41where
42    Context: HasField<Tag, Value: Display>,
43{
44    fn extract_string_arg(context: &Context, _phantom: PhantomData<FieldArg<Tag>>) -> Cow<'_, str> {
45        context.get_field(PhantomData).to_string().into()
46    }
47}
48
49pub struct JoinStringArgs;
50
51#[cgp_provider]
52impl<Context, Arg, Args> StringArgExtractor<Context, JoinArgs<Cons<Arg, Args>>> for JoinStringArgs
53where
54    Context: CanExtractStringArg<Arg>,
55    Self: StringArgExtractor<Context, JoinArgs<Args>>,
56{
57    fn extract_string_arg(
58        context: &Context,
59        _phantom: PhantomData<JoinArgs<Cons<Arg, Args>>>,
60    ) -> Cow<'_, str> {
61        let arg = context.extract_string_arg(PhantomData);
62        let args = Self::extract_string_arg(context, PhantomData::<JoinArgs<Args>>);
63
64        format!("{arg}{args}").into()
65    }
66}
67
68#[cgp_provider]
69impl<Context> StringArgExtractor<Context, JoinArgs<Nil>> for JoinStringArgs {
70    fn extract_string_arg(
71        _context: &Context,
72        _phantom: PhantomData<JoinArgs<Nil>>,
73    ) -> Cow<'_, str> {
74        "".into()
75    }
76}