parsing

python
from defusedxml.ElementTree import fromstring

payload = request.data.decode('utf-8')
root = fromstring(payload)
invoice_number = root.findtext('invoice_number')

XXE safe XML parsing with external entity resolution disabled

xxe xml parsing
by Kai Nakamura 1 tab
rust
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub enum LogLevel {
    Info,
    Warn,

Streaming Log File Aggregation With Buffered Line Iteration in Rust

rust streaming file-io
by codesnips 3 tabs
swift
import Foundation

// MARK: - Basic Codable
struct User: Codable {
    let id: Int
    let username: String

JSON encoding and decoding with Codable

swift codable json
by Sofia Martinez 1 tab
python
from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List

Parsing a Nested JSON Feed into Normalized Dataclasses in Python

dataclasses json parsing
by codesnips 3 tabs
rust
use nom::{
    bytes::complete::tag,
    character::complete::digit1,
    IResult,
};

nom for parser combinators and zero-copy parsing

rust parsing nom
by Marcus Chen 1 tab
rust
#[derive(Debug, Clone, Copy)]
pub struct ByteRange {
    pub start: u64,
    pub end: u64, // inclusive
}

Serving HTTP Range Requests in Axum with Partial Content Responses

axum http range-requests
by codesnips 3 tabs
typescript
import { z } from "zod";

export const rowSchema = z.object({
  email: z.string().email(),
  name: z.string().min(1, "name is required"),
  age: z.coerce.number().int().min(0, "age must be >= 0"),

Parse a CSV Upload into Typed Rows with Per-Row Validation Errors

typescript csv validation
by codesnips 3 tabs
python
from dataclasses import dataclass


class ConfigError(Exception):
    pass

Typed Argparse Subcommands with Dataclasses and Handler Dispatch

argparse cli dataclasses
by codesnips 3 tabs
rust
use serde::Deserialize;
use std::fmt;

#[derive(Debug, Deserialize)]
pub struct UserRecord {
    pub name: String,

Row-by-Row CSV Import Validation With Line-Numbered Errors in Rust

csv serde validation
by codesnips 3 tabs
rust
use pulldown_cmark::{html, Options, Parser};
use std::collections::HashSet;

pub struct SafeHtml(String);

impl SafeHtml {

Rendering Untrusted Markdown to Sanitized HTML with pulldown-cmark and ammonia

rust markdown html-sanitization
by codesnips 3 tabs
rust
use serde::{Deserialize, Deserializer};

#[derive(Debug, Deserialize)]
pub struct ListFilter {
    #[serde(default)]
    pub status: Status,

Parse URL Query Strings into a Typed Filter Struct with Defaults in Rust

rust serde query-string
by codesnips 3 tabs
rust
use std::error::Error;
use std::fmt;
use std::io;
use std::num::ParseIntError;

#[derive(Debug)]

Custom Error Enum With From Conversions for the ? Operator in Rust

rust error-handling traits
by codesnips 3 tabs