credify 0.4.0

A Rust library for validating LinkedIn profile URLs with LLM-friendly error messages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
<div align="center">
  <img src="credify_logo.png" alt="Credify Logo" width="200">
  
  # Credify
  
  [![Crates.io](https://img.shields.io/crates/v/credify.svg)](https://crates.io/crates/credify)
  [![Documentation](https://docs.rs/credify/badge.svg)](https://docs.rs/credify)
  [![CI](https://github.com/RustSandbox/Credify/workflows/CI/badge.svg)](https://github.com/RustSandbox/Credify/actions)
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
  [![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
</div>

A robust Rust library for validating LinkedIn profile URLs with AI-first design. Built for the era of AI agents and LLMs, Credify provides both traditional validation APIs and specialized functions optimized for AI tool calling, especially with frameworks like [Rig](https://github.com/0xPlaygrounds/rig).

**๐ŸŽฏ New in v0.4.0**: Enhanced LinkedIn 404 detection with comprehensive apostrophe encoding support and complete Rig framework integration examples with real-world patterns.

## ๐Ÿš€ Real-World Use Cases

### AI Recruiting Assistant
```rust
// Validate candidate profiles before outreach
let result = rig_validate_json("https://linkedin.com/in/john-doe").await;
// Returns: {"valid": true, "username": "john-doe", "confidence": 95, ...}
```

### Lead Generation Bot
```rust
// Quick validation for scraped LinkedIn URLs
if rig_is_valid(potential_lead_url).await {
    add_to_crm(potential_lead_url);
}
```

### Professional Network Analyzer
```rust
// Get human-readable validation for chat interfaces
let message = rig_validate_text(profile_url).await;
// Returns: "โœ… Valid profile @jane-smith (90% confidence)"
```

### Data Enrichment Pipeline
```rust
// Batch validate URLs with confidence scoring
let results = urls.iter().map(|url| rig_validate(url)).collect().await;
// Filter by confidence level for data quality
```

## ๐ŸŒŸ Key Features

- **๐Ÿค– AI-First Design** - Multiple API levels from simple booleans to rich structured data
- **๐ŸŽฏ Rig Framework Optimized** - Ergonomic helpers designed specifically for Rig tools
- **โšก Async & Sync APIs** - Full async support to prevent blocking runtime panics
- **๐Ÿ“Š Structured Responses** - `AIValidationResult` with confidence scores and decisions
- **๐Ÿ” Smart Validation** - Format checking, existence verification, and intelligent fallbacks
- **๐Ÿ“ Rich Error Context** - Detailed explanations with actionable suggestions
- **๐Ÿ›ก๏ธ Never Panics** - Comprehensive error handling throughout
- **๐Ÿš€ High Performance** - Optimized for concurrent operations

## ๐Ÿ“ฆ Installation

```toml
[dependencies]
credify = "0.4.0"
```

Or use cargo add:

```bash
cargo add credify
```

## ๐Ÿš€ Quick Start

### For AI Agents & Rig Framework (Recommended)

```rust
use credify::{rig_is_valid, rig_validate_text};

// Ultra-simple validation
if rig_is_valid("https://linkedin.com/in/johndoe").await {
    println!("Valid LinkedIn profile!");
}

// Get a human-readable response
let message = rig_validate_text("https://linkedin.com/in/johndoe").await;
// Returns: "โœ… Valid profile @johndoe (95% confidence)"
```

### For Rig Tool Implementation

```rust
// Complete Rig tool implementation
#[derive(Deserialize)]
struct ValidateLinkedInArgs {
    url: String,
}

#[derive(Deserialize, Serialize)]
struct LinkedInValidator;

#[async_trait]
impl Tool for LinkedInValidator {
    const NAME: &'static str = "validate_linkedin_profile";
    type Args = ValidateLinkedInArgs;
    type Output = String;
    type Error = ToolError;

    async fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: Self::NAME.to_string(),
            description: "Validates LinkedIn profile URLs and returns structured information".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "The LinkedIn profile URL to validate"
                    }
                },
                "required": ["url"]
            }),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        // Just one line! No runtime panics, perfect for Rig
        Ok(credify::rig_validate_json(&args.url).await)
    }
}
```

## ๐Ÿ“š API Overview

### ๐ŸŽฏ Ergonomic Rig Helpers (New!)

| Function | Returns | Use Case |
|----------|---------|----------|
| `rig_is_valid()` | `bool` | Quick true/false checks |
| `rig_validate_text()` | `String` | One-line human-readable responses |
| `rig_validate_json()` | `String` | Clean JSON for tool responses |
| `rig_validate()` | `RigValidationResult` | Structured data with all details |

### ๐Ÿค– AI-Optimized Functions

| Function | Returns | Use Case |
|----------|---------|----------|
| `ai_validate()` | `AIValidationResult` | Full structured data |
| `ai_validate_json()` | `String` | JSON for AI consumption |
| `validate_for_llm()` | `String` | Verbose text reports |

### ๐Ÿ”ง Traditional API

| Function | Returns | Use Case |
|----------|---------|----------|
| `is_valid_linkedin_profile_format()` | `bool` | Format checking only |
| `LinkedInValidator::is_valid_linkedin_profile_url()` | `Result<bool>` | Full validation |

## ๐Ÿ’ก Usage Examples

### 1. Rig Framework Integration (Recommended)

```rust
use credify::{rig_validate, RigValidationResult};
use rig::tool::Tool;

#[derive(Deserialize, Serialize)]
struct LinkedInChecker;

impl Tool for LinkedInChecker {
    const NAME: &'static str = "linkedin_checker";
    type Args = CheckArgs;
    type Output = String;
    type Error = MyError;

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        // One line - that's it!
        Ok(credify::rig_validate_json(&args.url).await)
    }
}

// Or use structured data
async fn check_with_details(url: &str) {
    let result: RigValidationResult = credify::rig_validate(url).await;
    
    println!("Valid: {}", result.valid);
    println!("Status: {}", result.status);
    println!("Action: {}", result.action);
    println!("Confidence: {}%", result.confidence);
    
    if let Some(username) = result.username {
        println!("Username: @{}", username);
    }
}
```

### 2. AI Agent Integration

```rust
use credify::{ai_validate, AIDecision};

async fn validate_for_ai(url: &str) {
    let result = ai_validate(url).await;
    
    // Simple boolean check
    if result.is_valid {
        println!("Profile is valid!");
    }
    
    // Use confidence for nuanced decisions
    if result.confidence >= 0.9 {
        println!("High confidence validation");
    }
    
    // AI-friendly decision enum
    match result.decision {
        AIDecision::Accept => {
            // Use the profile
            println!("Accepted: {}", result.username.unwrap_or_default());
        }
        AIDecision::Retry => {
            // Network issue, try again
            println!("Temporary issue, retry in a moment");
        }
        AIDecision::Reject => {
            // Invalid URL
            println!("Invalid: {}", result.reason);
        }
    }
}
```

### 3. Quick Validation

```rust
use credify::is_valid_linkedin_profile_format;

// Format check only (no network calls)
if is_valid_linkedin_profile_format("https://linkedin.com/in/johndoe") {
    println!("Format is valid!");
}

// Full validation with network check
use credify::LinkedInValidator;

let validator = LinkedInValidator::new()?;
match validator.is_valid_linkedin_profile_url(url) {
    Ok(true) => println!("Profile exists!"),
    Ok(false) => println!("Profile not found"),
    Err(e) => println!("Error: {}", e),
}
```

### 4. Async Operations

```rust
use credify::validate_linkedin_url_async;

// Async validation
let is_valid = validate_linkedin_url_async(url).await?;

// Async with AI response
let json = credify::ai_validate_json_async(url).await;
```

## ๐ŸŽฏ Complete Rig Framework Integration Guide

### Setting Up Credify with Rig

Credify is designed to work seamlessly with the Rig framework for building AI agents. Here's a complete guide to integrating LinkedIn validation into your Rig-powered AI system.

#### 1. Basic Tool Setup

```rust
use credify::rig_validate_json;
use rig::{completion::ToolDefinition, tool::Tool};
use serde::{Deserialize, Serialize};
use serde_json::json;

#[derive(Debug, Deserialize)]
struct ValidateArgs {
    url: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct LinkedInValidator;

#[async_trait::async_trait]
impl Tool for LinkedInValidator {
    const NAME: &'static str = "validate_linkedin_profile";
    type Args = ValidateArgs;
    type Output = String;
    type Error = Box<dyn std::error::Error + Send + Sync>;

    async fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: Self::NAME.to_string(),
            description: "Validates LinkedIn profile URLs and returns structured data about the profile".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "The LinkedIn profile URL to validate (e.g., https://linkedin.com/in/username)"
                    }
                },
                "required": ["url"]
            }),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        Ok(rig_validate_json(&args.url).await)
    }
}
```

#### 2. Using with Rig Agents

```rust
use rig::providers::openai;
use rig::completion::{Prompt, ToolDefinition};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create the LinkedIn validator tool
    let linkedin_tool = LinkedInValidator;
    
    // Set up your AI client
    let client = openai::Client::new(&std::env::var("OPENAI_API_KEY")?);
    
    // Create an agent with the LinkedIn validation tool
    let agent = client
        .agent("gpt-4")
        .preamble("You are a professional network analyst.")
        .tool(linkedin_tool)
        .build();
    
    // Use the agent to validate profiles
    let response = agent
        .prompt("Check if https://linkedin.com/in/satya-nadella is a valid LinkedIn profile")
        .await?;
    
    println!("Agent response: {}", response);
    Ok(())
}
```

#### 3. Advanced Usage with Multiple Response Types

```rust
use credify::{rig_is_valid, rig_validate, rig_validate_text};

// Quick boolean check for conditional logic
async fn quick_check(url: &str) -> bool {
    rig_is_valid(url).await
}

// Human-readable response for chat interfaces
async fn chat_response(url: &str) -> String {
    rig_validate_text(url).await
}

// Structured data for complex workflows
async fn detailed_check(url: &str) {
    let result = rig_validate(url).await;
    
    match result.confidence {
        90..=100 => println!("High confidence: proceed with profile"),
        70..=89 => println!("Medium confidence: verify manually"),
        _ => println!("Low confidence: search for alternative")
    }
}
```

#### 4. Real-World Use Case: Recruiting Assistant

```rust
#[derive(Debug, Serialize, Deserialize)]
struct RecruitingAssistant {
    linkedin_validator: LinkedInValidator,
}

impl RecruitingAssistant {
    async fn validate_candidate_profiles(&self, profiles: Vec<String>) -> Vec<CandidateStatus> {
        let mut results = Vec::new();
        
        for profile_url in profiles {
            let validation = rig_validate(&profile_url).await;
            
            results.push(CandidateStatus {
                url: profile_url,
                valid: validation.valid,
                username: validation.username,
                confidence: validation.confidence,
                action: match validation.valid {
                    true => "Schedule interview",
                    false => "Request updated profile"
                }.to_string(),
            });
        }
        
        results
    }
}

#[derive(Debug, Serialize)]
struct CandidateStatus {
    url: String,
    valid: bool,
    username: Option<String>,
    confidence: u8,
    action: String,
}
```

#### 5. Error Handling Best Practices

```rust
// Credify's Rig helpers NEVER panic, making them safe for production
async fn safe_validation(url: &str) -> String {
    // This will always return a valid JSON response, even on errors
    let result = rig_validate_json(url).await;
    
    // Parse the result to handle different scenarios
    if let Ok(parsed) = serde_json::from_str::<RigValidationResult>(&result) {
        if parsed.valid {
            format!("Profile @{} is valid", parsed.username.unwrap_or_default())
        } else {
            format!("Invalid profile: {}", parsed.status)
        }
    } else {
        "Validation service temporarily unavailable".to_string()
    }
}
```

#### 6. Batch Processing with Rig

```rust
use futures::future::join_all;

async fn batch_validate(urls: Vec<String>) -> Vec<(String, bool)> {
    let futures = urls.into_iter().map(|url| async move {
        let valid = rig_is_valid(&url).await;
        (url, valid)
    });
    
    join_all(futures).await
}
```

### Function Calling Patterns

#### Pattern 1: Simple Validation Tool

```rust
// Minimal implementation for basic needs
impl Tool for SimpleLinkedInChecker {
    async fn call(&self, args: Args) -> Result<String, Error> {
        if rig_is_valid(&args.url).await {
            Ok("Valid LinkedIn profile".to_string())
        } else {
            Ok("Invalid LinkedIn profile".to_string())
        }
    }
}
```

#### Pattern 2: Detailed Analysis Tool

```rust
// Rich responses for complex AI workflows
impl Tool for DetailedLinkedInAnalyzer {
    async fn call(&self, args: Args) -> Result<String, Error> {
        let result = rig_validate(&args.url).await;
        
        Ok(json!({
            "valid": result.valid,
            "username": result.username,
            "confidence": result.confidence,
            "status": result.status,
            "recommended_action": result.action,
            "profile_url": if result.valid {
                Some(format!("https://linkedin.com/in/{}", 
                    result.username.as_ref().unwrap_or(&"unknown".to_string())))
            } else {
                None
            }
        }).to_string())
    }
}
```

#### Pattern 3: Multi-Step Validation

```rust
// Complex validation with fallback strategies
impl Tool for SmartLinkedInValidator {
    async fn call(&self, args: Args) -> Result<String, Error> {
        // First, try the provided URL
        let result = rig_validate(&args.url).await;
        
        if result.valid {
            return Ok(rig_validate_json(&args.url).await);
        }
        
        // If invalid, try common variations
        if let Some(username) = extract_username(&args.url) {
            let variations = vec![
                format!("https://linkedin.com/in/{}", username),
                format!("https://www.linkedin.com/in/{}", username),
                format!("https://linkedin.com/in/{}/", username),
            ];
            
            for variant in variations {
                if rig_is_valid(&variant).await {
                    return Ok(rig_validate_json(&variant).await);
                }
            }
        }
        
        // Return detailed error information
        Ok(json!({
            "valid": false,
            "error": "Profile not found",
            "suggestions": [
                "Check the username spelling",
                "Verify the profile hasn't been deleted",
                "Try searching by name on LinkedIn"
            ]
        }).to_string())
    }
}
```

### Integration Tips

1. **Always use async**: All Rig helpers are async to prevent blocking
2. **Never panics**: Rig helpers return valid responses even on errors
3. **Structured responses**: Use `rig_validate_json` for consistent AI parsing
4. **Confidence scores**: Use confidence levels to make nuanced decisions
5. **Batch wisely**: Process multiple URLs concurrently for better performance

## โš ๏ธ Important: Async Usage

When using Credify in async contexts (like web servers or AI frameworks), **always use the async versions** to avoid runtime panics:

```rust
// โŒ WRONG - Can cause panic in async context
async fn my_tool() {
    let result = credify::ai_validate_json(url); // Panic!
}

// โœ… CORRECT - Use async version
async fn my_tool() {
    let result = credify::ai_validate_json_async(url).await; // Works!
}

// โœ… BEST - Use Rig helpers (always async)
async fn my_tool() {
    let result = credify::rig_validate_json(url).await; // Perfect!
}
```

## ๐Ÿ“Š Response Types

### RigValidationResult

```rust
pub struct RigValidationResult {
    pub valid: bool,              // Simple pass/fail
    pub username: Option<String>, // LinkedIn username if found
    pub confidence: u8,           // 0-100 percentage
    pub status: String,           // Human-readable status
    pub action: String,           // Suggested action for AI
}
```

### AIValidationResult

```rust
pub struct AIValidationResult {
    pub is_valid: bool,
    pub confidence: f32,          // 0.0 to 1.0
    pub decision: AIDecision,     // Accept/Retry/Reject
    pub username: Option<String>,
    pub reason: String,
    pub metadata: ValidationMetadata,
}
```

## ๐Ÿค– Why AI-Friendly Validation Matters

Traditional validation returns simple true/false or error codes. AI agents need rich context to make intelligent decisions:

- **Context-Rich Responses**: Understand why validation failed
- **Confidence Scores**: Make nuanced decisions based on certainty
- **Actionable Suggestions**: Know what to do next
- **Structured Data**: Easy to parse and reason about

## ๐Ÿ› ๏ธ Advanced Features

### Custom User Agent

```rust
let validator = LinkedInValidator::new_with_user_agent(
    "MyBot/1.0 (https://mybot.com)"
)?;
```

### Handling LinkedIn Authentication

LinkedIn often returns AUTH_REQUIRED (999 status) for valid profiles. Credify intelligently handles this:

```rust
// AUTH_REQUIRED is treated as a valid profile
let result = rig_validate(url).await;
if result.valid && result.status.contains("auth required") {
    println!("Profile likely exists but LinkedIn is blocking checks");
}
```

## ๐Ÿ“– More Examples

Check out the `examples/` directory for:

- `basic.rs` - Simple validation examples
- `rig_ergonomic.rs` - Ergonomic Rig API showcase
- `rig_integration.rs` - Full Rig framework integration with function calling
- `rig_async_proper.rs` - Advanced async patterns for Rig
- `batch_validator.rs` - Validate multiple URLs concurrently
- `llm_simple.rs` - LLM-friendly validation
- `ai_agent_demo.rs` - Complete AI agent implementation

### Quick Example: Rig Function Calling

```rust
// Define your tool
#[derive(Tool)]
#[tool(
    name = "linkedin_validator",
    description = "Validates LinkedIn profile URLs"
)]
struct LinkedInValidator;

// Implement the tool - just one line!
impl LinkedInValidator {
    async fn validate(&self, url: String) -> String {
        credify::rig_validate_json(&url).await
    }
}

// Use with your AI agent
let agent = client
    .agent("gpt-4")
    .tool(LinkedInValidator)
    .build();

let response = agent
    .prompt("Check these LinkedIn profiles for our hiring pipeline")
    .await?;
```

Run examples with:

```bash
cargo run --example rig_ergonomic
```

## ๐Ÿงช Testing

```bash
# Run all tests
cargo test

# Run with verbose output
cargo test -- --nocapture
```

## ๐Ÿ“„ License

Licensed under either of:

- Apache License, Version 2.0 ([LICENSE-APACHE]LICENSE-APACHE)
- MIT license ([LICENSE-MIT]LICENSE-MIT)

at your option.

## ๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## ๐Ÿ™ Acknowledgments

Built with โค๏ธ for the AI agent community. Special thanks to the [Rig framework](https://github.com/0xPlaygrounds/rig) team for inspiring the ergonomic API design.

---

<div align="center">
  <sub>Built by <a href="https://github.com/hghalebi">Hamze Ghalebi</a></sub>
</div>