U
    /ji<                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZejdk rd dlZej	Z	ejdk rpej
Z
ejZejZejZqej
Z
ejZejZejZnej
Z
ejZejZejZej	Z	ejdk redZnejZd dlmZ ejddd	Zed
Zejde	dZejddG dd dejeef ZejZejZdddddZddddddZddddddZddd d!d"d#Z d$d%d&d'd(Z!d)d*d+d,d-Z"d)d*d+d.d/Z#ejdk rej$dd0d)d)d+d1d2Z%ej$dd0d)d)d+d3d4Z&n8ej$dd0d)d)d+d5d2Z%ej$dd0d)d)d+d6d4Z&ejddG d7d8 d8eje Z'ej(Z(ej)j*Z*ej)j+Z+ej,Z,ej-Z-ej.Z.ej/Z/ej0Z0ej1Z1ej2Z3ej4Z4dS )9    )annotationsN)      )r   	   r   
   )declarative_asn1TT)	covariantUTag)bound)frozenc                   @  s"   e Zd ZU dZded< ded< dS )Varianta  
    A tagged variant for CHOICE fields with the same underlying type.

    Use this when you have multiple CHOICE alternatives with the same type
    and need to distinguish between them:

        foo: (
            Annotated[Variant[int, typing.Literal["IntA"]], Implicit(0)]
            | Annotated[Variant[int, typing.Literal["IntB"]], Implicit(1)]
        )

    Usage:
        example = Example(foo=Variant(5, "IntA"))
        decoded.foo.value  # The int value
        decoded.foo.tag    # "IntA" or "IntB"
    r   valuestrtagN)__name__
__module____qualname____doc____annotations__ r   r   A/tmp/pip-unpacked-wheel-qzbw3lpx/cryptography/hazmat/asn1/asn1.pyr   2   s   
r   typebool)
field_typereturnc                 C  s*   t tdrtjtjfntjf}t| |kS )N	UnionType)hasattrtypesr   typingUnionget_type_origin)r   Zunion_typesr   r   r   	_is_unionM   s
    r$   tupler   zdeclarative_asn1.Annotation)metadata
field_namer   c                 C  s   d }d }d }| D ]}t |tr>|d k	r6td| d|j}qt |tjrh|d k	rbtd| d|}qt |tjr|d k	rtd| d|}qtd| qtj|||dS )Nz-multiple DEFAULT annotations found in field ''z7multiple IMPLICIT/EXPLICIT annotations found in field 'z*multiple SIZE annotations found in field 'zunsupported annotation: )defaultencodingsize)
isinstanceDefault	TypeErrorr   r   EncodingSize
Annotation)r&   r'   r)   r*   r+   Zraw_annotationr   r   r   _extract_annotationY   s8    



  r2   z
typing.Anyzdeclarative_asn1.AnnotatedType)r   r'   r   c                   s  t | tkr&t| j}t| ^} }nt }|jd k	rpt | tj	t
fkrp| tjtjtttfkrptd d| tkrt|jtrtd dn|jd k	rtd dt| dr| j}t|tjjtjjfstd| tttj||S t| rPt| }t|dkrt |kr|d t!d kr>|d	 n|d }|tkrXtd
t"|}|j#$ svtd|jd k	rtdtj%|}nt|jtrtdfdd|D }	|	d	 j&d k	 t' fdd|	D rtd rdd |	D }
t|	t|
krtdtj(|	}t |krJtj%t|t n|}nht | tj	krt"t| d	 }tj)|}n8t | t
krt"t| d	 }tj
|}n
t*| }t||S )Nzfield 'z' has a SIZE annotation, but SIZE annotations are only supported for fields of types: [SEQUENCE OF, SET OF, BIT STRING, OCTET STRING, UTF8String, PrintableString, IA5String]zW' has an IMPLICIT annotation, but IMPLICIT annotations are not supported for TLV types.zT' has a DEFAULT annotation, but DEFAULT annotations are not supported for TLV types.__asn1_root__zunsupported root type:       r   z=optional TLV types (`TLV | None`) are not currently supportedzoptional (`X | None`) types cannot have `X` annotated: annotations must apply to the union (i.e: `Annotated[X | None, annotation]`)z@optional (`X | None`) types should not have a DEFAULT annotationzCCHOICE (`X | Y | ...`) types should not have an IMPLICIT annotationc                   s"   g | ]}|t d k	rt| qS N)r   _type_to_variant).0arg)r'   r   r   
<listcomp>   s   z)_normalize_field_type.<locals>.<listcomp>c                 3  s   | ]}|j d k	 kV  qd S r6   tag_namer8   v)are_union_types_taggedr   r   	<genexpr>   s   z(_normalize_field_type.<locals>.<genexpr>zbWhen using `asn1.Variant` in a union, all the other types in the union must also be `asn1.Variant`c                 S  s   h | ]
}|j qS r   r;   r=   r   r   r   	<setcomp>   s     z(_normalize_field_type.<locals>.<setcomp>zBWhen using `asn1.Variant` in a union, the tags used must be unique)+r#   	Annotatedr2   __metadata__get_type_argsr   r1   r+   builtinslistSetOfbytesr   	BitString	IA5StringPrintableStringr.   TLVr,   r*   Implicitr)   r   r3   TypeSequenceSetAnnotatedTyper!   castr$   lenNoneTyper   _normalize_field_type
annotationZis_emptyOptionr<   anyZChoiceZ
SequenceOfnon_root_python_to_rust)r   r'   rV   _Z	root_typeZ
union_argsZoptional_typeannotated_typeZrust_field_typevariantstagsZrust_choice_type
inner_typer   )r?   r'   r   rU   |   s    





 
 




 

 
 
rU   zdeclarative_asn1.Variant)tr'   r   c           	      C  s   t | tk}|rt| d n| }t |tkrt|\}}t |tjk	rNtdt|d }t|drl|j}n
t	
|}|rt	|t| j|}nt	|t	 }t	t||S t	|t| |d S d S )Nr   zWhen using `asn1.Variant` in a type annotation, the second type parameter must be a `typing.Literal` type. E.g: `Variant[int, typing.Literal["MyInt"]]`.r3   )r#   rB   rD   r   r!   Literalr.   r   r3   r   rY   rQ   r2   rC   r1   rU   )	r_   r'   Zis_annotatedr^   Z
value_typeZtag_literalr<   Z	rust_typeZann_typer   r   r   r7     s6    


r7   zdict[str, type]z)dict[str, declarative_asn1.AnnotatedType])
raw_fieldsr   c                 C  s,   i }|   D ]\}}t||}|||< q|S r6   )itemsrU   )ra   fieldsr'   r   Zannotated_field_typer   r   r   _annotate_fields7  s
    

rd   ztype[U]None)clsr   c                 C  s.   t | dd}tj| t|}t| d| d S NT)Zinclude_extrasr3   )get_type_hintsr   rN   rO   rd   setattrrf   ra   rootr   r   r   _register_asn1_sequenceD  s    rl   c                 C  s.   t | dd}tj| t|}t| d| d S rg   )rh   r   rN   rP   rd   ri   rj   r   r   r   _register_asn1_setK  s    rm   )Zkw_only_defaultc                 C  s@   t jdkr"tjddddd| }ntjddd| }t| |S Nr   FTrepreqZ
match_argsZkw_only)rp   rq   )sysversion_infodataclasses	dataclassrl   rf   Zdataclass_clsr   r   r   sequenceZ  s"    
	rw   c                 C  s@   t jdkr"tjddddd| }ntjddd| }t| |S rn   )rr   rs   rt   ru   rm   rv   r   r   r   setq  s"    
	rx   c                 C  s"   t jddddd| }t| |S NFTro   )rt   ru   rl   rv   r   r   r   rw     s    c                 C  s"   t jddddd| }t| |S ry   )rt   ru   rm   rv   r   r   r   rx     s    c                   @  s   e Zd ZU ded< dS )r-   r   r   N)r   r   r   r   r   r   r   r   r-     s   
r-   )5
__future__r   rE   rt   rr   r    r!   rs   Ztyping_extensionsZLiteralStringrh   get_argsrD   
get_originr#   rB   r   rT   Z"cryptography.hazmat.bindings._rustr   TypeVarr	   r   r   ru   Genericr   Z
decode_derZ
encode_derr$   r2   rU   r7   rd   rl   rm   Zdataclass_transformrw   rx   r-   rG   r/   ZExplicitrM   r0   rK   rJ   ZUTCTimeZGeneralizedTimerI   ZTlvrL   ZNullr   r   r   r   <module>   s~   





# +




